Alert Dialog Setup In Android Using Android Studio

In this article, we are going to see how to set up an Alert dialog to the back button in an Android app using Android Studio.

Introduction

In this article, we are going to see how to set up an Alert dialog to the back button in Android app using Android Studio. It is used to alert the user by asking if they want to close or continue. This alert dialog can be provided by us.

Step 1

Create a new project in Android Studio.

Android
 
Give a name to the project and click "Next".
 
Android
 
Select the "Phone and Tablet" and click "Next".
 
Android
 
Select an empty activity and click "Next".
 
Android
 
At last, give the activity a name and click on "Finish".
 
Android
 
Step 2

Setup the gradle by just locating the Gradle Scripts>>Build.Gradle

Android
 
And type the following dependency in your app's build.gradle.
 
Android
 
Code copy is here:
  1. maven  
  2. {  
  3.    url "https://jitpack.io"  
  4. }   
Step 3

 Next, go to app >> res >> layout >> activity_main.xml. Select activity page.

Android
 
And just type the code as follows:
 
Android
 
Code copy is here:
  1. <TextView  
  2.    android:layout_width="wrap_content"  
  3.    android:layout_height="wrap_content"  
  4.    android:text="There is no Activity in this page"  
  5.    android:textSize="30dp"  
  6.    app:layout_constraintBottom_toBottomOf="parent"  
  7.    app:layout_constraintLeft_toLeftOf="parent"  
  8.    app:layout_constraintRight_toRightOf="parent"  
  9. app:layout_constraintTop_toTopOf="parent" />   
Step 4

Next, go to app >> java>>Mainactivity.java. Select Mainactivity page:

Android
And just type the code as follows:
 
Android
 
Code copy is here:
  1. public class MainActivity extends Activity {  
  2.     @Override  
  3.     protected void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.activity_main);  
  6.     }  
  7.     @Override  
  8.     public void onBackPressed() {  
  9.             final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);  
  10.             builder.setMessage("Are you want to do this");  
  11.             builder.setCancelable(true);  
  12.             builder.setNegativeButton("Yes"new DialogInterface.OnClickListener() {  
  13.                 @Override  
  14.                 public void onClick(DialogInterface dialogInterface, int i) {  
  15.                     dialogInterface.cancel();  
  16.                 }  
  17.             });  
  18.             builder.setPositiveButton("close!"new DialogInterface.OnClickListener() {  
  19.                 @Override  
  20.                 public void onClick(DialogInterface dialogInterface, int i) {  
  21.                     finish();  
  22.                 }  
  23.             });  
  24.             AlertDialog alertDialog = builder.create();  
  25.             alertDialog.show();  
Step 5

After Step 4, sync all the dependency gradles and Mainactivity.java resource files by clicking the Sync button on the top right corner of the gradle page.

 
Android
 
Step 6

Verify the preview.

->After the code is applied, the preview will appear like this.

Android
 
Step 7

Next, go to Android Studio and deploy the application. Select an Emulator or your Android mobile with USB debugging enabled. Give it a few seconds to make installations and set permissions.

Android
 
Run the application in your desired emulator (Shift + F10).
 
Android
 
Explanation of source code

The source code provided in this article is just the dependencies of Alert dialog controller and the code used in activity_main.xml will make the back button alert and to define its attributes.

Summary

In this article we created the app named Alert Dialog, then we have inserted a Gradle and we learned how to give an alert when the back button is pressed and finally we have deployed that as  output.

*Support and Share, Thank You*