Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stack AlertDialog buttons vertically?

Tags:

java

android

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?

like image 289
Stiliyan Avatar asked Mar 08 '16 23:03

Stiliyan


People also ask

How many buttons can be set up on an AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

How many maximum buttons are supported in an AlertDialog?

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.

What are the three buttons that can be added to a dialog?

There are three functions for adding Buttons to Android Dialog, setPositiveButton(int textId, DialogInterface.


3 Answers

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>
like image 148
rafaelc Avatar answered Oct 16 '22 05:10

rafaelc


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.

like image 24
sgtcoder Avatar answered Oct 16 '22 06:10

sgtcoder


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, _ -> {} })
like image 6
Shubham Agarwal Avatar answered Oct 16 '22 07:10

Shubham Agarwal