Join us on the livestream at Android Dev Summit on 7-8 November 2018, starting at 10AM PDT!

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.

Summary

Nested classes

class ChipGroup.LayoutParams

{@link ChipGroup.LayoutParams for {@link ChipGroup}. 

interface ChipGroup.OnCheckedChangeListener

Interface definition for a callback to be invoked when the checked chip changed in this group. 

XML attributes

ChipGroup_checkedChip  

Inherited constants

From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

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

Public methods

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

int getChipSpacingHorizontal()

Returns the horizontal spacing between chips in this group.

int getChipSpacingVertical()

Returns the vertical spacing between chips in this group.

boolean isSingleSelection()

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

void setChipSpacing(int chipSpacing)

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

void setChipSpacingHorizontal(int chipSpacingHorizontal)

Sets the horizontal spacing between chips in this group.

void setChipSpacingHorizontalResource(int id)

Sets the horizontal spacing between chips in this group.

void setChipSpacingResource(int id)

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

void setChipSpacingVertical(int chipSpacingVertical)

Sets the vertical spacing between chips in this group.

void setChipSpacingVerticalResource(int id)

Sets the vertical spacing between chips in this group.

void setDividerDrawableHorizontal(Drawable divider)

Deprecated.

void setDividerDrawableVertical(Drawable divider)

Deprecated.

void setFlexWrap(int flexWrap)

Deprecated Use setSingleLine(int) instead.

void setOnCheckedChangeListener(ChipGroup.OnCheckedChangeListener listener)

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

void setOnHierarchyChangeListener(ViewGroup.OnHierarchyChangeListener listener)
void setShowDividerHorizontal(int dividerMode)

Deprecated.

void setShowDividerVertical(int dividerMode)

Deprecated.

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()
boolean isSingleLine()
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

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

ChipGroup_checkedChip

Related methods:

Public constructors

ChipGroup

ChipGroup (Context context)

Parameters
context Context

ChipGroup

ChipGroup (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

ChipGroup

ChipGroup (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

addView

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

Parameters
child View

index int

params ViewGroup.LayoutParams

check

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

void clearCheck ()

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

generateLayoutParams

ViewGroup.LayoutParams generateLayoutParams (AttributeSet attrs)

Parameters
attrs AttributeSet

Returns
ViewGroup.LayoutParams

getCheckedChipId

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.

Related XML Attributes:

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

getChipSpacingHorizontal

int getChipSpacingHorizontal ()

Returns the horizontal spacing between chips in this group.

Returns
int

getChipSpacingVertical

int getChipSpacingVertical ()

Returns the vertical spacing between chips in this group.

Returns
int

isSingleSelection

boolean isSingleSelection ()

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

Returns
boolean

setChipSpacing

void setChipSpacing (int chipSpacing)

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

Parameters
chipSpacing int

setChipSpacingHorizontal

void setChipSpacingHorizontal (int chipSpacingHorizontal)

Sets the horizontal spacing between chips in this group.

Parameters
chipSpacingHorizontal int

setChipSpacingHorizontalResource

void setChipSpacingHorizontalResource (int id)

Sets the horizontal spacing between chips in this group.

Parameters
id int

setChipSpacingResource

void setChipSpacingResource (int id)

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

Parameters
id int

setChipSpacingVertical

void setChipSpacingVertical (int chipSpacingVertical)

Sets the vertical spacing between chips in this group.

Parameters
chipSpacingVertical int

setChipSpacingVerticalResource

void setChipSpacingVerticalResource (int id)

Sets the vertical spacing between chips in this group.

Parameters
id int

setDividerDrawableHorizontal

void setDividerDrawableHorizontal (Drawable divider)

Deprecated. Use setChipSpacingHorizontal(int) instead.

Parameters
divider Drawable

setDividerDrawableVertical

void setDividerDrawableVertical (Drawable divider)

Deprecated. Use setChipSpacingVertical(int) instead.

Parameters
divider Drawable

setFlexWrap

void setFlexWrap (int flexWrap)

Deprecated Use setSingleLine(int) instead.

Parameters
flexWrap int

setOnCheckedChangeListener

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
listener ChipGroup.OnCheckedChangeListener: the callback to call on checked state change

setOnHierarchyChangeListener

void setOnHierarchyChangeListener (ViewGroup.OnHierarchyChangeListener listener)

Parameters
listener ViewGroup.OnHierarchyChangeListener

setShowDividerHorizontal

void setShowDividerHorizontal (int dividerMode)

Deprecated. Use setChipSpacingHorizontal(int) instead.

Parameters
dividerMode int

setShowDividerVertical

void setShowDividerVertical (int dividerMode)

Deprecated. Use setChipSpacingVertical(int) instead.

Parameters
dividerMode int

setSingleLine

void setSingleLine (boolean singleLine)

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

Parameters
singleLine boolean

setSingleLine

void setSingleLine (int id)

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

Parameters
id int

setSingleSelection

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

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

boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
boolean

generateDefaultLayoutParams

ViewGroup.LayoutParams generateDefaultLayoutParams ()

Returns
ViewGroup.LayoutParams

generateLayoutParams

ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams lp)

Parameters
lp ViewGroup.LayoutParams

Returns
ViewGroup.LayoutParams

getItemSpacing

int getItemSpacing ()

Returns
int

getLineSpacing

int getLineSpacing ()

Returns
int

isSingleLine

boolean isSingleLine ()

Returns
boolean

onFinishInflate

void onFinishInflate ()

onLayout

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

Parameters
sizeChanged boolean

left int

top int

right int

bottom int

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

setItemSpacing

void setItemSpacing (int itemSpacing)

Parameters
itemSpacing int

setLineSpacing

void setLineSpacing (int lineSpacing)

Parameters
lineSpacing int