Android App DevelopmentEducationProgramming

Material Exit Dialog in Android

Create Material Exit Dialog in Android
Create Material Exit Dialog in Android

Hey Guys, We are back with Another Android App Tutorial. Today we are Going to Discuss Material Exit Dialog in Android.

Nowadays users are preferring apps that are easy to use with a Good Looking UI. So it is very important to develop a Good UI for an App. 

Using Google’s Material Design Library we can Design an Awesome UI with ease. Google made it Easy to Develop Material Design UI with Material Elements and Options. 

Moreover, Material Design is Open-source and available free to use by anyone. And It is available for Android, IOS, Web, and Flutter. So you can use it on Android as well as IOS. The Best thing is it Provides almost all the components that are used to design Mobile Apps and Web Apps.

How Material Design Useful?

Material Design provides all the Components. Let’s See in Android. 

  1. It Provides App Bars
  2. It Provides Bottom Navigation
  3. It Provides Buttons
  4. It Provides Floating Action Buttons
  5. It Provides Cards
  6. It Provides Check Boxes
  7. It Provides Chips
  8. It Provides Date Pickers
  9. It Provides Dialogs
  10. It Provides Menus
  11. It Provides Navigation Drawer
  12. It Provides Progress Indicators
  13. It Provides Radio Buttons
  14. It Provides Bottom Sheets
  15. It Provides Sliders
  16. It Provides Snack bars
  17. It Provides Switches
  18. It Provides Tabs
  19. It Provides Text Fields
  20. It Provides Time Pickers

In this tutorial, We are going to Use Material Dialog as Our Exit Dialog with Yes Or No Click Action and an exit message on the Dialog.

Let’s Go for Development.

Create a New Android Project or Open an Existing Project where you are going to implement exit Dialog.

I have Created a Sample App.


Add this Library to your app-level build.gradle file. If you working on android R it will be added by default. But On previous versions, you have to add it manually.

// Material Design Library

dependencies {

    implementation ''

Now Let’s Create a Utils Class. So We can Use Material Dialog Globally.

package com.example.sampleapp.Apputils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;

import com.example.sampleapp.R;

public class AppUtills {

    public interface AlertMagnatic {
        public abstract void PositiveMethod(DialogInterface dialog, int id);
        public abstract void NegativeMethod(DialogInterface dialog, int id);

    public static void ExitDialog(Context mContext, String title, String msg, String positiveBtnCaption, String negativeBtnCaption, boolean isCancelable, final AlertMagnatic target) {

        MaterialAlertDialogBuilder materialAlertDialogBuilder = new MaterialAlertDialogBuilder( mContext );

        int imageResource = R.drawable.dialog_background;
        @SuppressLint("UseCompatLoadingForDrawables") Drawable image = mContext.getResources().getDrawable(imageResource);

        materialAlertDialogBuilder.setTitle( title ).setMessage( msg ).setCancelable( false ).setPositiveButton( positiveBtnCaption, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                target.PositiveMethod(dialogInterface, i);
        } ).setNegativeButton( negativeBtnCaption, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                target.NegativeMethod(dialogInterface, i);

        } ).setBackground( image );

        materialAlertDialogBuilder.setCancelable( isCancelable );;
        if (isCancelable){
            materialAlertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {

                public void onCancel(DialogInterface arg0) {
                    target.NegativeMethod(null, 0);



I use abstact methods in Click actions for setting Dynamic Click actions where ever we use this Dialog.

Now We are going to Implement Exit Dialog in MainActivity. The Dialog is Shown to the User when he Presses the back button on MainActivity or on your Default Activity. So Our Material Dialog goes Under the OnBackpressed method.

package com.example.sampleapp;


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import com.example.sampleapp.Apputils.AppUtills;

public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    protected void onCreate(Bundle savedInstanceState) {

        mActivity = MainActivity.this;
        mContext = mActivity.getApplicationContext();


    public void onBackPressed() {

        AppUtills.ExitDialog(mActivity, getResources().getString(R.string.dialog_title), getResources().getString(R.string.dialog_message),getResources(). getString(R.string.positive_btn),
                getResources().getString(R.string.negative_btn), true, new AppUtills.AlertMagnatic() {
                    public void PositiveMethod(DialogInterface dialog, int id) {
                        Intent a = new Intent( Intent.ACTION_MAIN );
                        a.addCategory( Intent.CATEGORY_HOME );
                        a.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
                        startActivity( a );

                    public void NegativeMethod(DialogInterface dialog, int id) {

Woah! We Achieved material Exit Dialog in Our App. So When Ever User clicks the back button the Dialog shows. And you can Customize the Dialog with your custom background, title and you can also add a Custom view for the dialog.

Wanna Learn more Android Tutorials. Check them Here.

Related Articles

Back to top button