[CustomDialog.java]

public class CustomDialog extends Dialog {

    private TextView mTitleView;
    private TextView mContentView;
    private Button mLeftButton;
    private Button mRightButton;
    private String mTitle;
    private String mContent;
    private String mLeftButtonText;
    private String mRightButtonText;

    private View.OnClickListener mLeftClickListener;
    private View.OnClickListener mRightClickListener;

    // 클릭버튼이 하나일때 생성자 함수로 클릭이벤트를 받는다.
    public CustomDialog(Context context,
                        String title,
                        String content,
                        String buttonText,
                        View.OnClickListener singleListener) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        this.mTitle = title;
        this.mContent = content;
        this.mRightButtonText = buttonText;
        this.mRightClickListener = singleListener;
    }

    // 클릭버튼이 확인과 취소 두개일때 생성자 함수로 이벤트를 받는다
    public CustomDialog(Context context, String title,
                        String content,
                        String leftButtonText,
                        String rightButtonText,
                        View.OnClickListener leftListener,
                        View.OnClickListener rightListener) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        this.mTitle = title;
        this.mContent = content;
        this.mLeftButtonText = leftButtonText;
        this.mRightButtonText = rightButtonText;
        this.mLeftClickListener = leftListener;
        this.mRightClickListener = rightListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 다이얼로그 외부 화면 흐리게 표현
        WindowManager.LayoutParams lpWindow = new WindowManager.LayoutParams();
        lpWindow.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        lpWindow.dimAmount = 0.8f;
        getWindow().setAttributes(lpWindow);

        setContentView(R.layout.dialog_custom_layout);

        mTitleView = (TextView) findViewById(R.id.txt_title);
        mContentView = (TextView) findViewById(R.id.txt_content);
        mLeftButton = (Button) findViewById(R.id.left_button);
        mRightButton = (Button) findViewById(R.id.right_button);

        // 제목과 내용을 생성자에서 셋팅한다.
        mTitleView.setText(mTitle);
        mContentView.setText(mContent);
        mLeftButton.setText(this.mLeftButtonText);
        mRightButton.setText(this.mRightButtonText);

        // 클릭 이벤트 셋팅
        if (mLeftClickListener != null && mRightClickListener != null) {
            mLeftButton.setOnClickListener(mLeftClickListener);
            mRightButton.setOnClickListener(mRightClickListener);
        } else if (mLeftClickListener != null) {
            mLeftButton.setOnClickListener(mLeftClickListener);
            mRightButton.setVisibility(View.GONE);
        } else if (mRightClickListener != null) {
            mRightButton.setOnClickListener(mRightClickListener);
            mLeftButton.setVisibility(View.GONE);
        }
    }

}

[dialog_custom_layout.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"
    android:gravity="center"
    >

    <LinearLayout
        android:layout_width="335dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constrainedHeight="true"
        app:layout_constraintHeight_max="250dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:padding="20dp"
        android:background="@drawable/corner_round_layout">

        <TextView
            android:id="@+id/txt_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="타이틀"
            android:gravity="center"
            android:textColor="@android:color/black"
            android:textSize="18dp" />

        <TextView
            android:id="@+id/txt_content"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:lineSpacingExtra="5dp"
            android:text="안녕하세요. 다이알로그입니다.\n두줄입니다.\n세줄입니다."
            android:gravity="center"
            android:textColor="@android:color/black"
            android:textSize="16dp" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:orientation="horizontal" >

            <kr.kfcc.insurance.CustomUI.CustomButton
                android:id="@+id/left_button"
                android:layout_width="0dp"
                android:layout_height="56dp"
                android:layout_weight="1"
                android:text="취소"
                android:textSize="16dp"
                android:textColor="#333333"
                android:layout_marginRight="9dp"
                android:fontFamily="@font/spoqahansansneo_bold"
                app:corner_Radius="50dp"
                app:stroke_Width="0dp"
                app:border_Color_Start="#000000"
                app:border_Color_End="#000000"
                app:background_Color_Start="#DFE5E8"
                app:background_Color_End="#DFE5E8"
                />

            <kr.kfcc.insurance.CustomUI.CustomButton
                android:layout_width="0dp"
                android:layout_height="56dp"
                android:layout_weight="1"
                android:id="@+id/right_button"
                android:text="확인"
                android:textSize="16dp"
                android:textColor="#FFFFFF"
                android:fontFamily="@font/spoqahansansneo_bold"
                app:corner_Radius="50dp"
                app:stroke_Width="0dp"
                app:border_Color_Start="#000000"
                app:border_Color_End="#000000"
                app:background_Color_Start="#3074f0"
                app:background_Color_End="#3074f0"
                />
        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

[사용방법]

View.OnClickListener cancleListener = new View.OnClickListener() {
    public void onClick(View v) {
        mCustomDialog.dismiss();
    }
};

View.OnClickListener exitListener = new View.OnClickListener() {
    public void onClick(View v) {

    }
};

// 버튼 2개
mCustomDialog = new CustomDialog(this,
        "", // 제목
        "제출하지 않고 나가시겠어요?\n(촬영한 " + ImageBucket.getInstance().getImageSize() + "장의 사진은 삭제됩니다.)", // 내용
        "취소",
        "나가기",
        cancleListener, // 왼쪽 버튼 이벤트
        exitListener); // 오른쪽 버튼 이벤트

// 버튼 1개
mCustomDialog = new CustomDialog(this, 
        "", 
        "테스트 메세지 입니다.", 
        "확인", 
        cancleListener);

mCustomDialog.show();

 

+ Recent posts