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.
![Snake-game-android-studio-source-code](https://learnoset.com/Learnoset/Projects/SnakeGame/snake-game-in-android-studio-with-source-code.jpg)
Snake game android studio source code
![Login-and-Register-Activity-Material-UI-design-in-Android-Studio](https://learnoset.com/Learnoset/Projects/LoginAndRegister05/login_register_05.png)
Login and Register Activity Material UI design in Android Studio
![Online-Quiz-Application-using-Firebase-and-Admob](https://learnoset.com/Learnoset/Projects/QuizAppAdmob/thumbnail.jpg)
Online Quiz Application using Firebase and Admob
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.