MaterialCardView

public class MaterialCardView implements Shapeable

Known direct subclasses
CircularRevealCardView

A CircularRevealWidget wrapper for MaterialCardView.


Provides a Material card.

This class supplies Material styles for the card in the constructor. The widget will display the correct default Material styles without the use of a style flag.

Stroke width can be set using the strokeWidth attribute. Set the stroke color using the strokeColor attribute. Without a strokeColor, the card will not render a stroked border, regardless of the strokeWidth value.

Cards implement Checkable, a default way to switch to android:checked_state is not provided. Clients have to call setChecked. This shows the app:checkedIcon and changes the overlay color.

Cards also have a custom state meant to be used when a card is draggable app:dragged_state. It's used by calling setDragged. This changes the overlay color and elevates the card to convey motion.

Note: The actual view hierarchy present under MaterialCardView is NOT guaranteed to match the view hierarchy as written in XML. As a result, calls to getParent() on children of the MaterialCardView, will not return the MaterialCardView itself, but rather an intermediate View. If you need to access a MaterialCardView directly, set an android:id and use findViewById.

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

Summary

Nested types

@Retention(value = RetentionPolicy.SOURCE)
public annotation MaterialCardView.CheckedIconGravity

Positions the icon can be set to.

Interface definition for a callback to be invoked when the card checked state changes.

Constants

static final int

Gravity used to position the checked icon at the bottom|end of the Card.

static final int

Gravity used to position the checked icon at the bottom|start of the Card.

static final int

Gravity used to position the checked icon at the top|end of the Card.

static final int

Gravity used to position the checked icon at the top|start of the Card.

Public constructors

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

Public methods

ColorStateList
ColorStateList

Sets the ripple color for this card.

Drawable

Returns this cards's checked icon.

int

Gets the checked icon gravity for this card

int
int
ColorStateList

Returns the android.content.res.ColorStateList used to tint the checked icon.

int
int
int
int
float

Returns the interpolation on the Shape Path of the card.

float
ColorStateList

Gets the ripple color for this card.

ShapeAppearanceModel

Due to limitations in the current implementation, if you modify the returned object call setShapeAppearanceModel again with the modified value to propagate the required changes.

int

This method is deprecated.

use getStrokeColorStateList

ColorStateList

Returns the stroke ColorStateList of this card view.

int

Returns the stroke width of this card view.

boolean

Returns whether this Card is checkable.

boolean
boolean
void
onInitializeAccessibilityEvent(AccessibilityEvent accessibilityEvent)
void
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
void
setBackground(Drawable drawable)
void
setBackgroundDrawable(Drawable drawable)
void
setCardBackgroundColor(ColorStateList color)
void
void
setCardElevation(float elevation)
void
setCardForegroundColor(ColorStateList foregroundColor)

Sets the foreground color for this card.

void
setCheckable(boolean checkable)

Sets whether this Card is checkable.

void
setChecked(boolean checked)
void
setCheckedIcon(Drawable checkedIcon)

Sets this card's checked icon.

void

Sets the checked icon gravity for this card

void
setCheckedIconMargin(int checkedIconMargin)
void
setCheckedIconMarginResource(int checkedIconMarginResId)

Sets the margin of the checked icon using a resource id.

void

Sets this card's checked icon using a resource id.

void
setCheckedIconSize(int checkedIconSize)

Sets the size of the checked icon

void
setCheckedIconSizeResource(int checkedIconSizeResId)

Sets the size of the checked icon using a resource id.

void
setCheckedIconTint(ColorStateList checkedIconTint)

Sets this checked icon color tint using the specified .

void
setClickable(boolean clickable)
void
setContentPadding(int left, int top, int right, int bottom)
void
setDragged(boolean dragged)

Call this when the Card is being dragged to apply the right color and elevation changes.

void
setMaxCardElevation(float maxCardElevation)
void

Register a callback to be invoked when the checked state of this Card changes.

void
setPreventCornerOverlap(boolean preventCornerOverlap)
void
setProgress(float progress)

Sets the interpolation on the Shape Path of the card.

void
setRadius(float radius)
void
setRippleColor(ColorStateList rippleColor)

Sets the ripple color for this card.

void
setRippleColorResource(int rippleColorResourceId)

Sets the ripple color resource for this card.

void

Sets the ShapeAppearanceModel that defines the shape.

void
setStrokeColor(ColorStateList strokeColor)

Sets the stroke color of this card view.

void
setStrokeColor(int strokeColor)

Sets the stroke color of this card view.

void
setStrokeWidth(int strokeWidth)

Sets the stroke width of this card view.

