The Android Developer Challenge is back! Submit your idea before December 2.

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 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, orientation VERTICAL is not supported yet.

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.

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_singleSelection  

Inherited constants

From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

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.

CharSequence getAccessibilityClassName()
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 isSingleSelection()

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

void onViewRemoved(View child)
void removeOnButtonCheckedListener(MaterialButtonToggleGroup.OnButtonCheckedListener listener)

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

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

From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML attributes

MaterialButtonToggleGroup_checkedButton

Related methods:

MaterialButtonToggleGroup_singleSelection

Related methods:

Public constructors

MaterialButtonToggleGroup

MaterialButtonToggleGroup (Context context)

Parameters
context Context

MaterialButtonToggleGroup

MaterialButtonToggleGroup (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

MaterialButtonToggleGroup

MaterialButtonToggleGroup (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

addOnButtonCheckedListener

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

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

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

void clearChecked ()

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

clearOnButtonCheckedListeners

void clearOnButtonCheckedListeners ()

Remove all previously added MaterialButtonToggleGroup.OnButtonCheckedListeners.

getAccessibilityClassName

CharSequence getAccessibilityClassName ()

Returns
CharSequence

getCheckedButtonId

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.

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

getCheckedButtonIds

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.

isSingleSelection

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

onViewRemoved

void onViewRemoved (View child)

Parameters
child View

removeOnButtonCheckedListener

void removeOnButtonCheckedListener (MaterialButtonToggleGroup.OnButtonCheckedListener listener)

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

Parameters
listener MaterialButtonToggleGroup.OnButtonCheckedListener: listener to remove

setSingleSelection

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

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

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

void dispatchDraw (Canvas canvas)

Parameters
canvas Canvas

getChildDrawingOrder

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

void onFinishInflate ()

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int