Set a listener to be invoked when the negative button of the dialog is pressed.
Parameters:
text - The text to display in the negative 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 negative button. This will set the button text to No and display an optional toast when the No button is pressed. The AlertDialog will close automatically when the Negative button is pressed.
@Override
public void onClick(DialogInterface dialog, int which) {
// To test, Add a Toast, with a context of getBaseContext() and message such as
// "No? Any way I can get you to change your mind?"
}
});
4. 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 Neutral Button Listener
Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setNegativeButton(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