Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

ChipDrawable

public class ChipDrawable
extends Drawable

java.lang.Object
   ↳ Drawable
     ↳ com.google.android.material.chip.ChipDrawable


ChipDrawable contains all the layout and draw logic for Chip.

You can use ChipDrawable directly in contexts that require a Drawable. For example, an auto-complete enabled EditText can replace snippets of text with a ChipDrawable to represent it as a semantic entity. To create an instance of ChipDrawable, use createFromResource(Context, int) and pass in an XML resource in this form:

<chip xmlns:app="http://schemas.android.com/apk/res-auto"
     app:chipText="Hello, World!"/>
 

The basic attributes you can set are:

When used in this stand-alone mode, the host view must explicitly manage the ChipDrawable's state:

ChipDrawable's horizontal layout is as follows:

   chipStartPadding     iconEndPadding     closeIconStartPadding         chipEndPadding
    +                    +                                    +                      +
    |                    |                                    |                      |
    |  iconStartPadding  |  textStartPadding   textEndPadding | closeIconEndPadding  |
    |   +                |    +                            +  |                  +   |
    |   |                |    |                            |  |                  |   |
    v   v                v    v                            v  v                  v   v
 +-----+----+-----------+----+----+---------------------+----+----+----------+----+-----+
 |     |    |       XX  |    |    |  XX   X  X  X  XXX  |    |    | X      X |    |     |
 |     |    |      XX   |    |    | X  X  X  X  X  X  X |    |    |  XX  XX  |    |     |
 |     |    |  XX XX    |    |    | X     XXXX  X  XXX  |    |    |    XX    |    |     |
 |     |    |   XXX     |    |    | X  X  X  X  X  X    |    |    |  XX  XX  |    |     |
 |     |    |    X      |    |    |  XX   X  X  X  X    |    |    | X      X |    |     |
 +-----+----+-----------+----+----+---------------------+----+----+----------+----+-----+
                  ^                           ^                         ^
                  |                           |                         |
                  +                           +                         +
             chipIconSize                  *dynamic*              closeIconSize
 

ChipDrawable contains three child drawables: ERROR(/#chipIcon), ERROR(/#checkedIcon), and ERROR(/#closeIcon). chipIcon and checkedIcon inherit the state of this drawable, but closeIcon contains its own state that you can set with setCloseIconState(int[]).

See also:

Summary

Nested classes

interface ChipDrawable.Delegate

Delegate interface to be implemented by Views that own a ChipDrawable. 

Public methods

static ChipDrawable createFromAttributes(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Returns a ChipDrawable from the given attributes.

static ChipDrawable createFromResource(Context context, int id)

Returns a ChipDrawable from the given XML resource.

void draw(Canvas canvas)
int getAlpha()
Drawable getCheckedIcon()
ColorStateList getChipBackgroundColor()
float getChipCornerRadius()
float getChipEndPadding()
Drawable getChipIcon()
float getChipIconSize()
float getChipMinHeight()
float getChipStartPadding()
ColorStateList getChipStrokeColor()
float getChipStrokeWidth()
CharSequence getChipText()
void getChipTouchBounds(RectF bounds)

Returns the chip's ChipDrawable-absolute bounds (top-left is [ChipDrawable.getBounds().left, ChipDrawable.getBounds().top]).

Drawable getCloseIcon()
float getCloseIconEndPadding()
float getCloseIconSize()
float getCloseIconStartPadding()
int[] getCloseIconState()

Describes the current state of the close icon.

ColorStateList getCloseIconTint()
void getCloseIconTouchBounds(RectF bounds)

Returns the close icon's ChipDrawable-absolute bounds (top-left is [ChipDrawable.getBounds().left, ChipDrawable.getBounds().top]).

ColorFilter getColorFilter()
MotionSpec getHideMotionSpec()
float getIconEndPadding()
float getIconStartPadding()
int getIntrinsicHeight()

Returns the height at which the chip would like to be laid out.

int getIntrinsicWidth()

Returns the width at which the chip would like to be laid out.

int getOpacity()
void getOutline(Outline outline)
ColorStateList getRippleColor()
MotionSpec getShowMotionSpec()
TextAppearance getTextAppearance()
float getTextEndPadding()
float getTextStartPadding()
boolean getUseCompatRipple()

Returns whether this ChipDrawable should draw its own compatibility ripples.

void invalidateDrawable(Drawable who)
boolean isCheckable()
boolean isCheckedIconEnabled()
boolean isChipIconEnabled()
boolean isCloseIconEnabled()
boolean isCloseIconStateful()

Indicates whether the close icon drawable will change its appearance based on state.

boolean isStateful()

Indicates whether this chip drawable will change its appearance based on state.

boolean onLayoutDirectionChanged(int layoutDirection)
void scheduleDrawable(Drawable who, Runnable what, long when)
void setAlpha(int alpha)

Sets the alpha of this ChipDrawable.

void setCheckable(boolean checkable)
void setCheckableResource(int id)
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 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 chipMinHeight)
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)
boolean setCloseIconState(int[] stateSet)