void
setUseCompatPadding(boolean useCompatPadding)
void

Protected methods

void
int[]
onCreateDrawableState(int extraSpace)
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Constants

CHECKED_ICON_GRAVITY_BOTTOM_END

public static final int CHECKED_ICON_GRAVITY_BOTTOM_END

Gravity used to position the checked icon at the bottom|end of the Card.

CHECKED_ICON_GRAVITY_BOTTOM_START

public static final int CHECKED_ICON_GRAVITY_BOTTOM_START

Gravity used to position the checked icon at the bottom|start of the Card.

CHECKED_ICON_GRAVITY_TOP_END

public static final int CHECKED_ICON_GRAVITY_TOP_END

Gravity used to position the checked icon at the top|end of the Card.

CHECKED_ICON_GRAVITY_TOP_START

public static final int CHECKED_ICON_GRAVITY_TOP_START

Gravity used to position the checked icon at the top|start of the Card.

Public constructors

MaterialCardView

public MaterialCardView(Context context)

MaterialCardView

public MaterialCardView(Context context, AttributeSet attrs)

MaterialCardView

public MaterialCardView(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

getCardBackgroundColor

public ColorStateList getCardBackgroundColor()

getCardForegroundColor

public ColorStateList getCardForegroundColor()

Sets the ripple color for this card.

ref com.google.android.material.R.styleable#MaterialCardView_cardForegroundColor

getCheckedIcon

public Drawable getCheckedIcon()

Returns this cards's checked icon.

ref com.google.android.material.R.styleable#MaterialCardView_checkedIcon

See also
setCheckedIcon

getCheckedIconGravity

@MaterialCardView.CheckedIconGravity
public int getCheckedIconGravity()

Gets the checked icon gravity for this card

ref com.google.android.material.R.styleable#MaterialCard_checkedIconGravity

Returns
int

Checked Icon gravity of the card.

getCheckedIconMargin

public int getCheckedIconMargin()

getCheckedIconSize

public int getCheckedIconSize()

getCheckedIconTint

public ColorStateList getCheckedIconTint()

Returns the android.content.res.ColorStateList used to tint the checked icon.

ref com.google.android.material.R.styleable#MaterialCardView_checkedIconTint

getContentPaddingBottom

public int getContentPaddingBottom()

getContentPaddingLeft

public int getContentPaddingLeft()

getContentPaddingRight

public int getContentPaddingRight()

getContentPaddingTop

public int getContentPaddingTop()

getProgress

public float getProgress()

Returns the interpolation on the Shape Path of the card.

getRadius

public float getRadius()

getRippleColor

public ColorStateList getRippleColor()

Gets the ripple color for this card.

ref com.google.android.material.R.styleable#MaterialCardView_rippleColor

Returns
ColorStateList

The color used for the ripple.

getShapeAppearanceModel

public ShapeAppearanceModel getShapeAppearanceModel()

Due to limitations in the current implementation, if you modify the returned object call setShapeAppearanceModel again with the modified value to propagate the required changes.

getStrokeColor

public int getStrokeColor()

getStrokeColorStateList

public ColorStateList getStrokeColorStateList()

Returns the stroke ColorStateList of this card view.

getStrokeWidth

public int getStrokeWidth()

Returns the stroke width of this card view.

isCheckable

public boolean isCheckable()

Returns whether this Card is checkable.

ref com.google.android.material.R.styleable#MaterialCardView_android_checkable

See also
setCheckable

isChecked

public boolean isChecked()

isDragged

public boolean isDragged()

onInitializeAccessibilityEvent

public void onInitializeAccessibilityEvent(AccessibilityEvent accessibilityEvent)

onInitializeAccessibilityNodeInfo

public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)

setBackground

public void setBackground(Drawable drawable)

setBackgroundDrawable

public void setBackgroundDrawable(Drawable drawable)

setCardBackgroundColor

public void setCardBackgroundColor(ColorStateList color)

setCardBackgroundColor

public void setCardBackgroundColor(int color)

setCardElevation

public void setCardElevation(float elevation)

setCardForegroundColor

public void setCardForegroundColor(ColorStateList foregroundColor)

Sets the foreground color for this card.

ref com.google.android.material.R.styleable#MaterialCardView_cardForegroundColor

Parameters
ColorStateList foregroundColor

Color to use for the foreground.

setCheckable

public void setCheckable(boolean checkable)

Sets whether this Card is checkable.

ref com.google.android.material.R.styleable#MaterialCardView_android_checkable

Parameters
boolean checkable

Whether this chip is checkable.

setChecked

