What is the ButtonBarLayout and how should we use it?

As others pointed out, the class description tells exactly what it is: an extension of LinearLayout that automatically switches to vertical orientation when it can't fit its child views horizontally.

I might add that this was clearly done to fit with the material design specifications about dialogs. They make a distinction between side by side buttons and stacked buttons. See for example:

Side-by-side buttons are recommended when the text of each label does not exceed the maximum button width, such as the commonly used OK/Cancel buttons.

enter image description here

While you should go for stacked buttons when the single button is too large, or there's not enough room for both:

When text labels exceed the maximum button width, use stacked buttons to accommodate the text. Affirmative actions are stacked above dismissive actions.

enter image description here

So, one possible use of this class, is when designing your own dialogs. For example, AlertDialog and AlertDialog.Builder offer internal support for dialogs with buttons, but sometimes you just want to subclass DialogFragment or AppCompatDialogFragment for a better control.

There, it might be useful to setup a bottom button bar that follows the design guidelines, and have full control on the buttons (like enabling and disabling, things you can't do with an AlertDialog AFAIK).


The source code describes ButtonBarLayout as follows:

/**
 * An extension of LinearLayout that automatically switches to vertical
 * orientation when it can't fit its child views horizontally.
 */

So, in essence, it is nothing but a smart LinearLayout which manages auto-switching orientations based on available space on screen.

The same ButtonBarLayout.java file describes mAllowStacking in comments as follows:

/** Whether the current configuration allows stacking. */

Source Code Here