Specify a set of states for the close icon.

void setCloseIconTint(ColorStateList closeIconTint)
void setCloseIconTintResource(int id)
void setColorFilter(ColorFilter colorFilter)
void setDelegate(ChipDrawable.Delegate delegate)

Sets the View delegate that owns this ChipDrawable.

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 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)
void setTintList(ColorStateList tint)
void setTintMode(Mode tintMode)
void setUseCompatRipple(boolean useCompatRipple)

Sets whether this ChipDrawable should draw its own compatibility ripples.

boolean setVisible(boolean visible, boolean restart)
void unscheduleDrawable(Drawable who, Runnable what)

Protected methods

boolean onLevelChange(int level)
void onSizeChange()

Attempts to call onChipDrawableSizeChange() on the delegate.

boolean onStateChange(int[] state)

Inherited methods

From class java.lang.Object

Public methods

createFromAttributes

ChipDrawable createFromAttributes (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Returns a ChipDrawable from the given attributes.

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Returns
ChipDrawable

createFromResource

ChipDrawable createFromResource (Context context, 
                int id)

Returns a ChipDrawable from the given XML resource. All attributes from ERROR(/R.styleable#ChipDrawable) and a custom style attribute are supported. A chip resource may look like:

<chip
     xmlns:app="http://schemas.android.com/apk/res-auto"
     style="@style/Widget.MaterialComponents.Chip.Entry"
     app:chipIcon="@drawable/custom_icon"/>
 

Parameters
context Context

id int

Returns
ChipDrawable

draw

void draw (Canvas canvas)

Parameters
canvas Canvas

getAlpha

int getAlpha ()

Returns
int

getCheckedIcon

Drawable getCheckedIcon ()

Returns
Drawable

getChipBackgroundColor

ColorStateList getChipBackgroundColor ()

Returns
ColorStateList

getChipCornerRadius

float getChipCornerRadius ()

Returns
float

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

getChipTouchBounds

void getChipTouchBounds (RectF bounds)

Returns the chip's ChipDrawable-absolute bounds (top-left is [ChipDrawable.getBounds().left, ChipDrawable.getBounds().top]).

Parameters
bounds RectF

getCloseIcon

Drawable getCloseIcon ()

Returns
Drawable

getCloseIconEndPadding

float getCloseIconEndPadding ()

Returns
float

getCloseIconSize

float getCloseIconSize ()

Returns
float

getCloseIconStartPadding

float getCloseIconStartPadding ()

Returns
float

getCloseIconState

int[] getCloseIconState ()

Describes the current state of the close icon.

Returns
int[]

getCloseIconTint

ColorStateList getCloseIconTint ()

Returns
ColorStateList

getCloseIconTouchBounds

void getCloseIconTouchBounds (RectF bounds)

Returns the close icon's ChipDrawable-absolute bounds (top-left is [ChipDrawable.getBounds().left, ChipDrawable.getBounds().top]).

Parameters
bounds RectF

getColorFilter

ColorFilter getColorFilter ()

Returns
ColorFilter

getHideMotionSpec

MotionSpec getHideMotionSpec ()

Returns
MotionSpec

getIconEndPadding

float getIconEndPadding ()

Returns
float

getIconStartPadding

float getIconStartPadding ()

Returns
float

getIntrinsicHeight

int getIntrinsicHeight ()

Returns the height at which the chip would like to be laid out.

Returns
int

getIntrinsicWidth

int getIntrinsicWidth ()

Returns the width at which the chip would like to be laid out.

Returns
int

getOpacity

int getOpacity ()

Returns
int

getOutline

void getOutline (Outline outline)

Parameters
outline Outline

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

getUseCompatRipple

boolean getUseCompatRipple ()

Returns whether this ChipDrawable should draw its own compatibility ripples.

Returns
boolean

invalidateDrawable

void invalidateDrawable (Drawable who)

Parameters
who Drawable

isCheckable

boolean isCheckable ()

Returns
boolean

isCheckedIconEnabled

boolean isCheckedIconEnabled ()

Returns
boolean

isChipIconEnabled

boolean isChipIconEnabled ()

Returns
boolean

isCloseIconEnabled

boolean isCloseIconEnabled ()

Returns
boolean

isCloseIconStateful

boolean isCloseIconStateful ()

Indicates whether the close icon drawable will change its appearance based on state.

The logic here and isStateful() must match ERROR(/#onStateChange(int[], int[])).

Returns
boolean

isStateful

boolean isStateful ()

Indicates whether this chip drawable will change its appearance based on state.

The logic here and isCloseIconStateful() must match ERROR(/#onStateChange(int[], int[])).

Returns
boolean

onLayoutDirectionChanged

boolean onLayoutDirectionChanged (int layoutDirection)

Parameters
layoutDirection int

Returns
boolean

scheduleDrawable

void scheduleDrawable (Drawable who, 
                Runnable what, 
                long when)

Parameters
who Drawable

what Runnable

when long

setAlpha

void setAlpha (int alpha)

Sets the alpha of this ChipDrawable. This will drastically decrease draw performance. You are highly encouraged to use View instead.

Parameters
alpha int

setCheckable

void setCheckable (boolean checkable)

Parameters
checkable boolean

setCheckableResource

void setCheckableResource (int id)

Parameters
id int

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

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 chipMinHeight)

Parameters
chipMinHeight 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

setCloseIconState

boolean setCloseIconState (int[] stateSet)

Specify a set of states for the close icon. This is a separate state set than the one used for the rest of the chip.

Parameters
stateSet int

Returns
boolean

setCloseIconTint

void setCloseIconTint (ColorStateList closeIconTint)

Parameters
closeIconTint ColorStateList

setCloseIconTintResource

void setCloseIconTintResource (int id)

Parameters
id int

setColorFilter

void setColorFilter (ColorFilter colorFilter)

Parameters
colorFilter ColorFilter

setDelegate

void setDelegate (ChipDrawable.Delegate delegate)

Sets the View delegate that owns this ChipDrawable.

Parameters
delegate ChipDrawable.Delegate

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

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

setTintList

void setTintList (ColorStateList tint)

Parameters
tint ColorStateList

setTintMode

void setTintMode (Mode tintMode)

Parameters
tintMode Mode

setUseCompatRipple

void setUseCompatRipple (boolean useCompatRipple)

Sets whether this ChipDrawable should draw its own compatibility ripples.

Parameters
useCompatRipple boolean

setVisible

boolean setVisible (boolean visible, 
                boolean restart)

Parameters
visible boolean

restart boolean

Returns
boolean

unscheduleDrawable

void unscheduleDrawable (Drawable who, 
                Runnable what)

Parameters
who Drawable

what Runnable

Protected methods

onLevelChange

boolean onLevelChange (int level)

Parameters
level int

Returns
boolean

onSizeChange

void onSizeChange ()

Attempts to call onChipDrawableSizeChange() on the delegate.

onStateChange

boolean onStateChange (int[] state)

Parameters
state int

Returns
boolean