I am using builder to create AlertDialogs
in Android using the pattern:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(...);
builder.setMessage(...);
builder.setPositiveButton(button1Text, ...);
builder.setNeutralButton(button2Text, ...);
builder.setNegativeButton(button3Text, ...);
builder.show();
Currently, only two of the buttons are displayed, because the buttons are too wide to fit in the dialog. How can I enforce the buttons to stack vertically?
I am using the Theme.AppCompat.Dialog.Alert
theme, which uses ButtonBarLayout
to build the buttons. According to this answer, ButtonBarLayout
can stack wide buttons vertically automatically, when its mAllowStacking
property is set, but it seems to default to false in my case. Is there a way I can set it to true when I build the AlertDialog
?
AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.
There should not be more than three action buttons in AlertDialog, and they are positive, negative, and neutral. Positive is used to accept and continue with the action.
There are three functions for adding Buttons to Android Dialog, setPositiveButton(int textId, DialogInterface.
You can't do that with an AlertDialog
. You should create a custom Dialog
, and implement that yourself. Something like this would do it
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle(...);
dialog.setMessage(...);
dialog.show();
and your layout dialog_layout.xml
should be something like
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
orientation="vertical">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
What if you did the alert box as a list?
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color)
.setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}
Example taken from here (under adding a list): https://developer.android.com/guide/topics/ui/dialogs.html
Then just take those list options and turn them into what you want.
I don't prefer hacks. But this strikes me immediately.
If the button text it too long to all fit horizontally, then it will automatically get laid out in a vertical column of three buttons.
Just make the button text long.
builder.setPositiveButton(" Yes", { dialog, _ -> {} })
builder.setNeutralButton(" May be", { dialog, _ -> {} })
builder.setNegativeButton(" No", { dialog, _ -> {} })
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