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.
AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.
setIcon() method is use to set the icon on Alert dialog box.
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 .
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.
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):
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With