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();
- Check Internet Connection WIFI 4G is active on Android Programmatically
- Android Emulator cannot be opened because the developer cannot be verified. [M1 Mac]
- How to Change Android Toast Position?
- Fail to connect to camera service Android java RuntimeException
- How to create Custom RatingBar Android Programming Tutorial
- Fixing Android unknown error 961 while downloading app
- Android AlertDialog with Yes No and Cancel Button
- Share or Send SMS via Android Intent
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- Pass data between two Android Activities and access it using Intent
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- Android EditText Cursor Colour appears to be white
- Android Development - How to switch between two Activities
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Android : Remove ListView Separator/divider programmatically or using xml property
- Android is starting optimizing... app 1 of 1
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- AlertDialog with single button example : Android
- Android : Exception raised during rendering: action_bar API 22
- Maven : java.lang.ClassNotFoundException: Xmx512m
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Center align text in TextView Android Programming
- How to Download and Install Android adb Tool on Linux, Mac or Windows
- Multiline EditText in Android Example
- How to remove Extra Spaces in Notepad++ - NotepadPlusPlus
- Two Ways to Extract rar (*.rar) files on Mac - MacOS
- Java Multi-line String Example - Java
- Java 8 Leap year check using Year class from java.time api - Java
- Microsoft Lists and SharePoint Online edit grid view - use undo and redo changes - SharePoint
- How to get SharePoint List Item URL using PowerShell - SharePoint
- Docker Commit Command with Examples - Docker
- How to get Java Thread name Example [Program] - Java