MaterialButtonToggleGroup

public class MaterialButtonToggleGroup
extends LinearLayout

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.LinearLayout
         ↳ com.google.android.material.button.MaterialButtonToggleGroup


A common container for a set of related, toggleable 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.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 ViewGroup.addView(View) 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 classes

interface MaterialButtonToggleGroup.OnButtonCheckedListener

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

XML attributes

MaterialButtonToggleGroup_checkedButton  
MaterialButtonToggleGroup_selectionRequired  
MaterialButtonToggleGroup_singleSelection  

Inherited constants

Inherited fields

Public constructors

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

Public methods

void addOnButtonCheckedListener(MaterialButtonToggleGroup.OnButtonCheckedListener listener)

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

void addView(View child, int index, ViewGroup.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 clearChecked()

Clears the selections.

void clearOnButtonCheckedListeners()

Remove all previously added MaterialButtonToggleGroup.OnButtonCheckedListeners.

int getCheckedButtonId()

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

List<Integer> getCheckedButtonIds()

Returns the identifiers of the selected MaterialButtons in this group.

boolean isSelectionRequired()

Returns whether we prevent all child buttons from being deselected.

boolean isSingleSelection()

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

void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
void onViewRemoved(View child)
void removeOnButtonCheckedListener(MaterialButtonToggleGroup.OnButtonCheckedListener listener)

Remove a listener that was previously added via addOnButtonCheckedListener(OnButtonCheckedListener).

void setEnabled(boolean enabled)

Enables this MaterialButtonToggleGroup and all its MaterialButton children

void setSelectionRequired(boolean selectionRequired)

Sets whether we prevent all child buttons from being deselected.

void setSingleSelection(boolean singleSelection)

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

void setSingleSelection(int id)

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 dispatchDraw(Canvas canvas)
int getChildDrawingOrder(int childCount, int i)

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

void onFinishInflate()
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Inherited methods

XML attributes

MaterialButtonToggleGroup_checkedButton

Related methods:

MaterialButtonToggleGroup_selectionRequired

Related methods:

MaterialButtonToggleGroup_singleSelection

Related methods:

Public constructors

MaterialButtonToggleGroup

public MaterialButtonToggleGroup (Context context)

Parameters
context Context

MaterialButtonToggleGroup

public MaterialButtonToggleGroup (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

MaterialButtonToggleGroup

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

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 MaterialButtonToggleGroup.OnButtonCheckedListener.

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

Parameters
listener MaterialButtonToggleGroup.OnButtonCheckedListener: listener to add

addView

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

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

Parameters
child View

index int

params ViewGroup.LayoutParams

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
id int: 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 MaterialButtonToggleGroup.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 View.NO_ID. If not in single selection mode, the return value is View.NO_ID.

Related XML Attributes:

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

getCheckedButtonIds

public List<Integer> getCheckedButtonIds ()

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.

Related XML Attributes:

Returns
boolean

isSingleSelection

public boolean isSingleSelection ()

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

Related XML Attributes:

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

onInitializeAccessibilityNodeInfo

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Parameters
info AccessibilityNodeInfo

onViewRemoved

public void onViewRemoved (View child)

Parameters
child View

removeOnButtonCheckedListener

public void removeOnButtonCheckedListener (MaterialButtonToggleGroup.OnButtonCheckedListener listener)

Remove a listener that was previously added via addOnButtonCheckedListener(OnButtonCheckedListener).

Parameters
listener MaterialButtonToggleGroup.OnButtonCheckedListener: listener to remove

setEnabled

public void setEnabled (boolean enabled)

Enables this MaterialButtonToggleGroup and all its MaterialButton children

Parameters
enabled boolean: boolean to setEnable MaterialButtonToggleGroup

setSelectionRequired

public void setSelectionRequired (boolean selectionRequired)

Sets whether we prevent all child buttons from being deselected.

Related XML Attributes:

Parameters
selectionRequired boolean

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.

Related XML Attributes:

Parameters
singleSelection boolean: whether this group only allows a single button to be checked

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.

Related XML Attributes:

Parameters
id int: boolean resource ID of 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
id int: View ID of MaterialButton to set unchecked

Protected methods

dispatchDraw

protected void dispatchDraw (Canvas canvas)

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

Parameters
childCount int

i int

Returns
int

onFinishInflate

protected void onFinishInflate ()

onMeasure

protected void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int