ChipGroup

public class ChipGroup
extends ViewGroup

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ com.google.android.material.chip.ChipGroup


A ChipGroup is used to hold multiple Chips. By default, the chips are reflowed across multiple lines. Set the app:singleLine attribute to constrain the chips to a single horizontal line. If you do so, you'll usually want to wrap this ChipGroup in a HorizontalScrollView.

ChipGroup also supports a multiple-exclusion scope for a set of chips. When you set the app:singleSelection attribute, checking one chip that belongs to a chip group unchecks any previously checked chip within the same group. The behavior mirrors that of RadioGroup.

When a chip is added to a chip group, its checked state will be preserved. If the chip group is in the single selection mode and there is an existing checked chip when another checked chip is added, the existing checked chip will be unchecked to maintain the single selection rule.

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

Summary

Nested classes

class ChipGroup.LayoutParams

A ChipGroup.LayoutParams implementation for ChipGroup

interface ChipGroup.OnCheckedChangeListener

This interface is deprecated. Use ChipGroup.OnCheckedStateChangeListener instead.  

interface ChipGroup.OnCheckedStateChangeListener

Interface definition for a callback which supports multiple checked IDs to be invoked when the checked chips changed in this group. 

XML attributes

ChipGroup_checkedChip  
ChipGroup_selectionRequired  

Inherited constants

Inherited fields

Public constructors

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

Public methods

void check(int id)

Sets the selection to the chip whose identifier is passed in parameter.

void clearCheck()

Clears the selection.

ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
int getCheckedChipId()

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

List<Integer> getCheckedChipIds()

Returns the identifiers of the selected Chips in this group.

int getChipSpacingHorizontal()

Returns the horizontal spacing between visible chips in this group.

int getChipSpacingVertical()

Returns the vertical spacing between visible chips in this group.

int getRowIndex(View child)

Gets the row index of the child, primarily for accessibility.

boolean isSelectionRequired()

Returns whether we prevent all child chips from being deselected.

boolean isSingleLine()

Returns whether this chip group is single line or reflowed multiline.

boolean isSingleSelection()

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

void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
void setChipSpacing(int chipSpacing)

Sets the horizontal and vertical spacing between visible chips in this group.

void setChipSpacingHorizontal(int chipSpacingHorizontal)

Sets the horizontal spacing between visible chips in this group.

void setChipSpacingHorizontalResource(int id)

Sets the horizontal spacing between visible chips in this group.

void setChipSpacingResource(int id)

Sets the horizontal and vertical spacing between visible chips in this group.

void setChipSpacingVertical(int chipSpacingVertical)

Sets the vertical spacing between visible chips in this group.

void setChipSpacingVerticalResource(int id)

Sets the vertical spacing between visible chips in this group.

void setDividerDrawableHorizontal(Drawable divider)

This method is deprecated. Use setChipSpacingHorizontal(int) instead.

void setDividerDrawableVertical(Drawable divider)

This method is deprecated. Use setChipSpacingVertical(int) instead.

void setFlexWrap(int flexWrap)

This method is deprecated. Use setSingleLine(int) instead.

void setOnCheckedChangeListener(ChipGroup.OnCheckedChangeListener listener)

This method is deprecated. use setOnCheckedStateChangeListener(OnCheckedStateChangeListener) instead.

void setOnCheckedStateChangeListener(ChipGroup.OnCheckedStateChangeListener listener)

Register a callback to be invoked when the checked chip changes in this group.

void setOnHierarchyChangeListener(ViewGroup.OnHierarchyChangeListener listener)
void setSelectionRequired(boolean selectionRequired)

Sets whether we prevent all child chips from being deselected.

void setShowDividerHorizontal(int dividerMode)

This method is deprecated. Use setChipSpacingHorizontal(int) instead.

void setShowDividerVertical(int dividerMode)

This method is deprecated. Use setChipSpacingVertical(int) instead.

void setSingleLine(boolean singleLine)

Sets whether this chip group is single line, or reflowed multiline.

void setSingleLine(int id)

Sets whether this chip group is single line, or reflowed multiline.

void setSingleSelection(boolean singleSelection)

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

void setSingleSelection(int id)

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

Protected methods

boolean checkLayoutParams(ViewGroup.LayoutParams p)
ViewGroup.LayoutParams generateDefaultLayoutParams()
ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp)
int getItemSpacing()
int getLineSpacing()
int getRowCount()
void onFinishInflate()
void onLayout(boolean sizeChanged, int left, int top, int right, int bottom)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void setItemSpacing(int itemSpacing)
void setLineSpacing(int lineSpacing)

Inherited methods

XML attributes

ChipGroup_checkedChip

Related methods:

ChipGroup_selectionRequired

Related methods:

Public constructors

ChipGroup

public ChipGroup (Context context)

Parameters
context Context

ChipGroup

