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 R.styleable.Chip are supported. Do not use the android:background attribute. It will be ignored because Chip manages its own background Drawable. Also do not use the android:drawableStart and android:drawableEnd attributes. They will be ignored because Chip manages its own start (app:chipIcon) and end (app:closeIcon) drawables. The basic attributes you can set are:

  • android:checkable - If true, the chip can be toggled. If false, the chip acts like a button.
  • android:text - Sets the text of the chip.
  • app:chipIcon and app:chipIconEnabled - Sets the icon of the chip. Usually on the left.
  • app:checkedIcon and app:checkedIconEnabled - Sets a custom icon to use when checked. Usually on the left.
  • app:closeIcon and app:closeIconEnabled - Sets a custom icon that the user can click to close. Usually on the right.

You can register a listener on the main chip with setOnClickListener or setOnCheckedChangeListener. You can register a listener on the close icon with setOnCloseIconClickListener.

For proper rendering of the ancestor TextView in RTL mode, call setLayoutDirection with View.LAYOUT_DIRECTION_LOCALE. By default, TextView's layout rendering sets the text padding in LTR on initial rendering and it only renders correctly after the layout has been invalidated so you need to ensure that initial rendering has the correct layout.

For more information, see the component developer guidance and design guidelines.

See also

Constructors

Link copied to clipboard
constructor(context: Context)
constructor(context: Context, attrs: AttributeSet)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int)

Properties

Functions

