You might have made use of Toast to display messages that fades in stays for few seconds and fades away. If you want to display some message and let user select the response (Yes or No) then we can make use of Dialog called as AlertDialog.

AlertDialog is a subclass of Dialog from android.app package. You can create a dialog with one, two or even three buttons. It is also possible to display only text message using setMessage() method.
There are three functions for adding Buttons to Android Dialog,
setPositiveButton(int textId, DialogInterface.OnClickListener listener) :
This is Yes button, when clicked the code written in the OnClickListener onClick() method will be displayed.
setNegativeButton(int textId, DialogInterface.OnClickListener listener) :
This is just like the setPositiveButton method which acts a "NO" negative button, we will write the logic in OnClickListener anonymous class.
setNeutralButton(int textId, DialogInterface.OnClickListener listener) :
This is how we can set the 3rd button. It is a called the Neutral button.
Android AlertDialog SnippetAlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("AlertDialog Example");
builder.setMessage("This is an Example of Android AlertDialog with 3 Buttons!!");
//Button One : Yes
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Yes button Clicked!", Toast.LENGTH_LONG).show();
}
});
//Button Two : No
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "No button Clicked!", Toast.LENGTH_LONG).show();
dialog.cancel();
}
});
//Button Three : Neutral
builder.setNeutralButton("Can't Say!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Neutral button Clicked!", Toast.LENGTH_LONG).show();
dialog.cancel();
}
});
AlertDialog diag = builder.create();
diag.show();
- Change Android Toast background color
- Maven : java.lang.ClassNotFoundException: Xmx512m
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Android Alert Dialog with Checkboxes example
- Android Error Generating Final Archive - Debug Certificate Expired
- How to add Newline to text in Android TextView
- Read Text file from SD Card : Android Programming
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- ActivityManager Warning: Activity not started, its current task has been brought to the front
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Android Developers Bluetooth Tutorial
- java.lang.ClassNotFoundException android.support.v7.widget.Toolbar [Fix]
- Android: Save Data in local Db using Android Room
- Channel 50 SMSes received every few minutes Android Phones
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Changing Android Intent Tittle using java code
- Android : No Launcher activity found! Error
- How to change TextView or EditText Text Color on Focus and on Press
- How to display Toast on Button Click : Android
- Android : Execute some code after back button is pressed
- Stop android adb service from command prompt or terminal
- [Soluiton] You already have the latest version of Android Studio installed
- Create Custom Android AlertDialog
- Android R Cannot Be Resolved To A Variable
- How to make Android EditText not editable
- How to check if Java main thread is alive - Java
- ERROR x86 emulation currently requires hardware acceleration. Intel HAXM is not installed on this machine - Android
- This operation couldnt be completed. Unable to locate a Java Runtime. [macOS] - MacOS
- SDK Manager: failed to install : Android Studio on Windows - Android-Studio
- List of Programming Languages Supported by Notepad++ - NotepadPlusPlus
- How to force quit or kill Notepad++ Process - NotepadPlusPlus
- TypeError: must be str, not int - Python
- How to Refresh Mac Desktop - MacOS