I use this codes for Android (Java) programming:
public static MessageBoxResult showOk(
Context context, String title, String message, String okMessa
Ended up with this extension for Kotlin:
fun AlertDialog.withCenteredButtons() {
val positive = getButton(AlertDialog.BUTTON_POSITIVE)
val negative = getButton(AlertDialog.BUTTON_NEGATIVE)
//Disable the material spacer view in case there is one
val parent = positive.parent as? LinearLayout
parent?.gravity = Gravity.CENTER_HORIZONTAL
val leftSpacer = parent?.getChildAt(1)
leftSpacer?.visibility = View.GONE
//Force the default buttons to center
val layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
layoutParams.weight = 1f
layoutParams.gravity = Gravity.CENTER
positive.layoutParams = layoutParams
negative.layoutParams = layoutParams
}
And use with:
.show().withCenteredButtons()
If you want to have Positive And Negative Buttons at the same time (Large & Center), you can use something like this:
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);
I presume you are using the AlertDialog from the Support library.
If that's the case try replacing your import to android.app.AlertDialog.
Tried crtn's method and Scott Brown's modification, both didn't render how I liked.
crtn's solution didn't change the appearance of the buttons for me at all (I'm using android.R.style.Theme_Material_Light_Dialog
) and Scott Brown's solution made my positive button extend past the edge of the dialog parent.
For Theme_Material_Light_Dialog
the buttons are contained within a LinearLayout
subclass that uses a blank View as its 2nd (index 1) element to push the buttons right.
I grab the Button
ref like crtn does:
AlertDialog dialog = bld.create();
dialog.show(); //show() MUST be called before dialog.getButton
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
But then I set the leftSpacer to View.GONE and the parent's gravity to CENTER_HORIZONTAL
LinearLayout parent = (LinearLayout) positiveButton.getParent();
parent.setGravity(Gravity.CENTER_HORIZONTAL);
View leftSpacer = parent.getChildAt(1);
leftSpacer.setVisibility(View.GONE);
This has the advantage that it doesn't break the dialog's button stacking behavior. The disadvantage is that if the internal layout changes, it will break, so YMMV.
Useandroid.support.v7.app.AlertDialog
which will align your positive and negative buttons in center.
android.app.AlertDialog
will place the button in the top leaving 16dp space in bottom.
Here is something really work.
The parent of the 3 buttons (neutral, positive ve and negative) is ButtonBarLayout, which extends LinearLayout. To centralize a view in LinearLayout, weight, width and layout_gravity(but not gravity) is important, and these code works perfectly:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); //create a new one
layoutParams.weight = 1.0 f;
layoutParams.gravity = Gravity.CENTER; //this is layout_gravity
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setLayoutParams(layoutParams);