Link copied to clipboard
Link copied to clipboard
Extends the touch target of this chip using a InsetDrawable if chip's intrinsic width / height is smaller than the minTargetPx.
Link copied to clipboard
Link copied to clipboard
Returns this chip's checked icon.
Link copied to clipboard
Returns the android.content.res.ColorStateList used to tint the checked icon.
Link copied to clipboard
Returns this chip's background color.
Link copied to clipboard
Returns this chip's corner radius.
Link copied to clipboard
Returns the ChipDrawable backing this chip.
Link copied to clipboard
Returns this chip's end padding.
Link copied to clipboard
Returns this chip's icon.
Link copied to clipboard
Returns this chip's icon size.
Link copied to clipboard
Returns the android.content.res.ColorStateList used to tint the chip icon.
Link copied to clipboard
Returns this chip's minimum height.
Link copied to clipboard
Returns this chip's start padding.
Link copied to clipboard
Returns this chip's stroke color.
Link copied to clipboard
Returns this chip's stroke width.
Link copied to clipboard
Returns this chip's text.
Link copied to clipboard
Returns this chip's close icon.
Link copied to clipboard
Returns this chip's close icon content description.
Link copied to clipboard
Returns the end padding for this chip's close icon.
Link copied to clipboard
Returns this chip's close icon size.
Link copied to clipboard
Returns the start padding for this chip's close icon.
Link copied to clipboard
Returns the tint color for this chip's close icon.
Link copied to clipboard
Link copied to clipboard
open fun getFocusedRect(@NonNull r: Rect)
Link copied to clipboard
Returns this chip's hide motion spec.
Link copied to clipboard
Returns the end padding for this chip's icon.
Link copied to clipboard
Returns the start padding for this chip's icon.
Link copied to clipboard
Returns this chip's ripple color.
Link copied to clipboard
Returns the ShapeAppearanceModel used for the shape definition.
Link copied to clipboard
Returns this chip's show motion spec.
Link copied to clipboard
Returns the end padding for this chip's text.
Link copied to clipboard
Returns the start padding for this chip's text.
Link copied to clipboard
open fun isCheckable(): Boolean
Returns whether this chip is checkable.
Link copied to clipboard
Link copied to clipboard
Returns whether this chip's checked icon is visible.
Link copied to clipboard
Link copied to clipboard
Returns whether this chip's icon is visible.
Link copied to clipboard
Link copied to clipboard
Returns whether this chip's close icon is visible.
Link copied to clipboard
Handles a change in the ChipDrawable's size.
Link copied to clipboard
Link copied to clipboard
open fun onResolvePointerIcon(@NonNull event: MotionEvent, pointerIndex: Int): PointerIcon
Link copied to clipboard
open fun onRtlPropertiesChanged(layoutDirection: Int)
Link copied to clipboard
Link copied to clipboard
Call this chip's close icon click listener, if it is defined.
Link copied to clipboard
open fun setBackground(background: Drawable)
Link copied to clipboard
open fun setBackgroundColor(color: Int)
Link copied to clipboard
open fun setBackgroundDrawable(background: Drawable)
Link copied to clipboard
open fun setBackgroundResource(resid: Int)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun setCheckable(checkable: Boolean)
Sets whether this chip is checkable.
Link copied to clipboard
Sets whether this chip is checkable using a resource id.
Link copied to clipboard
open fun setChecked(checked: Boolean)
Link copied to clipboard
open fun setCheckedIcon(@Nullable checkedIcon: Drawable)
Sets this chip's checked icon.
Link copied to clipboard
open fun setCheckedIconEnabled(checkedIconEnabled: Boolean)
Link copied to clipboard
Link copied to clipboard
Sets this chip's checked icon using a resource id.
Link copied to clipboard
open fun setCheckedIconTint(@Nullable checkedIconTint: ColorStateList)
Sets this chip's checked icon's color tint using the specified .
Link copied to clipboard
Sets this chip's checked icon's color tint using a resource id.
Link copied to clipboard
open fun setCheckedIconVisible(checkedIconVisible: Boolean)
Sets whether this chip's checked icon is visible.
Sets whether this chip's checked icon is visible using a resource id.
Link copied to clipboard
open fun setChipBackgroundColor(@Nullable chipBackgroundColor: ColorStateList)
Sets this chip's background color.
Link copied to clipboard
Sets this chip's background color using a resource id.
Link copied to clipboard
open fun setChipCornerRadius(chipCornerRadius: Float)
Link copied to clipboard
Link copied to clipboard
open fun setChipDrawable(@NonNull drawable: ChipDrawable)
Sets the ChipDrawable backing this chip.
Link copied to clipboard
open fun setChipEndPadding(chipEndPadding: Float)
Sets this chip's end padding.
Link copied to clipboard
Sets this chip's end padding using a resource id.
Link copied to clipboard
open fun setChipIcon(@Nullable chipIcon: Drawable)
Sets this chip's icon.
Link copied to clipboard
open fun setChipIconEnabled(chipIconEnabled: Boolean)
Link copied to clipboard
Link copied to clipboard
Sets this chip's icon using a resource id.
Link copied to clipboard
open fun setChipIconSize(chipIconSize: Float)
Sets this chip icon's size.
Link copied to clipboard
Sets this chip icon's size using a resource id.
Link copied to clipboard
open fun setChipIconTint(@Nullable chipIconTint: ColorStateList)
Sets this chip icon's color tint using the specified .
Link copied to clipboard
Sets this chip icon's color tint using a resource id.
Link copied to clipboard
open fun setChipIconVisible(chipIconVisible: Boolean)
Sets whether this chip's icon is visible.
Sets the visibility of this chip's icon using a resource id.
Link copied to clipboard
open fun setChipMinHeight(minHeight: Float)
Sets this chip's minimum height.
Link copied to clipboard
Sets this chip's minimum height using a resource id.
Link copied to clipboard
open fun setChipStartPadding(chipStartPadding: Float)
Sets this chip's start padding.
Link copied to clipboard
Sets this chip's start padding using a resource id.
Link copied to clipboard
open fun setChipStrokeColor(@Nullable chipStrokeColor: ColorStateList)
Sets this chip's stroke color.
Link copied to clipboard
Sets this chip's stroke color using a resource id.
Link copied to clipboard
open fun setChipStrokeWidth(chipStrokeWidth: Float)
Sets this chip's stroke width.
Link copied to clipboard
Sets this chip's stroke width using a resource id.
Link copied to clipboard
open fun setChipText(@Nullable chipText: CharSequence)
Link copied to clipboard
Link copied to clipboard
open fun setCloseIcon(@Nullable closeIcon: Drawable)
Sets this chip's close icon.
Link copied to clipboard
open fun setCloseIconContentDescription(@Nullable closeIconContentDescription: CharSequence)
Sets the content description for this chip's close icon.
Link copied to clipboard
open fun setCloseIconEnabled(closeIconEnabled: Boolean)
Link copied to clipboard
Link copied to clipboard
open fun setCloseIconEndPadding(closeIconEndPadding: Float)
Sets the end padding for this chip's close icon.
Link copied to clipboard
Sets the end padding for this chip's close icon using a resource id.
Link copied to clipboard
Sets this chip's close icon using a resource id.
Link copied to clipboard
open fun setCloseIconSize(closeIconSize: Float)
Sets this chip's close icon size.
Link copied to clipboard
Sets this chip's close icon size using a resource id.
Link copied to clipboard
open fun setCloseIconStartPadding(closeIconStartPadding: Float)
Sets the start padding for this chip's close icon.
Link copied to clipboard
Sets the start padding for this chip's close icon using a resource id.
Link copied to clipboard
open fun setCloseIconTint(@Nullable closeIconTint: ColorStateList)
Sets the tint color for this chip's close icon.
Link copied to clipboard
Sets the tint color for this chip's close icon using a resource id.
Link copied to clipboard
open fun setCloseIconVisible(closeIconVisible: Boolean)
Sets whether this chip close icon is visible.
Sets whether this chip close icon is visible using a resource id.
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Sets whether this chip should expand its bounds (if needed) to meet the minimum touch target size.
Link copied to clipboard
open fun setGravity(gravity: Int)
Link copied to clipboard
open fun setHideMotionSpec(@Nullable hideMotionSpec: MotionSpec)
Sets this chip's hide motion spec.
Link copied to clipboard
Sets this chip's hide motion spec using a resource id.
Link copied to clipboard
open fun setIconEndPadding(iconEndPadding: Float)
Sets the end padding for this chip's icon.
Link copied to clipboard
Sets the end padding for this chip's icon using a resource id.
Link copied to clipboard
open fun setIconStartPadding(iconStartPadding: Float)
Sets this chip's icon start padding.
Link copied to clipboard
Sets the start padding for this chip's icon using a resource id.
Link copied to clipboard
open fun setLayoutDirection(layoutDirection: Int)
Link copied to clipboard
open fun setLines(lines: Int)
Link copied to clipboard
open fun setMaxLines(maxLines: Int)
Link copied to clipboard
open fun setMaxWidth(@Px maxWidth: Int)
Link copied to clipboard
open fun setMinLines(minLines: Int)
Link copied to clipboard
Register a callback to be invoked when the close icon is clicked.
Link copied to clipboard
open fun setRippleColor(@Nullable rippleColor: ColorStateList)
Sets this chip's ripple color.
Link copied to clipboard
Sets this chip's ripple color using a resource id.
Link copied to clipboard
open fun setShapeAppearanceModel(@NonNull shapeAppearanceModel: ShapeAppearanceModel)
Sets the ShapeAppearanceModel that defines the shape.
Link copied to clipboard
open fun setShowMotionSpec(@Nullable showMotionSpec: MotionSpec)
Sets this chip's show motion spec.
Link copied to clipboard
Sets this chip's show motion spec using a resource id.
Link copied to clipboard
open fun setSingleLine(singleLine: Boolean)
Link copied to clipboard
Link copied to clipboard
open fun setTextAppearance(@Nullable textAppearance: TextAppearance)
Sets this chip's text appearance.
open fun setTextAppearance(resId: Int)
open fun setTextAppearance(context: Context, resId: Int)
Link copied to clipboard
Sets this chip's text appearance using a resource id.
Link copied to clipboard
open fun setTextEndPadding(textEndPadding: Float)
Sets the end padding for this chip's text.
Link copied to clipboard
Sets the end padding for this chip's text using a resource id.
Link copied to clipboard
open fun setTextSize(unit: Int, size: Float)
Link copied to clipboard
open fun setTextStartPadding(textStartPadding: Float)
Sets the start padding for this chip's text.
Link copied to clipboard
Sets the start padding for this chip's text using a resource id.
Link copied to clipboard
Returns whether this chip will expand its bounds (if needed) to meet the minimum touch target size.