Custom Alert Dialog in Android Studio

custom alert dialog android

In this post, we are working on a custom alert dialog that is designed to set a layout for exit dialog from an application,

In this example we will show you how to design custom alert dialog in the android studio

1) Create a new layout res/layout/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="@dimen/_120sdp"
    android:background="@color/red"
    android:layout_gravity="center"
    android:id="@+id/txt">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/alert_dialog"
        android:src="@drawable/exit_dialog"
        android:layout_marginLeft="@dimen/_20sdp"
        android:layout_marginTop="@dimen/_10sdp">
    </ImageView>



    <TextView
        android:id="@+id/txt_dia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do you realy want to exit ?"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/_10sdp"
        android:textColor="@color/white"
        android:textSize="18dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/no_id"
        android:text="No"
        android:textSize="@dimen/_10sdp"
        android:layout_below="@id/txt_dia"
        android:layout_toLeftOf="@id/yes_id"
        android:layout_marginTop="@dimen/_20sdp"
        android:layout_marginRight="@dimen/_10sdp"
        android:textColor="@color/black"/>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/yes_id"
        android:text="Yes"
        android:textSize="@dimen/_10sdp"
        android:layout_alignParentRight="true"
        android:layout_below="@id/txt_dia"
        android:layout_centerInParent="true"
        android:layout_marginTop="@dimen/_20sdp"
        android:layout_marginRight="@dimen/_66sdp"
        android:textColor="@color/black"/>

</RelativeLayout>

2) Create a style.xml and paste this code

<?xml version="1.0" encoding="utf-8"?>
<resources>

        <style name="custom_dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:windowBackground">@drawable/popupbackground</item>
        </style>
</resources>

3) Use the code to call the layout in main_activity.java file where

  @Override
    public void onBackPressed() {
        final Dialog dialog = new Dialog(this,R.style.custom_dialog);
        dialog.setContentView(R.layout.custom_dialog);

        Button yes = dialog.findViewById(R.id.yes_id);
        Button no = dialog.findViewById(R.id.no_id);

        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
    }
}


4) If you want to exit your app with an exit button then use theĀ  
onBackPressed() method and call on exit button 

button5=findViewById(R.id.btn_exitapp_id);
button5.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        onBackPressed();
    }
});

or just use  the above code in the main layout When user press back button dialog will open