MaterialButtonToggleGroup

public class MaterialButtonToggleGroup extends MaterialButtonGroup


A common container for a set of related, toggleable MaterialButtons. The s 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.MaterialButtonToggleGroup
    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
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_private"/>
    <com.google.android.material.button.MaterialButton
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_team"/>
    <com.google.android.material.button.MaterialButton
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_everyone"/>
    <com.google.android.material.button.MaterialButton
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_custom"/>

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

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

Note: Styling must applied to each child button individually. It is recommended to use the materialButtonOutlinedStyle attribute for all child buttons. materialButtonOutlinedStyle will most closely match the Material Design guidelines for this component, and supports the checked state for child buttons.

Any MaterialButtons added to this view group are automatically marked as checkable, and by default multiple buttons within the same group can be checked. To enforce that only one button can be checked at a time, set the app:singleSelection attribute to true on the MaterialButtonToggleGroup or call setSingleSelection(true).

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

In order to cohesively group multiple buttons together, MaterialButtonToggleGroup overrides the start and end margins of any children added to this layout such that child buttons are placed directly adjacent to one another.

MaterialButtonToggleGroup also overrides any shapeAppearance, shapeAppearanceOverlay, or cornerRadius attribute set on MaterialButton children such that only the left-most corners of the first child and the right-most corners of the last child retain their shape appearance or corner size.

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

Summary

Nested types

Interface definition for a callback to be invoked when a MaterialButton is checked or unchecked in this group.

Public constructors

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

Public methods

void

Add a listener that will be invoked when the check state of a MaterialButton in this group changes.

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

This override prohibits Views other than MaterialButton to be added.

void
check(int id)

Sets the MaterialButton whose id is passed in to the checked state.

void

Clears the selections.

void

Remove all previously added OnButtonCheckedListeners.

int

When in single selection mode, returns the identifier of the selected button in this group.

List<Integer>

Returns the identifiers of the selected MaterialButtons in this group.

boolean

Returns whether we prevent all child buttons from being deselected.

boolean

Returns whether this group only allows a single button to be checked.

void
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
void

Remove a listener that was previously added via addOnButtonCheckedListener.

void
setSelectionRequired(boolean selectionRequired)

Sets whether we prevent all child buttons from being deselected.

void

Sets whether this group only allows a single button to be checked.

void
setSingleSelection(boolean singleSelection)

Sets whether this group only allows a single button to be checked.

void
uncheck(int id)

Sets the MaterialButton whose id is passed in to the unchecked state.

Protected methods

void

Inherited methods

From com.google.android.material.button.MaterialButtonGroup
void
dispatchDraw(Canvas canvas)
int
getChildDrawingOrder(int childCount, int i)

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

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
onLayout(boolean changed, int l, int t, int r, int b)
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
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.

Public constructors

MaterialButtonToggleGroup

public MaterialButtonToggleGroup(Context context)

MaterialButtonToggleGroup

public MaterialButtonToggleGroup(Context context, AttributeSet attrs)

MaterialButtonToggleGroup

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

Public methods

addOnButtonCheckedListener

public void addOnButtonCheckedListener(
    MaterialButtonToggleGroup.OnButtonCheckedListener listener
)

Add a listener that will be invoked when the check state of a MaterialButton in this group changes. See OnButtonCheckedListener.

Components that add a listener should take care to remove it when finished via removeOnButtonCheckedListener.

Parameters
MaterialButtonToggleGroup.OnButtonCheckedListener listener

listener to add

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.

check

public void check(int id)

Sets the MaterialButton whose id is passed in to the checked state. If this MaterialButtonToggleGroup is in single selection mode, then all other MaterialButtons in this group will be unchecked. Otherwise, other MaterialButtons will retain their checked state.

Parameters
int id

View ID of MaterialButton to set checked

clearChecked

public void clearChecked()

Clears the selections. When the selections are cleared, no MaterialButton in this group is checked and getCheckedButtonIds returns an empty list.

clearOnButtonCheckedListeners

public void clearOnButtonCheckedListeners()

Remove all previously added OnButtonCheckedListeners.

getCheckedButtonId

public int getCheckedButtonId()

When in single selection mode, returns the identifier of the selected button in this group. Upon empty selection, the returned value is NO_ID. If not in single selection mode, the return value is NO_ID.

ref R.styleable#MaterialButtonToggleGroup_checkedButton

Returns
int

The unique id of the selected MaterialButton in this group in single selection mode. When not in single selection mode, returns NO_ID.

getCheckedButtonIds

public List<IntegergetCheckedButtonIds()

Returns the identifiers of the selected MaterialButtons in this group. Upon empty selection, the returned value is an empty list.

Returns
List<Integer>

The unique IDs of the selected MaterialButtons in this group. When in single selection mode, returns a list with a single ID. When no MaterialButtons are selected, returns an empty list.

isSelectionRequired

public boolean isSelectionRequired()

Returns whether we prevent all child buttons from being deselected.

ref R.styleable#MaterialButtonToggleGroup_selectionRequired

isSingleSelection

public boolean isSingleSelection()

Returns whether this group only allows a single button to be checked.

ref R.styleable#MaterialButtonToggleGroup_singleSelection

Returns
boolean

whether this group only allows a single button to be checked

onInitializeAccessibilityNodeInfo

public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)

removeOnButtonCheckedListener

public void removeOnButtonCheckedListener(
    MaterialButtonToggleGroup.OnButtonCheckedListener listener
)

Remove a listener that was previously added via addOnButtonCheckedListener.

Parameters
MaterialButtonToggleGroup.OnButtonCheckedListener listener

listener to remove

setSelectionRequired

public void setSelectionRequired(boolean selectionRequired)

Sets whether we prevent all child buttons from being deselected.

ref R.styleable#MaterialButtonToggleGroup_selectionRequired

setSingleSelection

public void setSingleSelection(int id)

Sets whether this group only allows a single button to be checked.

Calling this method results in all the buttons in this group to become unchecked.

ref R.styleable#MaterialButtonToggleGroup_singleSelection

Parameters
int id

boolean resource ID of whether this group only allows a single button to be checked

setSingleSelection

public void setSingleSelection(boolean singleSelection)

Sets whether this group only allows a single button to be checked.

Calling this method results in all the buttons in this group to become unchecked.

ref R.styleable#MaterialButtonToggleGroup_singleSelection

Parameters
boolean singleSelection

whether this group only allows a single button to be checked

uncheck

public void uncheck(int id)

Sets the MaterialButton whose id is passed in to the unchecked state.

Parameters
int id

View ID of MaterialButton to set unchecked

Protected methods

onFinishInflate

protected void onFinishInflate()