align AlertDialog buttons to center align AlertDialog buttons to center android android

align AlertDialog buttons to center


Use crtn's method, but instead of changing the LayoutParam's gravity, change its width to ViewGroup.LayoutParams.MATCH_PARENT;


This worked for me :

    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle);    builder.setCancelable(true);    builder.setTitle(title);    builder.setMessage(message);    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {        }    });    final AlertDialog dialog = builder.create();    dialog.show(); //show() should be called before dialog.getButton().    final Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);    LinearLayout.LayoutParams positiveButtonLL = (LinearLayout.LayoutParams) positiveButton.getLayoutParams();    positiveButtonLL.gravity = Gravity.CENTER;    positiveButton.setLayoutParams(positiveButtonLL);


If you want to have Positive And Negative Buttons at the same time (Large & Center), you can use something like this:

Dialog Positive & Negative Buttons

AlertDialog alertDialog = new AlertDialog.Builder(this).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.setButton(AlertDialog.BUTTON_NEGATIVE, "No",        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();layoutParams.weight = 10;btnPositive.setLayoutParams(layoutParams);btnNegative.setLayoutParams(layoutParams);