public void setChecked(boolean checked)

setCheckedIcon

public void setCheckedIcon(Drawable checkedIcon)

Sets this card's checked icon.

ref com.google.android.material.R.styleable#MaterialCardView_checkedIcon

Parameters
Drawable checkedIcon

This card's checked icon.

setCheckedIconGravity

public void setCheckedIconGravity(
    @MaterialCardView.CheckedIconGravity int checkedIconGravity
)

Sets the checked icon gravity for this card

ref com.google.android.material.R.styleable#MaterialCard_checkedIconGravity

Parameters
@MaterialCardView.CheckedIconGravity int checkedIconGravity

checked icon gravity for this card

setCheckedIconMargin

public void setCheckedIconMargin(int checkedIconMargin)

setCheckedIconMarginResource

public void setCheckedIconMarginResource(int checkedIconMarginResId)

Sets the margin of the checked icon using a resource id.

ref com.google.android.material.R.styleable#MaterialCardView_checkedIconMargin

Parameters
int checkedIconMarginResId

The resource id of this Card's checked icon margin

setCheckedIconResource

public void setCheckedIconResource(int id)

Sets this card's checked icon using a resource id.

ref com.google.android.material.R.styleable#MaterialCardView_checkedIcon

Parameters
int id

The resource id of this Card's checked icon.

setCheckedIconSize

public void setCheckedIconSize(int checkedIconSize)

Sets the size of the checked icon

ref com.google.android.material.R.styleable#MaterialCardView_checkedIconSize

Parameters
int checkedIconSize

checked icon size

setCheckedIconSizeResource

public void setCheckedIconSizeResource(int checkedIconSizeResId)

Sets the size of the checked icon using a resource id.

ref com.google.android.material.R.styleable#MaterialCardView_checkedIconSize

Parameters
int checkedIconSizeResId

The resource id of this Card's checked icon size

setCheckedIconTint

public void setCheckedIconTint(ColorStateList checkedIconTint)

Sets this checked icon color tint using the specified .

ref com.google.android.material.R.styleable#MaterialCardView_checkedIconTint

Parameters
ColorStateList checkedIconTint

The tint color of this chip's icon.

setClickable

public void setClickable(boolean clickable)

setContentPadding

public void setContentPadding(int left, int top, int right, int bottom)

setDragged

public void setDragged(boolean dragged)

Call this when the Card is being dragged to apply the right color and elevation changes.

Parameters
boolean dragged

whether the card is currently being dragged or at rest.

setMaxCardElevation

public void setMaxCardElevation(float maxCardElevation)

setOnCheckedChangeListener

public void setOnCheckedChangeListener(
    MaterialCardView.OnCheckedChangeListener listener
)

Register a callback to be invoked when the checked state of this Card changes.

Parameters
MaterialCardView.OnCheckedChangeListener listener

the callback to call on checked state change

setPreventCornerOverlap

public void setPreventCornerOverlap(boolean preventCornerOverlap)

setProgress

public void setProgress(float progress)

Sets the interpolation on the Shape Path of the card. Useful for animations.

setRadius

public void setRadius(float radius)

setRippleColor

public void setRippleColor(ColorStateList rippleColor)

Sets the ripple color for this card.

ref com.google.android.material.R.styleable#MaterialCardView_rippleColor

Parameters
ColorStateList rippleColor

Color to use for the ripple.

setRippleColorResource

public void setRippleColorResource(int rippleColorResourceId)

Sets the ripple color resource for this card.

ref com.google.android.material.R.styleable#MaterialCardView_rippleColor

Parameters
int rippleColorResourceId

Color resource to use for the ripple.

setShapeAppearanceModel

public void setShapeAppearanceModel(ShapeAppearanceModel shapeAppearanceModel)

Sets the ShapeAppearanceModel that defines the shape.

setStrokeColor

public void setStrokeColor(ColorStateList strokeColor)

Sets the stroke color of this card view.

Parameters
ColorStateList strokeColor

The ColorStateList of the stroke.

setStrokeColor

public void setStrokeColor(int strokeColor)

Sets the stroke color of this card view.

Parameters
int strokeColor

The color of the stroke.

setStrokeWidth

public void setStrokeWidth(int strokeWidth)

Sets the stroke width of this card view.

Parameters
int strokeWidth

The width in pixels of the stroke.

setUseCompatPadding

public void setUseCompatPadding(boolean useCompatPadding)

toggle

public void toggle()

Protected methods

onAttachedToWindow

protected void onAttachedToWindow()

onCreateDrawableState

protected int[] onCreateDrawableState(int extraSpace)

onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)