Alert Dialog in Android Studio
MainActivity.java
/* Code From Learnoset - Learn Code Online Android App.*/
/*Download Learnoset from PlayStore to learn coding*/
// create an object of AlertDialog.Builder class
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
// if setCancelable is true then dialog can be closed when user click outside the dialog
alertDialog.setCancelable(false);
// title of the dialog
alertDialog.setTitle("Your Title Here");
// message of the dialog
alertDialog.setMessage("Your Message here");
// set positive button / always appears on the bottom right corner with text (Right Btn)
alertDialog.setPositiveButton("Right Btn", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// handle click event on Right Btn like I'm showing Toast below
Toast.makeText(MainActivity.this, "Right Button Clicked", Toast.LENGTH_SHORT).show();
// use below code to hide the dialog on button click
//dialog.dismiss();
}
});
// set negative button / always appears to left of positive button with text (Left Btn)
alertDialog.setNegativeButton("Left Btn", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// handle click event on Left Btn like I'm showing Toast below
Toast.makeText(MainActivity.this, "Left Button Clicked", Toast.LENGTH_SHORT).show();
// use below code to hide the dialog on button click
//dialog.dismiss();
}
});
// show alert dialog
alertDialog.show();
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.
QR Code Scanner and Generator Application for Android
Simple Music Player App for Android (Offline Version)
Gamer Bazi - Tournament Application with Admob Ads & Web Admin Panel
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.