Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 936
Micah Hainline Avatar asked Jul 12 '12 20:07

Micah Hainline


People also ask

How many buttons can be set up on an AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

Which method is used to set in AlertDialog?

setIcon() method is use to set the icon on Alert dialog box.

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

What are the three buttons that can be added to a dialog?

There are three functions for adding Buttons to Android Dialog, setPositiveButton(int textId, DialogInterface. OnClickListener listener) : This is Yes button, when clicked the code written in the OnClickListener onClick() method will be displayed.


2 Answers

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.

like image 58
thegrinner Avatar answered Nov 03 '22 00:11

thegrinner


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);     } 
like image 35
林奕忠 Avatar answered Nov 02 '22 23:11

林奕忠