ChipGroup

public class 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 .

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 .

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 types

A ChipGroup.LayoutParams implementation for ChipGroup.

This interface is deprecated.

Use OnCheckedStateChangeListener instead.

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

Protected fields

int
int
int

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

Clears the selection.

LayoutParams
generateLayoutParams(AttributeSet attrs)
int

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

List<Integer>

Returns the identifiers of the selected Chips in this group.

int

Returns the horizontal spacing between visible chips in this group.

int

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

Returns whether we prevent all child chips from being deselected.

boolean

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

boolean

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

Sets the horizontal spacing between visible chips in this group.

void

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

Sets the vertical spacing between visible chips in this group.

void
setDividerDrawableHorizontal(Drawable divider)

This method is deprecated.

Use setChipSpacingHorizontal instead.

void
setDividerDrawableVertical(Drawable divider)

This method is deprecated.

Use setChipSpacingVertical instead.

void
setFlexWrap(int flexWrap)

This method is deprecated.

Use setSingleLine instead.

void

This method is deprecated.

use setOnCheckedStateChangeListener instead.

void

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

void
setOnHierarchyChangeListener(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 instead.

void
setShowDividerVertical(int dividerMode)

This method is deprecated.

Use setChipSpacingVertical instead.

void
setSingleLine(int id)

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

void
setSingleLine(boolean singleLine)

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

void

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

void
setSingleSelection(boolean singleSelection)

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

Protected methods

boolean
checkLayoutParams(LayoutParams p)
LayoutParams
LayoutParams
generateLayoutParams(LayoutParams lp)
int
int
int
void
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)

Public fields

chipSpacingHorizontal

public int chipSpacingHorizontal

chipSpacingVertical

public int chipSpacingVertical

Protected fields

itemSpacing

protected int itemSpacing

lineSpacing

protected int lineSpacing

rowCount

protected int rowCount

Public constructors

ChipGroup

public ChipGroup(Context context)

ChipGroup

public ChipGroup(Context context, AttributeSet attrs)

ChipGroup

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

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

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

generateLayoutParams

public LayoutParams generateLayoutParams(AttributeSet attrs)

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

ref R.styleable#ChipGroup_checkedChip

Returns
int

the unique id of the selected chip in this group in single selection mode

getCheckedChipIds

public List<IntegergetCheckedChipIds()

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.

getChipSpacingVertical

public int getChipSpacingVertical()

Returns the vertical spacing between visible chips in this group.

getRowIndex

public int getRowIndex(View child)

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

isSelectionRequired

public boolean isSelectionRequired()

Returns whether we prevent all child chips from being deselected.

ref R.styleable#ChipGroup_selectionRequired

isSingleLine

public boolean isSingleLine()

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

isSingleSelection

public boolean isSingleSelection()

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

onInitializeAccessibilityNodeInfo

public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)

setChipSpacing

public void setChipSpacing(int chipSpacing)

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

setChipSpacingHorizontal

public void setChipSpacingHorizontal(int chipSpacingHorizontal)

Sets the horizontal spacing between visible chips in this group.

setChipSpacingHorizontalResource

public void setChipSpacingHorizontalResource(int id)

Sets the horizontal spacing between visible chips in this group.

setChipSpacingResource

public void setChipSpacingResource(int id)

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

setChipSpacingVertical

public void setChipSpacingVertical(int chipSpacingVertical)

Sets the vertical spacing between visible chips in this group.

setChipSpacingVerticalResource

public void setChipSpacingVerticalResource(int id)

Sets the vertical spacing between visible chips in this group.

setDividerDrawableHorizontal

public void setDividerDrawableHorizontal(Drawable divider)

setDividerDrawableVertical

public void setDividerDrawableVertical(Drawable divider)

setFlexWrap

public void setFlexWrap(int flexWrap)

setOnCheckedChangeListener

public void setOnCheckedChangeListener(ChipGroup.OnCheckedChangeListener 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
ChipGroup.OnCheckedChangeListener listener

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
ChipGroup.OnCheckedStateChangeListener listener

the callback to call on checked state change

setOnHierarchyChangeListener

public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener)

setSelectionRequired

public void setSelectionRequired(boolean selectionRequired)

Sets whether we prevent all child chips from being deselected.

ref R.styleable#ChipGroup_selectionRequired

setShowDividerHorizontal

public void setShowDividerHorizontal(int dividerMode)

setShowDividerVertical

public void setShowDividerVertical(int dividerMode)

setSingleLine

public void setSingleLine(int id)

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

setSingleLine

public void setSingleLine(boolean singleLine)

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

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.

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.

Protected methods

checkLayoutParams

protected boolean checkLayoutParams(LayoutParams p)

generateDefaultLayoutParams

protected LayoutParams generateDefaultLayoutParams()

generateLayoutParams

protected LayoutParams generateLayoutParams(LayoutParams lp)

getItemSpacing

protected int getItemSpacing()

getLineSpacing

protected int getLineSpacing()

getRowCount

protected int getRowCount()

onFinishInflate

protected void onFinishInflate()

onLayout

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

onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

setItemSpacing

protected void setItemSpacing(int itemSpacing)

setLineSpacing

protected void setLineSpacing(int lineSpacing)