How to create a Custom Dialog box in android ?

Android Manifest Example

How to create a Custom Dialog box in android ?

How to make custom Dialog box in Android Jave :

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.

Custom Dialog Design :

Layout Name : dialog_layout.xml

XML

< ?xml version="1.0" encoding="utf-8"?> < < ImageView android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center_horizontal" android:src="@drawable/no_internet" /> < TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="This Is Custom Alert Dialog" android:textColor="@android:color/black" android:textSize="18sp" /> < LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="2"> < Button android:id="@+id/chancelBTN" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="5dp" android:layout_weight="1" android:background="#FFEB3B" android:text="Chancel" android:textColor="@android:color/black" android:textSize="18sp" /> < Button android:id="@+id/okBTN" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="5dp" android:layout_weight="1" android:background="#FFEB3B" android:text="OK" android:textColor="@android:color/black" android:textSize="18sp" /> < /LinearLayout> < /LinearLayout>

Custom Dialog Method :

private void ShowDialogBox (){ final AlertDialog.Builder alert = new AlertDialog.Builder(this); View mView = getLayoutInflater().inflate(R.layout.dialog_layout, null); alert.setView(mView); final AlertDialog alertDialog = alert.create(); alertDialog.setCancelable(false); mView.findViewById(R.id.chancelBTN).setOnClickListener(v -> { alertDialog.dismiss(); }); mView.findViewById(R.id.okBTN).setOnClickListener(v -> { Toast.makeText(this, "Clicked OK BTN", Toast.LENGTH_SHORT).show(); alertDialog.dismiss(); }); alertDialog.show(); }

How to Show Dialog ?

findViewById(R.id.showDialog).setOnClickListener(v -> { // code here === ShowDialogBox(); });

You can use ShowDialogBox(); to show your custom dialog in this Activity were you created above method.

buy source code ZIP file

Comments