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 the 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.
Give a name to the project and click "Next".
Select the "Phone and Tablet" and click "Next".
Select an empty activity and click "Next".
At last, give the activity a name and click on "Finish".
Step 2
Setup the Gradle by just locating the Gradle Scripts>>Build. Gradle
And type the following dependency in your app's build.gradle.
Code copy is here: 
  1. maven  
  2. {  
  3.    url ""  
  4. }   
Step 3
Next, go to app >> res >> layout >> activity_main.xml. Select activity page.
And just type the code as follows:
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>> Select Mainactivity page,
And just type the code as follows,
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.   ;  
Step 5
After Step 4, sync all the dependency gradles and resource files by clicking the Sync button on the top right corner of the gradle page.
Step 6
Verify the preview.
->After the code is applied, the preview will appear like this.
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.
Run the application in your desired emulator (Shift + F10).
Explanation of source code
The source code provided in this article is just the dependencies of the Alert dialog controller and the code used in activity_main.xml will make the back button alert and to define its attributes.


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*

Similar Articles