How to create a Custom Dialog box in android ?

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 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="15sp">  
   
   <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.

Video Tutorial :



Next Post Previous Post
No Comment
Add Comment
comment url