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 i
There is absolutely no need to switch the order of any buttons!
Apparently, it is not immediately obvious, but you can simply set the labels and the actions to ANY of the three handlers you have at your disposal, i.e. you are NOT required to have the OK button defined as a positiveButton.
For example, you can attach your Cancel action to the setNeutralButton()
button, and your "I feel lucky" action to the setNegativeButton()
.
Android has no semantics attached to negative/positive/neutral
so you might as well cancel your dialog in setPositiveButton()
and confirm the dialog using setNegativeButton()
. Just set the right labels and actions.
Example:
new AlertDialog.Builder(ctx)
.setTitle("Switching buttons the easy way")
.setPositiveButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(ctx, "I agree!", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("I feel lucky...", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(ctx, "Make a bet", Toast.LENGTH_SHORT).show();
}
})
.create()
.show();