ChipGroup

public class ChipGroup
extends ViewGroup

java.lang.Object
   ↳ 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 ERROR(app:singleLine/R.attr#singleLine 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 ERROR(app:singleSelection/R.attr#singleSelection 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

interface ChipGroup.OnCheckedChangeListener

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

Constants

int SHOW_DIVIDER_BEGINNING

Constant to show a divider at the beginning of the flex lines (or flex items).

int SHOW_DIVIDER_END

Constant to show a divider at the end of the flex lines or flex items.

int SHOW_DIVIDER_MIDDLE

Constant to show dividers between flex lines or flex items.

int SHOW_DIVIDER_NONE

Constant to show no dividers

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.

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

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

int getChildHeightMeasureSpec(int heightSpec, int padding, int childDimension)
int getChildWidthMeasureSpec(int widthSpec, int padding, int childDimension)
int getChipSpacingHorizontal()

Returns the horizontal spacing between chips in this group.

int getChipSpacingVertical()

Returns the vertical spacing between chips in this group.

int getDecorationLengthCrossAxis(View view)
int getDecorationLengthMainAxis(View view, int index, int indexInFlexLine)
Drawable getDividerDrawableHorizontal()
Drawable getDividerDrawableVertical()
View getFlexItemAt(int index)
int getFlexItemCount()
List<FlexLine> getFlexLines()
List<FlexLine> getFlexLinesInternal()
int getFlexWrap()
int getLargestMainSize()
View getReorderedChildAt(int index)

Returns a View, which is reordered by taking ERROR(/LayoutParams#order) parameters into account.

View getReorderedFlexItemAt(int index)
int getSumOfCrossSize()
boolean isMainAxisDirectionHorizontal()
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 onNewFlexItemAdded(View view, int index, int indexInFlexLine, FlexLine flexLine)
void onNewFlexLineAdded(FlexLine flexLine)
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 setDividerDrawable(Drawable divider)

Set a drawable to be used as a divider between items.

void setDividerDrawableHorizontal(Drawable divider)

Set a drawable to be used as a horizontal divider between items.

void setDividerDrawableVertical(Drawable divider)

Set a drawable to be used as a vertical divider between items.

void setFlexLines(List<FlexLine> flexLines)
void setFlexWrap(int flexWrap)

Sets the given flex wrap attribute to the flex container.

void setOnCheckedChangeListener(ChipGroup.OnCheckedChangeListener listener)

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

void setOnHierarchyChangeListener(OnHierarchyChangeListener listener)
void setShowDivider(int dividerMode)

Set how dividers should be shown between items in this layout.

void setShowDividerHorizontal(int dividerMode)

Set how horizontal dividers should be shown between items in this layout.

void setShowDividerVertical(int dividerMode)

Set how vertical dividers should be shown between items in this layout

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.

void updateViewCache(int position, View view)

Protected methods

boolean checkLayoutParams(ViewGroup.LayoutParams p)
ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp)
void onDraw(Canvas canvas)
void onFinishInflate()
void onLayout(boolean changed, int left, int top, int right, int bottom)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Inherited methods

From class java.lang.Object

Constants

SHOW_DIVIDER_BEGINNING

int SHOW_DIVIDER_BEGINNING

Constant to show a divider at the beginning of the flex lines (or flex items).

Constant Value: 1 (0x00000001)

SHOW_DIVIDER_END

int SHOW_DIVIDER_END

Constant to show a divider at the end of the flex lines or flex items.

Constant Value: 4 (0x00000004)

SHOW_DIVIDER_MIDDLE

int SHOW_DIVIDER_MIDDLE

Constant to show dividers between flex lines or flex items.

Constant Value: 2 (0x00000002)

SHOW_DIVIDER_NONE

int SHOW_DIVIDER_NONE

Constant to show no dividers

Constant Value: 0 (0x00000000)

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

generateLayoutParams

FlexboxLayout.LayoutParams generateLayoutParams (AttributeSet attrs)

Parameters
attrs AttributeSet

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

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

getChildHeightMeasureSpec

int getChildHeightMeasureSpec (int heightSpec, 
                int padding, 
                int childDimension)

Parameters
heightSpec int

padding int

childDimension int

Returns
int

getChildWidthMeasureSpec

int getChildWidthMeasureSpec (int widthSpec, 
                int padding, 
                int childDimension)

Parameters
widthSpec int

padding int

childDimension int

Returns
int

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

getDecorationLengthCrossAxis

int getDecorationLengthCrossAxis (View view)

Parameters
view View

Returns
int

getDecorationLengthMainAxis

int getDecorationLengthMainAxis (View view, 
                int index, 
                int indexInFlexLine)

Parameters
view View

index int

indexInFlexLine int

Returns
int

getDividerDrawableHorizontal

Drawable getDividerDrawableHorizontal ()

Returns
Drawable the horizontal divider drawable that will divide each item.

getDividerDrawableVertical

Drawable getDividerDrawableVertical ()

Returns
Drawable the vertical divider drawable that will divide each item.

getFlexItemAt

View getFlexItemAt (int index)

Parameters
index int

Returns
View

getFlexItemCount

int getFlexItemCount ()

Returns
int

getFlexLines

List<FlexLine> getFlexLines ()

Returns
List<FlexLine> the flex lines composing this flex container. This method returns a copy of the original list excluding a dummy flex line (flex line that doesn't have any flex items in it but used for the alignment along the cross axis). Thus any changes of the returned list are not reflected to the original list.

getFlexLinesInternal

List<FlexLine> getFlexLinesInternal ()

Returns
List<FlexLine>

getFlexWrap

int getFlexWrap ()

Returns
int

getLargestMainSize

int getLargestMainSize ()

Returns
int

getReorderedChildAt

View getReorderedChildAt (int index)

Returns a View, which is reordered by taking ERROR(/LayoutParams#order) parameters into account.

Parameters
index int: the index of the view

Returns
View the reordered view, which ERROR(/LayoutParams@order) is taken into account. If the index is negative or out of bounds of the number of contained views, returns null.

getReorderedFlexItemAt

View getReorderedFlexItemAt (int index)

Parameters
index int

Returns
View

getSumOfCrossSize

int getSumOfCrossSize ()

Returns
int

isMainAxisDirectionHorizontal

boolean isMainAxisDirectionHorizontal ()

Returns
boolean

isSingleLine

boolean isSingleLine ()

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

Returns
boolean

isSingleSelection

boolean isSingleSelection ()

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

Returns
boolean

onNewFlexItemAdded

void onNewFlexItemAdded (View view, 
                int index, 
                int indexInFlexLine, 
                FlexLine flexLine)

Parameters
view View

index int

indexInFlexLine int

flexLine FlexLine

onNewFlexLineAdded

void onNewFlexLineAdded (FlexLine flexLine)

Parameters
flexLine FlexLine

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

setDividerDrawable

void setDividerDrawable (Drawable divider)

Set a drawable to be used as a divider between items. The drawable is used for both horizontal and vertical dividers.

Parameters
divider Drawable: Drawable that will divide each item for both horizontally and vertically.

setDividerDrawableHorizontal

void setDividerDrawableHorizontal (Drawable divider)

Set a drawable to be used as a horizontal divider between items.

Parameters
divider Drawable: Drawable that will divide each item.

setDividerDrawableVertical

void setDividerDrawableVertical (Drawable divider)

Set a drawable to be used as a vertical divider between items.

Parameters
divider Drawable: Drawable that will divide each item.

setFlexLines

void setFlexLines (List<FlexLine> flexLines)

Parameters
flexLines List

setFlexWrap

void setFlexWrap (int flexWrap)

Sets the given flex wrap attribute to the flex container.

Parameters
flexWrap int: the flex wrap value

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 (OnHierarchyChangeListener listener)

Parameters
listener OnHierarchyChangeListener

setShowDivider

void setShowDivider (int dividerMode)

Set how dividers should be shown between items in this layout. This method sets the divider mode for both horizontally and vertically.

Parameters
dividerMode int: One or more of SHOW_DIVIDER_BEGINNING, SHOW_DIVIDER_MIDDLE, or SHOW_DIVIDER_END, or SHOW_DIVIDER_NONE to show no dividers.

setShowDividerHorizontal

void setShowDividerHorizontal (int dividerMode)

Set how horizontal dividers should be shown between items in this layout.

Parameters
dividerMode int: One or more of SHOW_DIVIDER_BEGINNING, SHOW_DIVIDER_MIDDLE, or SHOW_DIVIDER_END, or SHOW_DIVIDER_NONE to show no dividers.

setShowDividerVertical

void setShowDividerVertical (int dividerMode)

Set how vertical dividers should be shown between items in this layout

Parameters
dividerMode int: One or more of SHOW_DIVIDER_BEGINNING, SHOW_DIVIDER_MIDDLE, or SHOW_DIVIDER_END, or SHOW_DIVIDER_NONE to show no dividers.

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

updateViewCache

void updateViewCache (int position, 
                View view)

Parameters
position int

view View

Protected methods

checkLayoutParams

boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
boolean

generateLayoutParams

ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams lp)

Parameters
lp ViewGroup.LayoutParams

Returns
ViewGroup.LayoutParams

onDraw

void onDraw (Canvas canvas)

Parameters
canvas Canvas

onFinishInflate

void onFinishInflate ()

onLayout

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

Parameters
changed boolean

left int

top int

right int

bottom int

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int