How to create Custom Exit Alert Dialog in Android Studio

Hello Tech Lovers! Do you want to create a custom Alert Dialog for Android App in Android Studio? Today I am going to show you how you can create a custom Android Exit Alert Dialog Box using JAVA. The default AlertDialog makes use of the defined components like setIcon, setTitle, setMessage, setPositiveButton, etc but with Custom AlertDialog we can have the dialog customized and can define the layout of the dialog in XML as required.


Custom Alert Dialog in Android Studio

We will have 3 files, one is Java, and two will be XML files. One XML file will be for the Activity and the Second will be for the Custom Alert Dialog. 


activity_alert_box.xml

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

<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".Alert_Box">

    <Button

        android:id="@+id/alertbutton"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Alert"

        android:layout_centerInParent="true"

        android:backgroundTint="#ff0000"/>

</RelativeLayout>

The above XML file is the XML file for the Activity, now I am going to provide the JAVA code for the Custom Android Exit Alert Dialog.

Alert_Box.java

package com.freetoreads.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.DialogCompat;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.ContentProviderClient;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Alert_Box extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_box);
        Button alert = (Button) findViewById(R.id.alertbutton);
        alert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater layoutInflater = LayoutInflater.from(Alert_Box.this);
                View AlertView = layoutInflater.inflate(R.layout.custom_alert, null);
                Button AlertYes = (Button) AlertView.findViewById(R.id.Dialog_Yes);
                Button AlertNo = (Button) AlertView.findViewById(R.id.Dialog_No);
                AlertDialog.Builder alertBox = new AlertDialog.Builder(Alert_Box.this);
                 alertBox.setView(AlertView);
                alertBox.setCancelable(false);
                AlertDialog aleart = alertBox.create();
                aleart.show();
                AlertYes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                AlertNo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        aleart.cancel();
                        alert.setText("EHTISHAM ALI");
                    }
                });
            }
        });
    }
}

Now the last file is the XML file for the Dialog Box, You will have to design the Dialog Box in this XML file.

custom_alert.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp">
    <ImageView
        android:id="@+id/AlertImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@drawable/alert_image"></ImageView>
    <TextView
        android:id="@+id/Dialog_Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/AlertImage"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="15dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="15dp"
        android:layout_marginBottom="15dp"
        android:fontFamily="sans-serif-condensed-medium"
        android:text="Do you want to\nclose the app?"
        android:textColor="@color/teal_700"
        android:textSize="30dp"></TextView>
    <Button
        android:id="@+id/Dialog_Yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/Dialog_Text"
        android:layout_alignStart="@id/Dialog_Text"
        android:backgroundTint="#FF0000"
        android:padding="10dp"
        android:text="Yes"></Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Dialog_No"
        android:layout_toRightOf="@id/Dialog_Yes"
        android:layout_marginLeft="20dp"
        android:padding="10dp"
        android:backgroundTint="#228B22"
        android:layout_below="@id/Dialog_Text"
        android:text="No"></Button>
    </RelativeLayout>

In this way, you can easily create a Custom Exit Alert Dialog Box for Android App using Android Studio. If you face any issue or error please comment below I will get back to you as soon as possible. Thank You

Post a Comment

Previous Post Next Post