CustomDialog

In this tutorial, we show you how to create a custom dialog in Android. First of all we will create custom_dialog.xml:

custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray"
    android:padding="10dp">

    <LinearLayout
        android:id="@+id/llTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="horizontal">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/smallIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@drawable/small_logo" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:gravity="center_vertical"
            android:text="Custom Dialog"
            android:textColor="@android:color/holo_red_dark"
            android:textSize="10pt" />

    </LinearLayout>

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tvMsg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/llTitle"
        android:layout_margin="15dp"
        android:text="This is demonstrate how to create Custom Dialog into your project"
        android:textColor="@android:color/white"
        android:textSize="9pt" />

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvMsg"
        android:layout_alignParentRight="true"
        android:layout_marginTop="10dp">

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btnGo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Go" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel" />

    </androidx.appcompat.widget.LinearLayoutCompat>
</RelativeLayout>

Now create a method to show CustomDialog as:

public void customDialog(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    LayoutInflater inflater = getLayoutInflater();
    View customDialog =inflater.inflate(R.layout.custom_dialog, null, false);
    builder.setView(customDialog);

    final AlertDialog dialog = builder.create();

    AppCompatButton btnGo = customDialog.findViewById(R.id.btnGo);
    btnGo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
            Toast.makeText(MainActivity.this, "You can go ahead.", Toast.LENGTH_SHORT).show();
        }
    });

    AppCompatButton btnCancel = customDialog.findViewById(R.id.btnCancel);
    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
            Toast.makeText(MainActivity.this, "You can not go ahead.", Toast.LENGTH_SHORT).show();
        }
    });

    dialog.show();

    // below line keep dialog displayed when rotate screen, iff these lines not present
    // an exception will through, so keep best practice to include these lines
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    dialog.getWindow().setAttributes(lp);
}


Result of above code snippet is: