MaterialCardView

public class MaterialCardView
extends CardView implements Checkable

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ androidx.cardview.widget.CardView
           ↳ com.google.android.material.card.MaterialCardView
Known Direct Subclasses
Known Indirect Subclasses


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(boolean). 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(boolean). This changes the overlay color and elevates the card to convey motion.

Note: Avoid setting setClipToOutline(boolean) to true. There is an intermediate view to clip the content, setting this will have negative performance consequences.

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(int).

Summary

Nested classes

interface MaterialCardView.OnCheckedChangeListener

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

XML attributes

MaterialCardView_android_checkable  
MaterialCardView_checkedIcon  
MaterialCardView_checkedIconTint  
MaterialCardView_rippleColor  

Inherited constants

From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

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

Public methods

void addView(View child, int index, ViewGroup.LayoutParams params)
ColorStateList getCardBackgroundColor()
Drawable getCheckedIcon()

Returns this cards's checked icon.

ColorStateList getCheckedIconTint()

Returns the ColorStateList used to tint the checked icon.

int getContentPaddingBottom()
int getContentPaddingLeft()
int getContentPaddingRight()
int getContentPaddingTop()
float getRadius()
ColorStateList getRippleColor()

Gets the ripple color for this card.

int getStrokeColor()

This method is deprecated. use getStrokeColorStateList()

ColorStateList getStrokeColorStateList()

Returns the stroke ColorStateList of this card view.

int getStrokeWidth()

Returns the stroke width of this card view.

boolean isCheckable()

Returns whether this Card is checkable.

boolean isChecked()
boolean isDragged()
void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
void removeAllViews()
void removeView(View view)
void removeViewAt(int index)
void removeViewInLayout(View view)
void removeViews(int start, int count)
void removeViewsInLayout(int start, int count)
void setBackground(Drawable drawable)
void setBackgroundDrawable(Drawable drawable)
void setCardBackgroundColor(int color)
void setCardBackgroundColor(ColorStateList color)
void setCardElevation(float elevation)
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 setCheckedIconResource(int id)

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

void setCheckedIconTint(ColorStateList checkedIconTint)

Sets this checked icon color tint using the specified ColorStateList.

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 setLayoutParams(ViewGroup.LayoutParams params)
void setMaxCardElevation(float maxCardElevation)
void setOnCheckedChangeListener(MaterialCardView.OnCheckedChangeListener listener)

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

void setPreventCornerOverlap(boolean preventCornerOverlap)
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 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 toggle()

Protected methods

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

Inherited methods

From class androidx.cardview.widget.CardView
From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource
From interface android.widget.Checkable

XML attributes

MaterialCardView_android_checkable

Related methods:

MaterialCardView_checkedIcon

Related methods:

MaterialCardView_checkedIconTint

Related methods:

MaterialCardView_rippleColor

Related methods:

Public constructors

MaterialCardView

MaterialCardView (Context context)

Parameters
context Context

MaterialCardView

MaterialCardView (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

MaterialCardView

MaterialCardView (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

getCardBackgroundColor

ColorStateList getCardBackgroundColor ()

Returns
ColorStateList

getCheckedIcon

Drawable getCheckedIcon ()

Returns this cards's checked icon.

Related XML Attributes:

Returns
Drawable

getCheckedIconTint

ColorStateList getCheckedIconTint ()

Returns the ColorStateList used to tint the checked icon.

Related XML Attributes:

Returns
ColorStateList

getContentPaddingBottom

int getContentPaddingBottom ()

Returns
int

getContentPaddingLeft

int getContentPaddingLeft ()

Returns
int

getContentPaddingRight

int getContentPaddingRight ()

Returns
int

getContentPaddingTop

int getContentPaddingTop ()

Returns
int

getRadius

float getRadius ()

Returns
float

getRippleColor

ColorStateList getRippleColor ()

Gets the ripple color for this card.

Related XML Attributes:

Returns
ColorStateList The color used for the ripple.

getStrokeColor

int getStrokeColor ()

This method is deprecated.
use getStrokeColorStateList()

Returns
int

getStrokeColorStateList

ColorStateList getStrokeColorStateList ()

Returns the stroke ColorStateList of this card view.

Returns
ColorStateList

getStrokeWidth

int getStrokeWidth ()

Returns the stroke width of this card view.

Returns
int

isCheckable

boolean isCheckable ()

Returns whether this Card is checkable.

Related XML Attributes:

Returns
boolean

isChecked

boolean isChecked ()

Returns
boolean

isDragged

boolean isDragged ()

Returns
boolean

onInitializeAccessibilityNodeInfo

void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Parameters
info AccessibilityNodeInfo

removeAllViews

void removeAllViews ()

removeView

void removeView (View view)

Parameters
view View

removeViewAt

void removeViewAt (int index)

Parameters
index int

removeViewInLayout

void removeViewInLayout (View view)

Parameters
view View

removeViews

void removeViews (int start, 
                int count)

Parameters
start int

count int

removeViewsInLayout

void removeViewsInLayout (int start, 
                int count)

Parameters
start int

count int

setBackground

void setBackground (Drawable drawable)

Parameters
drawable Drawable

setBackgroundDrawable

void setBackgroundDrawable (Drawable drawable)

Parameters
drawable Drawable

setCardBackgroundColor

void setCardBackgroundColor (int color)

Parameters
color int

setCardBackgroundColor

void setCardBackgroundColor (ColorStateList color)

Parameters
color ColorStateList

setCardElevation

void setCardElevation (float elevation)

Parameters
elevation float

setCheckable

void setCheckable (boolean checkable)

Sets whether this Card is checkable.

Related XML Attributes:

Parameters
checkable boolean: Whether this chip is checkable.

setChecked

void setChecked (boolean checked)

Parameters
checked boolean

setCheckedIcon

void setCheckedIcon (Drawable checkedIcon)

Sets this card's checked icon.

Related XML Attributes:

Parameters
checkedIcon Drawable: This card's checked icon.

setCheckedIconResource

void setCheckedIconResource (int id)

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

Related XML Attributes:

Parameters
id int: The resource id of this Card's checked icon.

setCheckedIconTint

void setCheckedIconTint (ColorStateList checkedIconTint)

Sets this checked icon color tint using the specified ColorStateList.

Related XML Attributes:

Parameters
checkedIconTint ColorStateList: The tint color of this chip's icon.

setClickable

void setClickable (boolean clickable)

Parameters
clickable boolean

setContentPadding

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

Parameters
left int

top int

right int

bottom int

setDragged

void setDragged (boolean dragged)

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

Parameters
dragged boolean: whether the card is currently being dragged or at rest.

setLayoutParams

void setLayoutParams (ViewGroup.LayoutParams params)

Parameters
params ViewGroup.LayoutParams

setMaxCardElevation

void setMaxCardElevation (float maxCardElevation)

Parameters
maxCardElevation float

setOnCheckedChangeListener

void setOnCheckedChangeListener (MaterialCardView.OnCheckedChangeListener listener)

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

Parameters
listener MaterialCardView.OnCheckedChangeListener: the callback to call on checked state change

setPreventCornerOverlap

void setPreventCornerOverlap (boolean preventCornerOverlap)

Parameters
preventCornerOverlap boolean

setRadius

void setRadius (float radius)

Parameters
radius float

setRippleColor

void setRippleColor (ColorStateList rippleColor)

Sets the ripple color for this card.

Related XML Attributes:

Parameters
rippleColor ColorStateList: Color to use for the ripple.

setRippleColorResource

void setRippleColorResource (int rippleColorResourceId)

Sets the ripple color resource for this card.

Related XML Attributes:

Parameters
rippleColorResourceId int: Color resource to use for the ripple.

setStrokeColor

void setStrokeColor (ColorStateList strokeColor)

Sets the stroke color of this card view.

Parameters
strokeColor ColorStateList: The ColorStateList of the stroke.

setStrokeColor

void setStrokeColor (int strokeColor)

Sets the stroke color of this card view.

Parameters
strokeColor int: The color of the stroke.

setStrokeWidth

void setStrokeWidth (int strokeWidth)

Sets the stroke width of this card view.

Parameters
strokeWidth int: The width in pixels of the stroke.

setUseCompatPadding

void setUseCompatPadding (boolean useCompatPadding)

Parameters
useCompatPadding boolean

toggle

void toggle ()

Protected methods

onCreateDrawableState

int[] onCreateDrawableState (int extraSpace)

Parameters
extraSpace int

Returns
int[]

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int