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();
- Android Error Unexpected cast to Button: layout tag was FrameLayout
- ADT quit unexpectedly error on Mac OSX Android Eclipse SDK
- Parsing Data for android-21 failed unsupported major.minor version 51.0
- Android Studio Ctrl Shift o auto import not working
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
- Android : How to make TextView Scrollable
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Integrating Android Facebook SDK 3.17.2 Tutorial
- Android R Cannot Be Resolved To A Variable
- Android : Exception raised during rendering: action_bar API 22
- How to take screenshot on Android
- Read Text file from SD Card : Android Programming
- How to make Android EditText not editable
- Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later.
- The declared package does not match the expected package Eclipse
- Can't Run SDK Manager find_java.bat issue
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- Android Emulator Soft Back button action using Computer keyboard
- Multiline EditText in Android Example
- Use 5G Network on Android Emulator
- Make Android TextView Clickable like Buttons
- How to empty trash in Android Device
- Android : Execute some code after back button is pressed
- Disable Fading Edges Scroll Effect Android Views
- How To Disable Landscape Mode in Android Application
- [fix] RabbitMQ: AuthenticationFailureException ACCESS_REFUSED Login refused authentication mechanism PLAIN - 2022
- Install the minimal Linux on Docker (only 5 mb Alpine Linux) - Docker
- Add Sketch from iPhone to MacBook with macOS Monterey - MacOS
- Word count in Notepad++ - NotepadPlusPlus
- How to hide toolbar on Notepad++ - NotepadPlusPlus
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python] - Python
- Fix: Notepad++ bottom status bar not visible - NotepadPlusPlus
- [Solved] Mic not working on iPhone 7 after iOS 14 upgrade - Apple