How to Set an AlertDialog.Builder Neutral Button Listener

public AlertDialog.Builder setNeutralButton (CharSequence text,DialogInterface.OnClickListener listener) was added in API level 1
Set a listener to be invoked when the neutral button of the dialog is pressed.
Parameters:
text - The text to display in the neutral 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 neutral button. This will set the button text to Neutral and display an optional toast when the Neutral button is pressed. The AlertDialog will close automatically when the Neutral button is pressed.

myAlertDialogBuilder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
    @Override

public void onClick(DialogInterface dialog, int which) {
// To test, Add a Toast, with a context of getBaseContext() and message such as
        // "No opinion huh...."

     }
});

4. Compile and run!

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 Add an Icon/Drawable to an AlertDialog.Builder

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setNeutralButton(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