public ChipGroup (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

ChipGroup

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

check

public void check (int id)

Sets the selection to the chip whose identifier is passed in parameter.

In single selection mode, checking a chip also unchecks all others.

Parameters
id int: the unique id of the chip to select in this group

clearCheck

public void clearCheck ()

Clears the selection. When the selection is cleared, no chip in this group is selected and getCheckedChipId() returns View.NO_ID.

generateLayoutParams

public ViewGroup.LayoutParams generateLayoutParams (AttributeSet attrs)

Parameters
attrs AttributeSet

Returns
ViewGroup.LayoutParams

getCheckedChipId

public int getCheckedChipId ()

When in single selection mode, returns the identifier of the selected chip 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 chip in this group in single selection mode

getCheckedChipIds

public List<Integer> getCheckedChipIds ()

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

Returns
List<Integer> The unique IDs of the selected Chips in this group. When in single selection mode, returns a list with a single ID. When no Chips are selected, returns an empty list.

getChipSpacingHorizontal

public int getChipSpacingHorizontal ()

Returns the horizontal spacing between visible chips in this group.

Returns
int

getChipSpacingVertical

public int getChipSpacingVertical ()

Returns the vertical spacing between visible chips in this group.

Returns
int

getRowIndex

public int getRowIndex (View child)

Gets the row index of the child, primarily for accessibility.

Parameters
child View

Returns
int

isSelectionRequired

public boolean isSelectionRequired ()

Returns whether we prevent all child chips from being deselected.

Related XML Attributes:

Returns
boolean

isSingleLine

public boolean isSingleLine ()

Returns whether this chip group is single line or reflowed multiline.

Returns
boolean

isSingleSelection

public boolean isSingleSelection ()

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

Returns
boolean

onInitializeAccessibilityNodeInfo

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Parameters
info AccessibilityNodeInfo

setChipSpacing

public void setChipSpacing (int chipSpacing)

Sets the horizontal and vertical spacing between visible chips in this group.

Parameters
chipSpacing int

setChipSpacingHorizontal

public void setChipSpacingHorizontal (int chipSpacingHorizontal)

Sets the horizontal spacing between visible chips in this group.

Parameters
chipSpacingHorizontal int

setChipSpacingHorizontalResource

public void setChipSpacingHorizontalResource (int id)

Sets the horizontal spacing between visible chips in this group.

Parameters
id int

setChipSpacingResource

public void setChipSpacingResource (int id)

Sets the horizontal and vertical spacing between visible chips in this group.

Parameters
id int

setChipSpacingVertical

public void setChipSpacingVertical (int chipSpacingVertical)

Sets the vertical spacing between visible chips in this group.

Parameters
chipSpacingVertical int

setChipSpacingVerticalResource

public void setChipSpacingVerticalResource (int id)

Sets the vertical spacing between visible chips in this group.

Parameters
id int

setDividerDrawableHorizontal

public void setDividerDrawableHorizontal (Drawable divider)

This method is deprecated.
Use setChipSpacingHorizontal(int) instead.

Parameters
divider Drawable

setDividerDrawableVertical

public void setDividerDrawableVertical (Drawable divider)

This method is deprecated.
Use setChipSpacingVertical(int) instead.

Parameters
divider Drawable

setFlexWrap

public void setFlexWrap (int flexWrap)

This method is deprecated.
Use setSingleLine(int) instead.

Parameters
flexWrap int

setOnCheckedChangeListener

public void setOnCheckedChangeListener (ChipGroup.OnCheckedChangeListener listener)

This method is deprecated.
use setOnCheckedStateChangeListener(OnCheckedStateChangeListener) instead.

Register a callback to be invoked when the checked chip changes in this group. This callback is only invoked in single selection mode.

Parameters
listener ChipGroup.OnCheckedChangeListener: the callback to call on checked state change

setOnCheckedStateChangeListener

public void setOnCheckedStateChangeListener (ChipGroup.OnCheckedStateChangeListener listener)

Register a callback to be invoked when the checked chip changes in this group. This callback is only invoked in single selection mode.

Parameters
listener ChipGroup.OnCheckedStateChangeListener: the callback to call on checked state change

setOnHierarchyChangeListener

public void setOnHierarchyChangeListener (ViewGroup.OnHierarchyChangeListener listener)

Parameters
listener ViewGroup.OnHierarchyChangeListener

setSelectionRequired

public void setSelectionRequired (boolean selectionRequired)

Sets whether we prevent all child chips from being deselected.

Related XML Attributes:

Parameters
selectionRequired boolean

setShowDividerHorizontal

public void setShowDividerHorizontal (int dividerMode)

This method is deprecated.
Use setChipSpacingHorizontal(int) instead.

Parameters
dividerMode int

setShowDividerVertical

public void setShowDividerVertical (int dividerMode)

This method is deprecated.
Use setChipSpacingVertical(int) instead.

Parameters
dividerMode int

setSingleLine

public void setSingleLine (boolean singleLine)

Sets whether this chip group is single line, or reflowed multiline.

Parameters
singleLine boolean

setSingleLine

public void setSingleLine (int id)

Sets whether this chip group is single line, or reflowed multiline.

Parameters
id int

setSingleSelection

public void setSingleSelection (boolean singleSelection)

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

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

Parameters
singleSelection boolean

setSingleSelection

public void setSingleSelection (int id)

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

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

Parameters
id int

Protected methods

checkLayoutParams

protected boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
boolean

generateDefaultLayoutParams

protected ViewGroup.LayoutParams generateDefaultLayoutParams ()

Returns
ViewGroup.LayoutParams

generateLayoutParams

protected ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams lp)

Parameters
lp ViewGroup.LayoutParams

Returns
ViewGroup.LayoutParams

getItemSpacing

protected int getItemSpacing ()

Returns
int

getLineSpacing

protected int getLineSpacing ()

Returns
int

getRowCount

protected int getRowCount ()

Returns
int

onFinishInflate

protected void onFinishInflate ()

onLayout

protected void onLayout (boolean sizeChanged, 
                int left, 
                int top, 
                int right, 
                int bottom)

Parameters
sizeChanged boolean

left int

top int

right int

bottom int

onMeasure

protected void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

setItemSpacing

protected void setItemSpacing (int itemSpacing)

Parameters
itemSpacing int

setLineSpacing

protected void setLineSpacing (int lineSpacing)

Parameters
lineSpacing int