Ad

How To Align Alert Dialog Buttons To The Left?

- 1 answer

I want to create an alert dialog with ltr buttons. but it seems like we can't access positive button details.

this answer was for aligning buttons to center, but I want to align it to the left.

I just checked stack and didn't find anything that covers this.i don't want to create custom_layout.xml for dialog.

Ad

Answer

This is the answer from the link you posted. You need to change the weight of the layout params from 10 to -10 to go from center to left.

 AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
 alertDialog.setTitle("Title");
 alertDialog.setMessage("Message");

 alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Yes",
             new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });

alertDialog.show();

Button btnPositive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button btnNegative = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);

LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) btnPositive.getLayoutParams();

//Changing the weight to negative pushes it to the left.
layoutParams.weight = -10;

btnPositive.setLayoutParams(layoutParams);
btnNegative.setLayoutParams(layoutParams);

Hope it helps!

More information: Now that I understand the question, no sadly you cannot change the order or the buttons. The order is NEGATIVE - NEUTRAL - POSITIVE. But once you have the button you can choose whatever to do with. For instance, you can use the negative button as the positive, by just renaming it in one line.

Ad
source: stackoverflow.com
Ad