MaterialButtonGroup

public class MaterialButtonGroup

Known direct subclasses
MaterialButtonToggleGroup

A common container for a set of related, toggleable MaterialButtons.

MaterialSplitButton

A container for two MaterialButtons that together create a split button.


A common container for a set of related MaterialButtons. The MaterialButtons in this group will be shown on a single line.

This layout currently only supports child views of type MaterialButton. Buttons can be added to this view group via XML, as follows:

<com.google.android.material.button.MaterialButtonGroup
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toggle_button_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_private"/>
    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_team"/>
    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_everyone"/>
    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_custom"/>

</com.google.android.material.button.MaterialButtonGroup>

Buttons can also be added to this view group programmatically via the addView methods.

MaterialButtonGroup is a LinearLayout. Using android:layout_width="MATCH_PARENT" and removing android:insetBottom android:insetTop on the children is recommended if using VERTICAL.

For more information, see the component developer guidance and design guidelines.

Summary

Public fields

int

Public constructors

MaterialButtonGroup(Context context)
MaterialButtonGroup(Context context, AttributeSet attrs)
MaterialButtonGroup(
    Context context,
    AttributeSet attrs,
    int defStyleAttr
)

Public methods

void
addView(View child, int index, LayoutParams params)

This override prohibits Views other than MaterialButton to be added.

CornerSize

Returns the inner corner size of the group.

ShapeAppearanceModel

Returns the ShapeAppearanceModel of the group.

int

Returns the spacing (in pixels) between each button in the group.

void
onViewRemoved(View child)
void
setEnabled(boolean enabled)

Enables this MaterialButtonGroup and all its MaterialButton children

void

Sets the inner corner size of the group.

void
setOrientation(int orientation)
void

Sets the ShapeAppearanceModel of the group.

void
setSpacing(int spacing)

Sets the spacing between each button in the group.

Protected methods

void
dispatchDraw(Canvas canvas)
int
getChildDrawingOrder(int childCount, int i)

We keep track of which views are pressed and checked to draw them last.

void
onLayout(boolean changed, int l, int t, int r, int b)
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Public fields

spacing

public int spacing

Public constructors

MaterialButtonGroup

public MaterialButtonGroup(Context context)

MaterialButtonGroup

public MaterialButtonGroup(Context context, AttributeSet attrs)

MaterialButtonGroup

public MaterialButtonGroup(
    Context context,
    AttributeSet attrs,
    int defStyleAttr
)

Public methods

addView

public void addView(View child, int index, LayoutParams params)

This override prohibits Views other than MaterialButton to be added. It also makes updates to the add button shape and margins.

getInnerCornerSize

public CornerSize getInnerCornerSize()

Returns the inner corner size of the group.

getShapeAppearance

public ShapeAppearanceModel getShapeAppearance()

Returns the ShapeAppearanceModel of the group.

getSpacing

public int getSpacing()

Returns the spacing (in pixels) between each button in the group.

onViewRemoved

public void onViewRemoved(View child)

setEnabled

public void setEnabled(boolean enabled)

Enables this MaterialButtonGroup and all its MaterialButton children

Parameters
boolean enabled

boolean to setEnable MaterialButtonGroup

setInnerCornerSize

public void setInnerCornerSize(CornerSize cornerSize)

Sets the inner corner size of the group.

Can set as an AbsoluteCornerSize or RelativeCornerSize. Don't set relative corner size larger than 50% or absolute corner size larger than half height to avoid corner overlapping.

Parameters
CornerSize cornerSize

the inner corner size of the group

setOrientation

public void setOrientation(int orientation)

setShapeAppearance

public void setShapeAppearance(ShapeAppearanceModel shapeAppearance)

Sets the ShapeAppearanceModel of the group.

Parameters
ShapeAppearanceModel shapeAppearance

The new ShapeAppearanceModel of the group.

setSpacing

public void setSpacing(int spacing)

Sets the spacing between each button in the group.

Parameters
int spacing

the spacing (in pixels) between each button in the group

Protected methods

dispatchDraw

protected void dispatchDraw(Canvas canvas)

getChildDrawingOrder

protected int getChildDrawingOrder(int childCount, int i)

We keep track of which views are pressed and checked to draw them last. This prevents visual issues with overlapping strokes.

onLayout

protected void onLayout(boolean changed, int l, int t, int r, int b)

onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)