Android

[Android] Dialog

배현진 2023. 5. 4. 10:22

Custom Dialog와 라이브러리를 이용한 간단한 구현 방법을 알아본다.

 

[ Android Studio - Java ]


Custom Dialog

1. 원하는 디자인으로 커스텀 다이얼로그 레이아웃 파일 작성

<?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="wrap_content"
    android:padding="30dp"
    android:layout_gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_gravity = "center"
        android:padding = "20dp"
        android:text = "사진을 불러올 기능을 선택하세요" />

    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content" >

        <Button
            android:id = "@+id/Btn_Camera"
            android:layout_width = "0dp"
            android:layout_height = "wrap_content"
            android:layout_weight = "1"
            android:text = "카메라" />

        <Button
            android:id = "@+id/Btn_Gallery"
            android:layout_width = "0dp"
            android:layout_height = "wrap_content"
            android:layout_weight = "1"
            android:text = "갤러리" />

    </LinearLayout>

</LinearLayout>

 

2. 액티비티에서 Dialog 초기화 및 레이아웃 연결

private Dialog dialog;
     btn_popup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(_____Activity.this);
                dialog.setContentView(R.layout.dialog);
                dialog.show();

                Button Btn_Camera = dialog.findViewById(R.id.Btn_Camera);
                Btn_Camera.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    	// 실행 내용 작성
                        dialog.dismiss();
                    }
                });

                Button Btn_Gallery = dialog.findViewById(R.id.Btn_Gallery);
                Btn_Gallery.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    	// 실행 내용 작성
                        dialog.dismiss();
                    }
                });
            }
        });

 


LakuePopupActivity

https://github.com/lakue119/LakuePopupActivity

 

GitHub - lakue119/LakuePopupActivity

Contribute to lakue119/LakuePopupActivity development by creating an account on GitHub.

github.com

 

1. 프로젝트 수준의 gradle 수정

    allprojects {
        repositories {
        // ...
        maven { url 'https://jitpack.io' }
        }
    }

 

2. 모듈 수준의 gradle 수정

    dependencies {
         implementation 'com.github.lakue119:LakuePopupActivity:1.0.1'
    }

 

3. 원하는 activity에 구현

     btn_show_popup1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getBaseContext(), PopupActivity.class);
                intent.putExtra("type", PopupType.NORMAL);
                intent.putExtra("gravity", PopupGravity.CENTER);
                intent.putExtra("title", "공지사항");
                intent.putExtra("content", "Popup Activity was made by Lakue");
                intent.putExtra("buttonCenter", "종료");
                startActivityForResult(intent, 1);
            }
        });