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

Micah Hainline picture Micah Hainline · Jul 12, 2012 · Viewed 23.1k times · Source

Why I want to do this is another discussion entirely, but I need to figure out the best way to make all my alert dialogs have the positive button on the right side. Note that in version 3.0 and below the buttons normally appear as OK / Cancel and in 4.0 and above it is Cancel / OK. I want to force my application to use Cancel / OK in the simplest way possible. I have a lot of AlertDialogs in the application.

Answer

thegrinner picture thegrinner · Jul 12, 2012

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.