Introduction
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:
- maven
- {
- url "https://jitpack.io"
- }
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
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="There is no Activity in this page"
- android:textSize="30dp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
Step 4
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page,







Join the conversation! Your thoughts help the community grow.