Set a listener to be invoked when the positive button of the dialog is pressed.
Parameters:
text - The text to display in the positive button
listener - The DialogInterface.OnClickListener to use.
Returns: This Builder object to allow for chaining of calls to set methods
1. Create an AlertDialog with Supplied Arguments
2. Add the line below to the imports section.
import android.content.DialogInterface;
3. After your AlertDialog.Builder is instantiated in step 1, add the following code to set up the positive button. This will set the button text to Yes and display an optional toast when the Yes button is pressed. The AlertDialog will close automatically when the Positive button is clicked.
@Override
public void onClick(DialogInterface dialog, int which) {
// To test, Add a Toast, with a context of getBaseContext() and message such as
// "I can't believe you pressed me! You better sleep with one eye open tonight. Just sayin'."
}
});
5. The button order is defined by the OS itself. An OS change in button display order was made from API Level 10 to API Level 11.
In API Level 11+ the buttons will display in the order NEGATIVE|NEUTRAL|POSITIVE
In API Level 10 and below, the buttons will display int he order: POSITIVE|NEUTRAL|POSITIVE
API Level 10 button order |
Next Recommended Article: How to Set an AlertDialog.Builder Negative Button Listener
Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setPositiveButton(java.lang.CharSequence, android.content.DialogInterface.OnClickListener)
http://developer.android.com/reference/android/content/DialogInterface.OnClickListener.html
https://code.google.com/p/android/issues/detail?id=24138
http://stackoverflow.com/questions/20117732/dialog-box-positive-negative-buttons-in-different-position-depending-on-androi