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.
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 the "Next" button.
Now, select the version of Android and select the target Android devices.
Step 3
Now, add the activity and click the "Next" button.
Add the Activity name and click "Finish".
Step 4
Go to activity_main.xml. This XML file contains the designing code for your Android app.
The XML code is given below.
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="abu.exitnotification.MainActivity">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="EXIT NOTIFICATION APP!"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- </android.support.constraint.ConstraintLayout>
Step 5
Go to Main Activity.java. This Java program is the back-end language for your Android app.
The Java code is given below.
- package abu.exitnotification;
-
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.app.AlertDialog;
- import android.content.DialogInterface;
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public void onBackPressed() {
-
- AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
- builder.setTitle(R.string.app_name);
- builder.setIcon(R.mipmap.ic_launcher);
- builder.setMessage("Do you want to exit?")
- .setCancelable(false)
- .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- finish();
- }
- })
- .setNegativeButton("No", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.cancel();
- }
- });
- AlertDialog alert = builder.create();
- alert.show();
-
- }
- }
Step 6
Now, go to the menu bar and click "Make Project" or press ctrl+f9 in order to debug the app from errors.
Step 7
Then, click the "Run" button or press shift+f10 to run the project. Select "virtual machine" and click OK.
Conclusion
We have successfully added Android Exit Dialog to the Android App.