MaterialCardView
public
class
MaterialCardView
extends CardView
implements
Checkable,
Shapeable
java.lang.Object | |||||
↳ | android.view.View | ||||
↳ | android.view.ViewGroup | ||||
↳ | android.widget.FrameLayout | ||||
↳ | androidx.cardview.widget.CardView | ||||
↳ | com.google.android.material.card.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(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: 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 View.findViewById(int)
.
For more information, see the component developer guidance and design guidelines.
Summary
Nested classes | |
---|---|
@interface |
MaterialCardView.CheckedIconGravity
Positions the icon can be set to. |
interface |
MaterialCardView.OnCheckedChangeListener
Interface definition for a callback to be invoked when the card checked state changes. |
XML attributes | |
---|---|
MaterialCardView_android_checkable |
|
MaterialCardView_cardForegroundColor |
|
MaterialCardView_checkedIcon |
|
MaterialCardView_checkedIconMargin |
|
MaterialCardView_checkedIconSize |
|
MaterialCardView_checkedIconTint |
|
MaterialCardView_rippleColor |
Constants | |
---|---|
int |
CHECKED_ICON_GRAVITY_BOTTOM_END
Gravity used to position the checked icon at the bottom|end of the Card. |
int |
CHECKED_ICON_GRAVITY_BOTTOM_START
Gravity used to position the checked icon at the bottom|start of the Card. |
int |
CHECKED_ICON_GRAVITY_TOP_END
Gravity used to position the checked icon at the top|end of the Card. |
int |
CHECKED_ICON_GRAVITY_TOP_START
Gravity used to position the checked icon at the top|start of the Card. |
Inherited constants |
---|
Inherited fields |
---|
Public constructors | |
---|---|
MaterialCardView(Context context)
|
|
MaterialCardView(Context context, AttributeSet attrs)
|
|
MaterialCardView(Context context, AttributeSet attrs, int defStyleAttr)
|
Public methods | |
---|---|
ColorStateList
|
getCardBackgroundColor()
|
ColorStateList
|
getCardForegroundColor()
Sets the ripple color for this card. |
Drawable
|
getCheckedIcon()
Returns this cards's checked icon. |
int
|
getCheckedIconGravity()
Gets the checked icon gravity for this card |
int
|
getCheckedIconMargin()
|
int
|
getCheckedIconSize()
|
ColorStateList
|
getCheckedIconTint()
Returns the |
int
|
getContentPaddingBottom()
|
int
|
getContentPaddingLeft()
|
int
|
getContentPaddingRight()
|
int
|
getContentPaddingTop()
|
float
|
getProgress()
Returns the interpolation on the Shape Path of the card. |
float
|
getRadius()
|
ColorStateList
|
getRippleColor()
Gets the ripple color for this card. |
ShapeAppearanceModel
|
getShapeAppearanceModel()
Due to limitations in the current implementation, if you modify the returned object
call |
int
|
getStrokeColor()
This method is deprecated.
use |
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
|
onInitializeAccessibilityEvent(AccessibilityEvent accessibilityEvent)
|
void
|
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
|
void
|
setBackground(Drawable drawable)
|
void
|
setBackgroundDrawable(Drawable drawable)
|
void
|
setCardBackgroundColor(int color)
|
void
|
setCardBackgroundColor(ColorStateList color)
|
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
|
setCheckedIconGravity(int checkedIconGravity)
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
|
setCheckedIconResource(int id)
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
|
setOnCheckedChangeListener(MaterialCardView.OnCheckedChangeListener listener)
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
|
setShapeAppearanceModel(ShapeAppearanceModel shapeAppearanceModel)
Sets the |
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 | |
---|---|
void
|
onAttachedToWindow()
|
int[]
|
onCreateDrawableState(int extraSpace)
|
void
|
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
Inherited methods | |
---|---|
XML attributes
MaterialCardView_android_checkable
Related methods:
MaterialCardView_cardForegroundColor
Related methods:
MaterialCardView_checkedIcon
Related methods:
MaterialCardView_checkedIconMargin
Related methods:
MaterialCardView_checkedIconSize
Related methods:
MaterialCardView_checkedIconTint
Related methods:
MaterialCardView_rippleColor
Related methods:
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.
Constant Value: 8388693 (0x00800055)
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.
Constant Value: 8388691 (0x00800053)
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.
Constant Value: 8388661 (0x00800035)
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.
Constant Value: 8388659 (0x00800033)
Public constructors
MaterialCardView
public MaterialCardView (Context context)
Parameters | |
---|---|
context |
Context |
MaterialCardView
public MaterialCardView (Context context, AttributeSet attrs)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
MaterialCardView
public MaterialCardView (Context context, AttributeSet attrs, int defStyleAttr)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
Public methods
getCardBackgroundColor
public ColorStateList getCardBackgroundColor ()
Returns | |
---|---|
ColorStateList |
getCardForegroundColor
public ColorStateList getCardForegroundColor ()
Sets the ripple color for this card.
Related XML Attributes:
Returns | |
---|---|
ColorStateList |
See also:
getCheckedIcon
public Drawable getCheckedIcon ()
Returns this cards's checked icon.
Related XML Attributes:
Returns | |
---|---|
Drawable |
See also:
getCheckedIconGravity
public int getCheckedIconGravity ()
Gets the checked icon gravity for this card
Returns | |
---|---|
int |
Checked Icon gravity of the card. |
See also:
getCheckedIconMargin
public int getCheckedIconMargin ()
Returns | |
---|---|
int |
getCheckedIconSize
public int getCheckedIconSize ()
Returns | |
---|---|
int |
getCheckedIconTint
public ColorStateList getCheckedIconTint ()
Returns the ColorStateList
used to tint the checked icon.
Related XML Attributes:
Returns | |
---|---|
ColorStateList |
See also:
getContentPaddingBottom
public int getContentPaddingBottom ()
Returns | |
---|---|
int |
getContentPaddingLeft
public int getContentPaddingLeft ()
Returns | |
---|---|
int |
getContentPaddingRight
public int getContentPaddingRight ()
Returns | |
---|---|
int |
getContentPaddingTop
public int getContentPaddingTop ()
Returns | |
---|---|
int |
getProgress
public float getProgress ()
Returns the interpolation on the Shape Path of the card.
Returns | |
---|---|
float |
getRadius
public float getRadius ()
Returns | |
---|---|
float |
getRippleColor
public ColorStateList getRippleColor ()
Gets the ripple color for this card.
Related XML Attributes:
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(ShapeAppearanceModel)
again with the modified value
to propagate the required changes.
Returns | |
---|---|
ShapeAppearanceModel |
getStrokeColor
public int getStrokeColor ()
This method is deprecated.
use getStrokeColorStateList()
Returns | |
---|---|
int |
getStrokeColorStateList
public ColorStateList getStrokeColorStateList ()
Returns the stroke ColorStateList of this card view.
Returns | |
---|---|
ColorStateList |
getStrokeWidth
public int getStrokeWidth ()
Returns the stroke width of this card view.
Returns | |
---|---|
int |
isCheckable
public boolean isCheckable ()
Returns whether this Card is checkable.
Related XML Attributes:
Returns | |
---|---|
boolean |
See also:
isChecked
public boolean isChecked ()
Returns | |
---|---|
boolean |
isDragged
public boolean isDragged ()
Returns | |
---|---|
boolean |
onInitializeAccessibilityEvent
public void onInitializeAccessibilityEvent (AccessibilityEvent accessibilityEvent)
Parameters | |
---|---|
accessibilityEvent |
AccessibilityEvent |
onInitializeAccessibilityNodeInfo
public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)
Parameters | |
---|---|
info |
AccessibilityNodeInfo |
setBackground
public void setBackground (Drawable drawable)
Parameters | |
---|---|
drawable |
Drawable |
setBackgroundDrawable
public void setBackgroundDrawable (Drawable drawable)
Parameters | |
---|---|
drawable |
Drawable |
setCardBackgroundColor
public void setCardBackgroundColor (int color)
Parameters | |
---|---|
color |
int |
setCardBackgroundColor
public void setCardBackgroundColor (ColorStateList color)
Parameters | |
---|---|
color |
ColorStateList |
setCardElevation
public void setCardElevation (float elevation)
Parameters | |
---|---|
elevation |
float |
setCardForegroundColor
public void setCardForegroundColor (ColorStateList foregroundColor)
Sets the foreground color for this card.
Related XML Attributes:
Parameters | |
---|---|
foregroundColor |
ColorStateList : Color to use for the foreground. |
See also:
setCheckable
public void setCheckable (boolean checkable)
Sets whether this Card is checkable.
Related XML Attributes:
Parameters | |
---|---|
checkable |
boolean : Whether this chip is checkable. |
setChecked
public void setChecked (boolean checked)
Parameters | |
---|---|
checked |
boolean |
setCheckedIcon
public void setCheckedIcon (Drawable checkedIcon)
Sets this card's checked icon.
Related XML Attributes:
Parameters | |
---|---|
checkedIcon |
Drawable : This card's checked icon. |
setCheckedIconGravity
public void setCheckedIconGravity (int checkedIconGravity)
Sets the checked icon gravity for this card
Parameters | |
---|---|
checkedIconGravity |
int : checked icon gravity for this card |
See also:
setCheckedIconMargin
public void setCheckedIconMargin (int checkedIconMargin)
Parameters | |
---|---|
checkedIconMargin |
int |
setCheckedIconMarginResource
public void setCheckedIconMarginResource (int checkedIconMarginResId)
Sets the margin of the checked icon using a resource id.
Related XML Attributes:
Parameters | |
---|---|
checkedIconMarginResId |
int : 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.
Related XML Attributes:
Parameters | |
---|---|
id |
int : The resource id of this Card's checked icon. |
setCheckedIconSize
public void setCheckedIconSize (int checkedIconSize)
Sets the size of the checked icon
Related XML Attributes:
Parameters | |
---|---|
checkedIconSize |
int : checked icon size |
setCheckedIconSizeResource
public void setCheckedIconSizeResource (int checkedIconSizeResId)
Sets the size of the checked icon using a resource id.
Related XML Attributes:
Parameters | |
---|---|
checkedIconSizeResId |
int : 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 ColorStateList
.
Related XML Attributes:
Parameters | |
---|---|
checkedIconTint |
ColorStateList : The tint color of this chip's icon. |
setClickable
public void setClickable (boolean clickable)
Parameters | |
---|---|
clickable |
boolean |
setContentPadding
public void setContentPadding (int left, int top, int right, int bottom)
Parameters | |
---|---|
left |
int |
top |
int |
right |
int |
bottom |
int |
setDragged
public 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.
|
setMaxCardElevation
public void setMaxCardElevation (float maxCardElevation)
Parameters | |
---|---|
maxCardElevation |
float |
setOnCheckedChangeListener
public 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
public void setPreventCornerOverlap (boolean preventCornerOverlap)
Parameters | |
---|---|
preventCornerOverlap |
boolean |
setProgress
public void setProgress (float progress)
Sets the interpolation on the Shape Path of the card. Useful for animations.
Parameters | |
---|---|
progress |
float |
setRadius
public void setRadius (float radius)
Parameters | |
---|---|
radius |
float |
setRippleColor
public void setRippleColor (ColorStateList rippleColor)
Sets the ripple color for this card.
Related XML Attributes:
Parameters | |
---|---|
rippleColor |
ColorStateList : Color to use for the ripple. |
setRippleColorResource
public 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. |
setShapeAppearanceModel
public void setShapeAppearanceModel (ShapeAppearanceModel shapeAppearanceModel)
Sets the ShapeAppearanceModel
that defines the shape.
Parameters | |
---|---|
shapeAppearanceModel |
ShapeAppearanceModel |
setStrokeColor
public void setStrokeColor (ColorStateList strokeColor)
Sets the stroke color of this card view.
Parameters | |
---|---|
strokeColor |
ColorStateList : The ColorStateList of the stroke.
|
setStrokeColor
public void setStrokeColor (int strokeColor)
Sets the stroke color of this card view.
Parameters | |
---|---|
strokeColor |
int : The color of the stroke.
|
setStrokeWidth
public void setStrokeWidth (int strokeWidth)
Sets the stroke width of this card view.
Parameters | |
---|---|
strokeWidth |
int : The width in pixels of the stroke.
|
setUseCompatPadding
public void setUseCompatPadding (boolean useCompatPadding)
Parameters | |
---|---|
useCompatPadding |
boolean |
toggle
public void toggle ()
Protected methods
onAttachedToWindow
protected void onAttachedToWindow ()
onCreateDrawableState
protected int[] onCreateDrawableState (int extraSpace)
Parameters | |
---|---|
extraSpace |
int |
Returns | |
---|---|
int[] |
onMeasure
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
Parameters | |
---|---|
widthMeasureSpec |
int |
heightMeasureSpec |
int |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2023-12-15 UTC.