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,
And just type the code as follows,
Code copy is here
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public void onBackPressed() {
- final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
- builder.setMessage("Are you want to do this");
- builder.setCancelable(true);
- builder.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.cancel();
- }
- });
- builder.setPositiveButton("close!", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- finish();
- }
- });
- AlertDialog alertDialog = builder.create();
- 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.
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.
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*