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

Nested types

A LinearLayout.LayoutParams implementation for MaterialButtonGroup.

Constants

static final Object
static final int

A value for overflowMode.

static final int

A value for overflowMode.

static final int

A value for overflowMode.

Public fields

int
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.

MaterialButtonGroup.LayoutParams
generateLayoutParams(AttributeSet attrs)
ShapeAppearanceModel

Returns the original ShapeAppearanceModel for the MaterialButton child at the given index.

CornerSize

Returns the inner corner size of the group.

Drawable

Returns the icon shown for the overflow button, if present.

int

Returns the overflow mode.

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
setOverflowButtonIcon(Drawable icon)

Sets the icon to show for the overflow button.

void
setOverflowButtonIconResource(int iconResourceId)

Sets the icon to show for the overflow button.

void
setOverflowMode(int overflowMode)

Sets the overflow mode.

void

Sets the ShapeAppearanceModel of the group.

void
setSpacing(int spacing)

Sets the spacing between each button in the group.

Protected methods

boolean
checkLayoutParams(LayoutParams p)
void
dispatchDraw(Canvas canvas)
MaterialButtonGroup.LayoutParams
MaterialButtonGroup.LayoutParams
generateLayoutParams(LayoutParams p)
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)

Constants

OVERFLOW_BUTTON_TAG

public static final Object OVERFLOW_BUTTON_TAG

OVERFLOW_MODE_MENU

public static final int OVERFLOW_MODE_MENU = 1

A value for overflowMode. It indicates that the buttons that don't fit to the group size will be hidden in the group and contained in a popup menu.

OVERFLOW_MODE_NONE

public static final int OVERFLOW_MODE_NONE = 0

A value for overflowMode. It indicates that there's no handling to the buttons that don't fit to the group size.

OVERFLOW_MODE_WRAP

public static final int OVERFLOW_MODE_WRAP = 2

A value for overflowMode. It indicates that the buttons that don't fit to the group size will be displayed in another row under it.

Public fields

overflowMode

public int overflowMode

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.

generateLayoutParams

public MaterialButtonGroup.LayoutParams generateLayoutParams(AttributeSet attrs)

getChildOriginalShapeAppearanceModel

public ShapeAppearanceModel getChildOriginalShapeAppearanceModel(int index)

Returns the original ShapeAppearanceModel for the MaterialButton child at the given index.

getInnerCornerSize

public CornerSize getInnerCornerSize()

Returns the inner corner size of the group.

getOverflowButtonIcon

public Drawable getOverflowButtonIcon()

Returns the icon shown for the overflow button, if present.

ref com.google.android.material.R.styleable#MaterialButtonGroup_overflowButtonIcon

Returns
Drawable

the overflow button icon, if present.

getOverflowMode

public int getOverflowMode()

Returns the overflow mode.

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)

setOverflowButtonIcon

public void setOverflowButtonIcon(Drawable icon)

Sets the icon to show for the overflow button.

ref com.google.android.material.R.styleable#MaterialButtonGroup_overflowButtonIcon

Parameters
Drawable icon

Drawable to use for the overflow button's icon.

setOverflowButtonIconResource

public void setOverflowButtonIconResource(int iconResourceId)

Sets the icon to show for the overflow button.

ref com.google.android.material.R.styleable#MaterialButtonGroup_overflowButtonIcon

Parameters
int iconResourceId

drawable resource ID to use for the overflow button's icon.

setOverflowMode

public void setOverflowMode(int overflowMode)

Sets the overflow mode.

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

checkLayoutParams

protected boolean checkLayoutParams(LayoutParams p)

dispatchDraw

protected void dispatchDraw(Canvas canvas)

generateDefaultLayoutParams

protected MaterialButtonGroup.LayoutParams generateDefaultLayoutParams()

generateLayoutParams

protected MaterialButtonGroup.LayoutParams generateLayoutParams(LayoutParams p)

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)