Android  

How To Add Android Exit Dialog To Android App Using Android Studio

Introduction

Android is one of the most popular operating systems for mobile. In this article, I will show you how to add Android Exit Dialog to Android App using Android Studio

Requirements

Steps to be followed -

Follow these steps to add Android Exit Dialog to Android App. I have included the source code in the attachment.

Step 1

Open Android Studio and start a new Android Studio Project.

Android

Step 2

You can choose your application name and location where your project is stored. If you wish to use C++ for coding the project, mark the "Include C++ support" box, and click "Next" button.

Android

Now, select the version of Android and select the target Android devices.

Android

Step 3

Now, add the activity and click "Next" button.

Android

Add the Activity name and click "Finish".

Android

Step 4

Go to activity_main.xml. This XML file contains the designing code for your Android app.

Android

The XML code is given below.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="abu.exitnotification.MainActivity">  
  8.   
  9.     <TextView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="EXIT NOTIFICATION APP!"  
  13.         app:layout_constraintBottom_toBottomOf="parent"  
  14.         app:layout_constraintLeft_toLeftOf="parent"  
  15.         app:layout_constraintRight_toRightOf="parent"  
  16.         app:layout_constraintTop_toTopOf="parent" />  
  17.   
  18. </android.support.constraint.ConstraintLayout>  

Step 5

Go to  Main Activity.java. This Java program is the back-end language for your Android app.

Android

The Java code is given below.

  1. package abu.exitnotification;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.support.v7.app.AlertDialog;  
  6. import android.content.DialogInterface;  
  7.   
  8. public class MainActivity extends AppCompatActivity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.     }  
  15.     @Override  
  16.     public void onBackPressed() {  
  17.   
  18.         AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);  
  19.         builder.setTitle(R.string.app_name);  
  20.         builder.setIcon(R.mipmap.ic_launcher);  
  21.         builder.setMessage("Do you want to exit?")  
  22.                 .setCancelable(false)  
  23.                 .setPositiveButton("Yes"new DialogInterface.OnClickListener() {  
  24.                     public void onClick(DialogInterface dialog, int id) {  
  25.                         finish();  
  26.                     }  
  27.                 })  
  28.                 .setNegativeButton("No"new DialogInterface.OnClickListener() {  
  29.                     public void onClick(DialogInterface dialog, int id) {  
  30.                         dialog.cancel();  
  31.                     }  
  32.                 });  
  33.         AlertDialog alert = builder.create();  
  34.         alert.show();  
  35.   
  36.     }  
  37. }   

Step 6

Now, go to menu bar and click "Make Project" or press ctrl+f9 in order to debug the app from errors.

Android

Step 7

Then, click "Run" button or press shift+f10 to run the project. Select "virtual machine" and click OK.

Android

Conclusion

We have successfully added Android Exit Dialog to the Android App.