Chip

public class Chip
extends AppCompatCheckBox implements ChipDrawable.Delegate

java.lang.Object
   ↳ AppCompatCheckBox
     ↳ com.google.android.material.chip.Chip


Chips are compact elements that represent an attribute, text, entity, or action. They allow users to enter information, select a choice, filter content, or trigger an action.

The Chip widget is a thin view wrapper around the ChipDrawable, which contains all of the layout and draw logic. The extra logic exists to support touch, mouse, keyboard, and accessibility navigation. The main chip and close icon are considered to be separate logical sub-views, and contain their own navigation behavior and state.

All attributes from ERROR(/R.styleable#ChipDrawable) are supported. Do not use the android:background and android:text attributes. They will be ignored because Chip manages its own background Drawable and text. The basic attributes you can set are:

You can register a listener on the main chip with ERROR(/#setOnClickListener(OnClickListener)) or ERROR(/#setOnCheckedChangeListener(OnCheckedChangeListener)). You can register a listener on the close icon with setOnCloseIconClickListener(OnClickListener).

See also:

Summary

Public constructors

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

Public methods

Drawable getCheckedIcon()
ColorStateList getChipBackgroundColor()
float getChipCornerRadius()
Drawable getChipDrawable()

Returns the ChipDrawable backing this chip.

float getChipEndPadding()
Drawable getChipIcon()
float getChipIconSize()
float getChipMinHeight()
float getChipStartPadding()
ColorStateList getChipStrokeColor()
float getChipStrokeWidth()
CharSequence getChipText()
Drawable getCloseIcon()
float getCloseIconEndPadding()
float getCloseIconSize()
float getCloseIconStartPadding()
ColorStateList getCloseIconTint()
void getFocusedRect(Rect r)
MotionSpec getHideMotionSpec()
float getIconEndPadding()
float getIconStartPadding()
ColorStateList getRippleColor()
MotionSpec getShowMotionSpec()
TextAppearance getTextAppearance()
float getTextEndPadding()
float getTextStartPadding()
boolean isCheckable()
boolean isCheckedIconEnabled()
boolean isChipIconEnabled()
boolean isCloseIconEnabled()
void onChipDrawableSizeChange()

Handles a change in the ChipDrawable's size.

boolean onHoverEvent(MotionEvent event)
boolean onKeyDown(int keyCode, KeyEvent event)
PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex)
boolean onTouchEvent(MotionEvent event)
boolean performCloseIconClick()

Call this chip's ERROR(/#onCloseIconClickListener), if it is defined.

void setCheckable(boolean checkable)
void setCheckableResource(int id)
void setChecked(boolean checked)
void setCheckedIcon(Drawable checkedIcon)
void setCheckedIconEnabled(boolean checkedIconEnabled)
void setCheckedIconEnabledResource(int id)
void setCheckedIconResource(int id)
void setChipBackgroundColor(ColorStateList chipBackgroundColor)
void setChipBackgroundColorResource(int id)
void setChipCornerRadius(float chipCornerRadius)
void setChipCornerRadiusResource(int id)
void setChipDrawable(ChipDrawable drawable)

Sets the ChipDrawable backing this chip.

void setChipEndPadding(float chipEndPadding)
void setChipEndPaddingResource(int id)
void setChipIcon(Drawable chipIcon)
void setChipIconEnabled(boolean chipIconEnabled)
void setChipIconEnabledResource(int id)
void setChipIconResource(int id)
void setChipIconSize(float chipIconSize)
void setChipIconSizeResource(int id)
void setChipMinHeight(float minHeight)
void setChipMinHeightResource(int id)
void setChipStartPadding(float chipStartPadding)
void setChipStartPaddingResource(int id)
void setChipStrokeColor(ColorStateList chipStrokeColor)
void setChipStrokeColorResource(int id)
void setChipStrokeWidth(float chipStrokeWidth)
void setChipStrokeWidthResource(int id)
void setChipText(CharSequence chipText)
void setChipTextResource(int id)
void setCloseIcon(Drawable closeIcon)
void setCloseIconEnabled(boolean closeIconEnabled)
void setCloseIconEnabledResource(int id)
void setCloseIconEndPadding(float closeIconEndPadding)
void setCloseIconEndPaddingResource(int id)
void setCloseIconResource(int id)
void setCloseIconSize(float closeIconSize)
void setCloseIconSizeResource(int id)
void setCloseIconStartPadding(float closeIconStartPadding)
void setCloseIconStartPaddingResource(int id)
void setCloseIconTint(ColorStateList closeIconTint)
void setCloseIconTintResource(int id)
void setHideMotionSpec(MotionSpec hideMotionSpec)
void setHideMotionSpecResource(int id)
void setIconEndPadding(float iconEndPadding)
void setIconEndPaddingResource(int id)
void setIconStartPadding(float iconStartPadding)
void setIconStartPaddingResource(int id)
void setOnCloseIconClickListener(OnClickListener listener)

Register a callback to be invoked when the close icon is clicked.

void setRippleColor(ColorStateList rippleColor)
void setRippleColorResource(int id)
void setShowMotionSpec(MotionSpec showMotionSpec)
void setShowMotionSpecResource(int id)
void setTextAppearance(TextAppearance textAppearance)
void setTextAppearanceResource(int id)
void setTextEndPadding(float textEndPadding)
void setTextEndPaddingResource(int id)
void setTextStartPadding(float textStartPadding)
void setTextStartPaddingResource(int id)

Protected methods

boolean dispatchHoverEvent(MotionEvent event)
void drawableStateChanged()
int[] onCreateDrawableState(int extraSpace)
void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)

