How to create a Custom Dialog box in android in Java?


How to create a Custom Dialog box in android in Java:

A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.

Step 1 : Color add: (you change this color as your wish)

Copy this color and paste this color into your color.xml
<color name="main_color">#1F1F22</color>1F1F22
<color name="main_color2">#009688</color>
<color name="main_color3">#E91E63</color>
<color name="listItem_color">#004B7D</color>
<color name="listItem_textColor">#93CAFF</color>

Step 2 : Custom Dialog Box Design :

Layout Name : dialog_exitapp.xml
Create a file into layout folder named: dialog_exitapp.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="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:background="@drawable/custom_dialog_bg"
>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/main_color"
android:orientation="vertical"
android:paddingStart="30dp"
android:paddingEnd="30dp"
>

<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:src="@mipmap/ic_launcher"
android:layout_gravity="center"
android:layout_marginTop="3dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="3dp"
android:text="Hope to see you soon!"
android:textColor="@color/main_color2"
android:textSize="21sp"
android:textStyle="bold" />

<TextView
android:id="@+id/notifyUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="3dp"
android:layout_marginBottom="15sp"
android:text="Do you want to exit?"
android:textColor="@color/listItem_textColor"
android:textSize="16sp"
android:textStyle="bold" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
>

<Button
android:id="@+id/cancelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:backgroundTint="@color/main_color2"
android:text="Cancel"
android:textColor="@color/white" />

<Button
android:id="@+id/exitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:backgroundTint="@color/main_color3"
android:text="Exit"
android:textColor="@color/white" />

</LinearLayout>

</LinearLayout>


</LinearLayout>

Step 3 : Custom Dialog Box Background Shape Design :

Shape Name : custom_dialog_bg.xml
Create a file into drawable folde named: custom_dialog_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>

<corners android:radius="20dp"/>
<solid android:color="@color/main_color"/>

</shape>

Step 4 : Custom Dialog Box Java Code :

Java Code :  Java method
Copy this code and paste this code into your MainActivity or Homeactivity Java file into onBackPressed() method.
@Override
public void onBackPressed() {
    Dialog exitDialog = new Dialog(MainActivity.this);
    exitDialog.setContentView(R.layout.dialog_exitapp);
exitDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
exitDialog.getWindow().setBackgroundDrawable(getDrawable(R.drawable.custom_dialog_bg));

Button cancelBtn = exitDialog.findViewById(R.id.cancelBtn);
Button exitBtn = exitDialog.findViewById(R.id.exitBtn);

cancelBtn.setOnClickListener(v -> {
exitDialog.dismiss();
});

exitBtn.setOnClickListener(v -> {
exitDialog.dismiss();
super.onBackPressed();
});

exitDialog.setCancelable(false);
exitDialog.show();

}
Thanks from
Learn with Debasish
Next Post
No Comment
Add Comment
comment url