Android 12 Developer Preview is here! Try it out, and give us your feedback!

CardView

public class CardView
extends FrameLayout

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ androidx.cardview.widget.CardView


A FrameLayout with a rounded corner background and shadow.

CardView uses elevation property on Lollipop for shadows and falls back to a custom emulated shadow implementation on older platforms.

Due to expensive nature of rounded corner clipping, on platforms before Lollipop, CardView does not clip its children that intersect with rounded corners. Instead, it adds padding to avoid such intersection (See setPreventCornerOverlap(boolean) to change this behavior).

Before Lollipop, CardView adds padding to its content and draws shadows to that area. This padding amount is equal to maxCardElevation + (1 - cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom.

Since padding is used to offset content for shadows, you cannot set padding on CardView. Instead, you can use content padding attributes in XML or setContentPadding(int, int, int, int) in code to set the padding between the edges of the CardView and children of CardView.

Note that, if you specify exact dimensions for the CardView, because of the shadows, its content area will be different between platforms before Lollipop and after Lollipop. By using api version specific resource values, you can avoid these changes. Alternatively, If you want CardView to add inner padding on platforms Lollipop and after as well, you can call setUseCompatPadding(boolean) and pass true.

To change CardView's elevation in a backward compatible way, use setCardElevation(float). CardView will use elevation API on Lollipop and before Lollipop, it will change the shadow size. To avoid moving the View while shadow size is changing, shadow size is clamped by getMaxCardElevation(). If you want to change elevation dynamically, you should call setMaxCardElevation(float) when CardView is initialized. R.attr.cardBackgroundColor R.attr.cardCornerRadius R.attr.cardElevation R.attr.cardMaxElevation R.attr.cardUseCompatPadding R.attr.cardPreventCornerOverlap R.attr.contentPadding R.attr.contentPaddingLeft R.attr.contentPaddingTop R.attr.contentPaddingRight R.attr.contentPaddingBottom

Summary

Inherited constants

Inherited fields

Public constructors

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

Public methods

ColorStateList getCardBackgroundColor()

Returns the background color state list of the CardView.

float getCardElevation()

Returns the backward compatible elevation of the CardView.

int getContentPaddingBottom()

Returns the inner padding before the Card's bottom edge

int getContentPaddingLeft()

Returns the inner padding after the Card's left edge

int getContentPaddingRight()

Returns the inner padding before the Card's right edge

int getContentPaddingTop()

Returns the inner padding after the Card's top edge

float getMaxCardElevation()

Returns the backward compatible maximum elevation of the CardView.

boolean getPreventCornerOverlap()

Returns whether CardView should add extra padding to content to avoid overlaps with rounded corners on pre-Lollipop platforms.

float getRadius()

Returns the corner radius of the CardView.

boolean getUseCompatPadding()

Returns whether CardView will add inner padding on platforms Lollipop and after.

void setCardBackgroundColor(int color)

Updates the background color of the CardView

void setCardBackgroundColor(ColorStateList color)

Updates the background ColorStateList of the CardView

void setCardElevation(float elevation)

Updates the backward compatible elevation of the CardView.

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

Sets the padding between the Card's edges and the children of CardView.

void setMaxCardElevation(float maxElevation)

Updates the backward compatible maximum elevation of the CardView.

void setMinimumHeight(int minHeight)
void setMinimumWidth(int minWidth)
void setPadding(int left, int top, int right, int bottom)
void setPaddingRelative(int start, int top, int end, int bottom)
void setPreventCornerOverlap(boolean preventCornerOverlap)

On pre-Lollipop platforms, CardView does not clip the bounds of the Card for the rounded corners.

void setRadius(float radius)

Updates the corner radius of the CardView.

void setUseCompatPadding(boolean useCompatPadding)

CardView adds additional padding to draw shadows on platforms before Lollipop.

Protected methods

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Inherited methods

Public constructors

CardView

public CardView (Context context)

Parameters
context Context

CardView

public CardView (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

CardView

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

getCardBackgroundColor

public ColorStateList getCardBackgroundColor ()

Returns the background color state list of the CardView.

Returns
ColorStateList The background color state list of the CardView.

getCardElevation

public float getCardElevation ()

Returns the backward compatible elevation of the CardView.

Returns
float Elevation of the CardView

getContentPaddingBottom

public int getContentPaddingBottom ()

Returns the inner padding before the Card's bottom edge

Returns
int the inner padding before the Card's bottom edge

getContentPaddingLeft

public int getContentPaddingLeft ()

Returns the inner padding after the Card's left edge

Returns
int the inner padding after the Card's left edge

getContentPaddingRight

public int getContentPaddingRight ()