How can I set the order of the positive and negative buttons in AlertDialog?

You can extend the AlertDialog.Builder to force the order:

public class MyDialog extends AlertDialog.Builder {

    public MyDialog(Context arg0) {
        super(arg0);
    }

    @Override
    public Builder setPositiveButton(CharSequence text, OnClickListener listener) {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return super.setNegativeButton(text, listener);
        } else {
            return super.setPositiveButton(text, listener);
        }

    }

    @Override
    public Builder setNegativeButton(CharSequence text, OnClickListener listener) {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return super.setPositiveButton(text, listener);
        } else {
            return super.setNegativeButton(text, listener);
        }
    }

}

I've created a solution to force order of buttons for any Android version on any device.

The idea is that we can get list of buttons of dialog in order they appear on screen and set text and actions in order we want. It guarantees that we would have same order on any device.

Please check my GitHub repository

Here is example of creating and showing dialog:

 new FixedOrderDialogFragment.Builder()
    .setLeftButtonText(R.string.left)
    .setCenterButtonText(R.string.center)
    .setRightButtonText(R.string.right)
    .setDefaultButton(FixedOrderDialogFragment.BUTTON_RIGHT)
    .setMessage(R.string.message)
    .setTitle(R.string.app_name)
    .create()
 .show(getSupportFragmentManager(), "DEMO");

Please note that owner Activity should implement FixedOrderDialogFragment.FixedOrderDialogListener to be able restore dialog state on Activity re-creation.


Unfortunately, I don't believe you can. However, to quote the documentation:

Note: You can only add one of each button type to the AlertDialog. That is, you cannot have more than one "positive" button. This limits the number of possible buttons to three: positive, neutral, and negative. These names are technically irrelevant to the actual functionality of your buttons, but should help you keep track of which one does what.

So you can turn the different buttons into whatever you want. What you're seeing here is the order having switched (ordering from this answer):

  • On devices prior to ICS, the button order (left to right) was POSITIVE - NEUTRAL - NEGATIVE.
  • On newer devices using ICS, the button order (left to right) is now NEGATIVE - NEUTRAL - POSITIVE.

You might try checking the Build.VERSION and using that to decide which button is which at runtime.


this is my solution. It is work for me.

    // Show alertDialog after building
    AlertDialog alertDialog = createAlertDialog(context);
    alertDialog.show();
    // and find positiveButton and negativeButton
    Button positiveButton = (Button) alertDialog.findViewById(android.R.id.button1);
    Button negativeButton = (Button) alertDialog.findViewById(android.R.id.button2);
    // then get their parent ViewGroup
    ViewGroup buttonPanelContainer = (ViewGroup) positiveButton.getParent();
    int positiveButtonIndex = buttonPanelContainer.indexOfChild(positiveButton);
    int negativeButtonIndex = buttonPanelContainer.indexOfChild(negativeButton);
    if (positiveButtonIndex < negativeButtonIndex) {
        // prepare exchange their index in ViewGroup
        buttonPanelContainer.removeView(positiveButton);
        buttonPanelContainer.removeView(negativeButton);
        buttonPanelContainer.addView(negativeButton, positiveButtonIndex);
        buttonPanelContainer.addView(positiveButton, negativeButtonIndex);
    }