Inherited methods

From class java.lang.Object
From interface com.google.android.material.chip.ChipDrawable.Delegate

Public constructors

Chip

Chip (Context context)

Parameters
context Context

Chip

Chip (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

Chip

Chip (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

getCheckedIcon

Drawable getCheckedIcon ()

Returns
Drawable

getChipBackgroundColor

ColorStateList getChipBackgroundColor ()

Returns
ColorStateList

getChipCornerRadius

float getChipCornerRadius ()

Returns
float

getChipDrawable

Drawable getChipDrawable ()

Returns the ChipDrawable backing this chip.

Returns
Drawable

getChipEndPadding

float getChipEndPadding ()

Returns
float

getChipIcon

Drawable getChipIcon ()

Returns
Drawable

getChipIconSize

float getChipIconSize ()

Returns
float

getChipMinHeight

float getChipMinHeight ()

Returns
float

getChipStartPadding

float getChipStartPadding ()

Returns
float

getChipStrokeColor

ColorStateList getChipStrokeColor ()

Returns
ColorStateList

getChipStrokeWidth

float getChipStrokeWidth ()

Returns
float

getChipText

CharSequence getChipText ()

Returns
CharSequence

getCloseIcon

Drawable getCloseIcon ()

Returns
Drawable

getCloseIconEndPadding

float getCloseIconEndPadding ()

Returns
float

getCloseIconSize

float getCloseIconSize ()

Returns
float

getCloseIconStartPadding

float getCloseIconStartPadding ()

Returns
float

getCloseIconTint

ColorStateList getCloseIconTint ()

Returns
ColorStateList

getFocusedRect

void getFocusedRect (Rect r)

Parameters
r Rect

getHideMotionSpec

MotionSpec getHideMotionSpec ()

Returns
MotionSpec

getIconEndPadding

float getIconEndPadding ()

Returns
float

getIconStartPadding

float getIconStartPadding ()

Returns
float

getRippleColor

ColorStateList getRippleColor ()

Returns
ColorStateList

getShowMotionSpec

MotionSpec getShowMotionSpec ()

Returns
MotionSpec

getTextAppearance

TextAppearance getTextAppearance ()

Returns
TextAppearance

getTextEndPadding

float getTextEndPadding ()

Returns
float

getTextStartPadding

float getTextStartPadding ()

Returns
float

isCheckable

boolean isCheckable ()

Returns
boolean

isCheckedIconEnabled

boolean isCheckedIconEnabled ()

Returns
boolean

isChipIconEnabled

boolean isChipIconEnabled ()

Returns
boolean

isCloseIconEnabled

boolean isCloseIconEnabled ()

Returns
boolean

onChipDrawableSizeChange

void onChipDrawableSizeChange ()

Handles a change in the ChipDrawable's size.

onHoverEvent

boolean onHoverEvent (MotionEvent event)

Parameters
event MotionEvent

Returns
boolean

onKeyDown

boolean onKeyDown (int keyCode, 
                KeyEvent event)

Parameters
keyCode int

event KeyEvent

Returns
boolean

onResolvePointerIcon

PointerIcon onResolvePointerIcon (MotionEvent event, 
                int pointerIndex)

Parameters
event MotionEvent

pointerIndex int

Returns
PointerIcon

onTouchEvent

boolean onTouchEvent (MotionEvent event)

Parameters
event MotionEvent

Returns
boolean

performCloseIconClick

boolean performCloseIconClick ()

Call this chip's ERROR(/#onCloseIconClickListener), if it is defined. Performs all normal actions associated with clicking: reporting accessibility event, playing a sound, etc.

Returns
boolean True there was an assigned ERROR(/#onCloseIconClickListener) that was called, false otherwise is returned.

setCheckable

void setCheckable (boolean checkable)

Parameters
checkable boolean

setCheckableResource

void setCheckableResource (int id)

Parameters
id int

setChecked

void setChecked (boolean checked)

Parameters
checked boolean

setCheckedIcon

void setCheckedIcon (Drawable checkedIcon)

Parameters
checkedIcon Drawable

setCheckedIconEnabled

void setCheckedIconEnabled (boolean checkedIconEnabled)

Parameters
checkedIconEnabled boolean

setCheckedIconEnabledResource

void setCheckedIconEnabledResource (int id)

Parameters
id int

setCheckedIconResource

void setCheckedIconResource (int id)

Parameters
id int

setChipBackgroundColor

void setChipBackgroundColor (ColorStateList chipBackgroundColor)

Parameters
chipBackgroundColor ColorStateList

setChipBackgroundColorResource

void setChipBackgroundColorResource (int id)

Parameters
id int

setChipCornerRadius

void setChipCornerRadius (float chipCornerRadius)

Parameters
chipCornerRadius float

setChipCornerRadiusResource

void setChipCornerRadiusResource (int id)

Parameters
id int

setChipDrawable

void setChipDrawable (ChipDrawable drawable)

Sets the ChipDrawable backing this chip.

Parameters
drawable ChipDrawable

setChipEndPadding

void setChipEndPadding (float chipEndPadding)

Parameters
chipEndPadding float

setChipEndPaddingResource

void setChipEndPaddingResource (int id)

Parameters
id int

setChipIcon

void setChipIcon (Drawable chipIcon)

Parameters
chipIcon Drawable

setChipIconEnabled

void setChipIconEnabled (boolean chipIconEnabled)

Parameters
chipIconEnabled boolean

setChipIconEnabledResource

void setChipIconEnabledResource (int id)

Parameters
id int

setChipIconResource

void setChipIconResource (int id)

Parameters
id int

setChipIconSize

void setChipIconSize (float chipIconSize)

Parameters
chipIconSize float

setChipIconSizeResource

void setChipIconSizeResource (int id)

Parameters
id int

setChipMinHeight

void setChipMinHeight (float minHeight)

Parameters
minHeight float

setChipMinHeightResource

void setChipMinHeightResource (int id)

Parameters
id int

setChipStartPadding

void setChipStartPadding (float chipStartPadding)

Parameters
chipStartPadding float

setChipStartPaddingResource

void setChipStartPaddingResource (int id)

Parameters
id int

setChipStrokeColor

void setChipStrokeColor (ColorStateList chipStrokeColor)

Parameters
chipStrokeColor ColorStateList

setChipStrokeColorResource

void setChipStrokeColorResource (int id)

Parameters
id int

setChipStrokeWidth

void setChipStrokeWidth (float chipStrokeWidth)

Parameters
chipStrokeWidth float

setChipStrokeWidthResource

void setChipStrokeWidthResource (int id)

Parameters
id int

setChipText

void setChipText (CharSequence chipText)

Parameters
chipText CharSequence

setChipTextResource

void setChipTextResource (int id)

Parameters
id int

setCloseIcon

void setCloseIcon (Drawable closeIcon)

Parameters
closeIcon Drawable

setCloseIconEnabled

void setCloseIconEnabled (boolean closeIconEnabled)

Parameters
closeIconEnabled boolean

setCloseIconEnabledResource

void setCloseIconEnabledResource (int id)

Parameters
id int

setCloseIconEndPadding

void setCloseIconEndPadding (float closeIconEndPadding)

Parameters
closeIconEndPadding float

setCloseIconEndPaddingResource

void setCloseIconEndPaddingResource (int id)

Parameters
id int

setCloseIconResource

void setCloseIconResource (int id)

Parameters
id int

setCloseIconSize

void setCloseIconSize (float closeIconSize)

Parameters
closeIconSize float

setCloseIconSizeResource

void setCloseIconSizeResource (int id)

Parameters
id int

setCloseIconStartPadding

void setCloseIconStartPadding (float closeIconStartPadding)

Parameters
closeIconStartPadding float

setCloseIconStartPaddingResource

void setCloseIconStartPaddingResource (int id)

Parameters
id int

setCloseIconTint

void setCloseIconTint (ColorStateList closeIconTint)

Parameters
closeIconTint ColorStateList

setCloseIconTintResource

void setCloseIconTintResource (int id)

Parameters
id int

setHideMotionSpec

void setHideMotionSpec (MotionSpec hideMotionSpec)

Parameters
hideMotionSpec MotionSpec

setHideMotionSpecResource

void setHideMotionSpecResource (int id)

Parameters
id int

setIconEndPadding

void setIconEndPadding (float iconEndPadding)

Parameters
iconEndPadding float

setIconEndPaddingResource

void setIconEndPaddingResource (int id)

Parameters
id int

setIconStartPadding

void setIconStartPadding (float iconStartPadding)

Parameters
iconStartPadding float

setIconStartPaddingResource

void setIconStartPaddingResource (int id)

Parameters
id int

setOnCloseIconClickListener

void setOnCloseIconClickListener (OnClickListener listener)

Register a callback to be invoked when the close icon is clicked.

Parameters
listener OnClickListener

setRippleColor

void setRippleColor (ColorStateList rippleColor)

Parameters
rippleColor ColorStateList

setRippleColorResource

void setRippleColorResource (int id)

Parameters
id int

setShowMotionSpec

void setShowMotionSpec (MotionSpec showMotionSpec)

Parameters
showMotionSpec MotionSpec

setShowMotionSpecResource

void setShowMotionSpecResource (int id)

Parameters
id int

setTextAppearance

void setTextAppearance (TextAppearance textAppearance)

Parameters
textAppearance TextAppearance

setTextAppearanceResource

void setTextAppearanceResource (int id)

Parameters
id int

setTextEndPadding

void setTextEndPadding (float textEndPadding)

Parameters
textEndPadding float

setTextEndPaddingResource

void setTextEndPaddingResource (int id)

Parameters
id int

setTextStartPadding

void setTextStartPadding (float textStartPadding)

Parameters
textStartPadding float

setTextStartPaddingResource

void setTextStartPaddingResource (int id)

Parameters
id int

Protected methods

dispatchHoverEvent

boolean dispatchHoverEvent (MotionEvent event)

Parameters
event MotionEvent

Returns
boolean

drawableStateChanged

void drawableStateChanged ()

onCreateDrawableState

int[] onCreateDrawableState (int extraSpace)

Parameters
extraSpace int

Returns
int[]

onFocusChanged

void onFocusChanged (boolean focused, 
                int direction, 
                Rect previouslyFocusedRect)

Parameters
focused boolean

direction int

previouslyFocusedRect Rect