본문 바로가기
안드로이드 자바

[Android][JAVA] 반투명 로딩다이얼로그 구현하기

by teamnova 2023. 5. 21.

안녕하세요 오늘은 반투명 로딩다이얼로그를 구현해보겠습니다.

반투명 로딩다이얼로그를 구현하기 위해서는  다음과 사진과 같이 각각 2개의 java , xml 형태의 파일을 만들어서 코드를 작성해줘야 합니다.

구현하기 위해 필요한 요소들

먼저 원하는 형태의 다이얼로그를 만들어주겠습니다.

dialog_progress.xml 의 코드는 다음과 같습니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="6dp"
    android:gravity="center"
    >
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="72dp"
        android:layout_gravity="center_vertical"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Loading..."
        android:textStyle="bold"
        android:textColor="@android:color/white"
        android:textSize="16dp" />
</LinearLayout>

위에서 만든 progress dialog 레이아웃를 세팅하기 위해서 다음과 같이 코드를 작성해줍니다.

ProgressDialog 의 코드는 다음과 같습니다.

package com.example.stickodetest;

import android.app.Dialog;
import android.content.Context;
import android.view.Window;

import androidx.annotation.NonNull;

public class ProgressDialog extends Dialog { //커스텀 다이얼로그
    public ProgressDialog(@NonNull Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE); // 다이얼로그 제목 안 보이게
        setContentView(R.layout.dialog_progress);
    }
}

그다음  메인액티비티에서 커스텀한 다이얼로그를 불러오겠습니다.

mainActivity의 코드는 다음과 같습니다.

package com.example.stickodetest;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    private com.example.stickodetest.ProgressDialog customProgressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //로딩창 객체 생성
        customProgressDialog = new ProgressDialog(this);
        customProgressDialog.setCancelable(false); // 로딩창 주변 클릭 시 종료 막기
        //로딩창을 투명하게 하는 코드
        customProgressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        //getWindow (): 현재 액티비티의 Window 객체를 가져와서 Window 객체를 통해 뷰들의 위치 크기, 색상 조절
        //Window는 View 의 상위 개념으로, 뷰들을(버튼, 텍스트뷰, 이미지뷰) 감쌓고 있는 컨테이너 역할을 함
        customProgressDialog.show();

    }
}

 

다음은 메인액티비티의 레이아웃 코드를 작성해보겠습니다.

activirt_main.xml 코드는 다음과 같습니다.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/purple_200"
        android:gravity="center"
        android:text="mainActivity"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />


</androidx.constraintlayout.widget.ConstraintLayout>

 

완성된 모습은 다음과 같습니다.