𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼
Hello Hive Learners community members, I hope you all are well. Today we added some confirmation to our app. For example, if a action, we need to confirm by asking the user again that he wants to perform this action. May be user accidentally clicked that button. So we will use an AlertDialog today. Let's get started.
GitHub Link
Use this GitHub project to clone into your directory. The following lecture will constantly update it so you will never miss the latest code. Happy Coding!.
What Should I Learn
- How to create AlertDialog
- Add Buttons to AlertDialog
Assignment
- Show an AlertDialog on the button click
Procedure
First, we need to declare and initialize the AlertDialog. I am going to add the AlertDialog in the Welcome Activity.
Now in the logout button clock listeners, I am adding the Alert Dialog and moving the listener code inside the AletDialog listener. Alert Dialog can haButtonsutton with listeners, Positive, Negative, and Neutral.
I set the title and message for the AlertDialog and in the end, write the show() function to show the AlerDialog on button click.
alertDialog.setTitle("Confirmation");
alertDialog.setMessage("Do you really want to logout?");
alertDialog.setPositiveButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).setNegativeButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sharedPreferences.edit().remove("login_account").apply();
startActivity(new Intent(Welcome_Activity.this, MainActivity.class));
Toast.makeText(Welcome_Activity.this, "Logout Successfully", Toast.LENGTH_SHORT).show();
finish();
}
});
alertDialog.show();
When a user clicks on Yes then the code in the NegationButton listeners will run and If the user clicks on No then the Positive Listnere code run that will cancel the AlerDialog.
Now I am using the ProgressBAr code in the Yes Button and moving the Yes button code inside the ProfressBar. It will show a ProgressBar for two seconds and then execute the logout code.
logout_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.setTitle("Confirmation");
alertDialog.setMessage("Do you really want to logout?");
alertDialog.setPositiveButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).setNegativeButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (progressDialog.isShowing()) {
progressDialog.cancel();
}
sharedPreferences.edit().remove("login_account").apply();
startActivity(new Intent(Welcome_Activity.this, MainActivity.class));
Toast.makeText(Welcome_Activity.this, "Logout Successfully", Toast.LENGTH_SHORT).show();
finish();
}
}, 2000);
}
});
alertDialog.show();
}
});
Now let's run and check the app if the AlertDialog is showing or not.

Thank You
