Reputation: 6725
I know that pre-ICS, the standard order in an AlertDialog is "Ok" / "Cancel" and that has changed in ICS (the standard order became "Cancel" / "Ok")
However, even if I use
alert.setButton(AlertDialog.BUTTON_POSITIVE, "Ok", ...)
alert.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", ...)
I get the same result running the application in pre-ICS and in ICS: in both the positive option appears at left, negative at right.
Shouldn't this be automatic, since we are using System constants to define where positive (AlertDialog.BUTTON_POSITIVE) and negative (AlertDialog.BUTTON_NEGATIVE) are?
EDIT: After accepting the solution, I would like to refer that I am now using the following code to set the buttons in the correct order in ICS and pre-ICS:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { // pre-ICS, show Positive/Negative
alertDialog.setButton(labelPositive, positiveListener);
alertDialog.setButton2(labelNegative, negativeListener);
} else { // ICS+, show Negative/Positive
alertDialog.setButton(labelNegative, negativeListener);
alertDialog.setButton2(labelPositive, positiveListener);
}
Upvotes: 3
Views: 5351
Reputation: 86948
I'm not sure what you expect as an answer... The constant values are the same, Android simply switched the wording.
You probably know that BUTTON_POSITIVE
has no mandate that it link to an "affirmative" action. So BUTTON_POSITIVE
can have the "Cancel" code as readily as the "Ok" code, in fact they are just constants (positive: -1, neutral: -2, negative: -3).
Imagine a pre-ICS Dialog with the positive (left) and negative (right) buttons respectively labeled <-
and ->
. Watch what happens if ICS did automatically switch the values of the existing constants:
Pre-ICS Dialog | Post-ICS Dialog
--------------------------------|-------------------------------
| <- | | -> | | | -> | | <- |
ie. "Ok" "Cancel" | "Cancel" "Ok"
The buttons no longer make sense...
In short, ICS would've broken existing code in one generation by automatically reordering the buttons. So if the idea that the order of the default labels is not consistent in pre- and post-ICS devices truly bothers you, then consider using their constant values (-1
, -2
, -3
) rather than their aliases.
Upvotes: 2