Android APIs
public class

CardView

extends FrameLayout
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.support.v7.widget.CardView

Class Overview

A FrameLayout with a rounded corner background and shadow.

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

Due to expensive nature of rounded corner clipping, on platforms before L, 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 L, 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 Card 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 L and after L. By using api version specific resource values, you can avoid these changes. Alternatively, If you want CardView to add inner padding on platforms L and after as well, you can set setUseCompatPadding(boolean) to true.

To change CardView's elevation in a backward compatible way, use setCardElevation(float). CardView will use elevation API on L and before L, 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.

Summary

XML Attributes
Attribute Name Related Method Description
android.support.v7.cardview:cardBackgroundColor setCardBackgroundColor(int) Background color for CardView. 
android.support.v7.cardview:cardCornerRadius setRadius(float) Corner radius for CardView. 
android.support.v7.cardview:cardElevation setMaxCardElevation(float) Elevation for CardView. 
android.support.v7.cardview:cardMaxElevation Maximum Elevation for CardView. 
android.support.v7.cardview:cardPreventCornerOverlap setPreventCornerOverlap(boolean) Add padding to CardView on v20 and before to prevent intersections between the Card content and rounded corners. 
android.support.v7.cardview:cardUseCompatPadding setUseCompatPadding(boolean) Add padding in API v21+ as well to have the same measurements with previous versions. 
android.support.v7.cardview:contentPadding setContentPadding(int,int,int,int) Inner padding between the edges of the Card and children of the CardView. 
android.support.v7.cardview:contentPaddingBottom setContentPadding(int,int,int,int) Inner padding between the bottom edge of the Card and children of the CardView. 
android.support.v7.cardview:contentPaddingLeft setContentPadding(int,int,int,int) Inner padding between the left edge of the Card and children of the CardView. 
android.support.v7.cardview:contentPaddingRight setContentPadding(int,int,int,int) Inner padding between the right edge of the Card and children of the CardView. 
android.support.v7.cardview:contentPaddingTop setContentPadding(int,int,int,int) Inner padding between the top edge of the Card and children of the CardView. 
[Expand]
Inherited XML Attributes
From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Constants
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
CardView(Context context)
CardView(Context context, AttributeSet attrs)
CardView(Context context, AttributeSet attrs, int defStyleAttr)
Public Methods
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 elevation of the CardView.
boolean getPreventCornerOverlap()
Returns whether CardView should add extra padding to content to avoid overlaps with rounded corners on API versions 20 and below.
float getRadius()
Returns the corner radius of the CardView.
boolean getUseCompatPadding()
Returns whether CardView will add inner padding on platforms L and after.
void setCardBackgroundColor(int color)
Updates the background color of the CardView
void setCardElevation(float radius)
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 radius)
Updates the backward compatible elevation of the CardView.
void setPadding(int left, int top, int right, int bottom)
Sets the padding.
void setPaddingRelative(int start, int top, int end, int bottom)
Sets the relative padding.
void setPreventCornerOverlap(boolean preventCornerOverlap)
On API 20 and before, 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 L.
Protected Methods
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

[Expand]
Inherited Methods
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

XML Attributes

android.support.v7.cardview:cardBackgroundColor

Background color for CardView.

Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This is a private symbol.

Related Methods

android.support.v7.cardview:cardCornerRadius

Corner radius for CardView.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This is a private symbol.

Related Methods

android.support.v7.cardview:cardElevation

Elevation for CardView.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This is a private symbol.

Related Methods

android.support.v7.cardview:cardMaxElevation

Maximum Elevation for CardView.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This is a private symbol.

Related Methods

android.support.v7.cardview:cardPreventCornerOverlap

Add padding to CardView on v20 and before to prevent intersections between the Card content and rounded corners.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This is a private symbol.

android.support.v7.cardview:cardUseCompatPadding

Add padding in API v21+ as well to have the same measurements with previous versions.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This is a private symbol.

android.support.v7.cardview:contentPadding

Inner padding between the edges of the Card and children of the CardView.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This is a private symbol.

android.support.v7.cardview:contentPaddingBottom

Inner padding between the bottom edge of the Card and children of the CardView.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This is a private symbol.

android.support.v7.cardview:contentPaddingLeft

Inner padding between the left edge of the Card and children of the CardView.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This is a private symbol.

android.support.v7.cardview:contentPaddingRight

Inner padding between the right edge of the Card and children of the CardView.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This is a private symbol.

android.support.v7.cardview:contentPaddingTop

Inner padding between the top edge of the Card and children of the CardView.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This is a private symbol.

Public Constructors

public CardView (Context context)

public CardView (Context context, AttributeSet attrs)

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

Public Methods

public float getCardElevation ()

Returns the backward compatible elevation of the CardView.

Returns
  • Elevation of the CardView

public int getContentPaddingBottom ()

Returns the inner padding before the Card's bottom edge

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

public int getContentPaddingLeft ()

Returns the inner padding after the Card's left edge

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

public int getContentPaddingRight ()

Returns the inner padding before the Card's right edge

Returns
  • the inner padding before the Card's right edge

public int getContentPaddingTop ()

Returns the inner padding after the Card's top edge

Returns
  • the inner padding after the Card's top edge

public float getMaxCardElevation ()

Returns the backward compatible elevation of the CardView.

Returns
  • Elevation of the CardView

public boolean getPreventCornerOverlap ()

Returns whether CardView should add extra padding to content to avoid overlaps with rounded corners on API versions 20 and below.

Returns
  • True if CardView prevents overlaps with rounded corners on platforms before L. Default value is true.

public float getRadius ()

Returns the corner radius of the CardView.

Returns
  • Corner radius of the CardView
See Also

public boolean getUseCompatPadding ()

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

Returns
  • True CardView adds inner padding on platforms L and after to have same dimensions with platforms before L.

public void setCardBackgroundColor (int color)

Updates the background color of the CardView

Parameters
color The new color to set for the card background

public void setCardElevation (float radius)

Updates the backward compatible elevation of the CardView.

Parameters
radius The backward compatible elevation in pixels.

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

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

Depending on platform version or getUseCompatPadding() settings, CardView may update these values before calling setPadding(int, int, int, int).

Parameters
left The left padding in pixels
top The top padding in pixels
right The right padding in pixels
bottom The bottom padding in pixels

public void setMaxCardElevation (float radius)

Updates the backward compatible elevation of the CardView.

Calling this method has no effect if device OS version is L or newer and getUseCompatPadding() is false.

Parameters
radius The backward compatible elevation in pixels.

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

Sets the padding. The view may add on the space required to display the scrollbars, depending on the style and visibility of the scrollbars. So the values returned from getPaddingLeft(), getPaddingTop(), getPaddingRight() and getPaddingBottom() may be different from the values set in this call.

Parameters
left the left padding in pixels
top the top padding in pixels
right the right padding in pixels
bottom the bottom padding in pixels

public void setPaddingRelative (int start, int top, int end, int bottom)

Sets the relative padding. The view may add on the space required to display the scrollbars, depending on the style and visibility of the scrollbars. So the values returned from getPaddingStart(), getPaddingTop(), getPaddingEnd() and getPaddingBottom() may be different from the values set in this call.

Parameters
start the start padding in pixels
top the top padding in pixels
end the end padding in pixels
bottom the bottom padding in pixels

public void setPreventCornerOverlap (boolean preventCornerOverlap)

On API 20 and before, CardView does not clip the bounds of the Card for the rounded corners. Instead, it adds padding to content so that it won't overlap with the rounded corners. You can disable this behavior by setting this field to false.

Setting this value on API 21 and above does not have any effect unless you have enabled compatibility padding.

Parameters
preventCornerOverlap Whether CardView should add extra padding to content to avoid overlaps with the CardView corners.

public void setRadius (float radius)

Updates the corner radius of the CardView.

Parameters
radius The radius in pixels of the corners of the rectangle shape
See Also

public void setUseCompatPadding (boolean useCompatPadding)

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

This may cause Cards to have different sizes between L and before L. If you need to align CardView with other Views, you may need api version specific dimension resources to account for the changes. As an alternative, you can set this flag to true and CardView will add the same padding values on platforms L and after.

Since setting this flag to true adds unnecessary gaps in the UI, default value is false.

Parameters
useCompatPadding True if CardView should add padding for the shadows on platforms L and above.

Protected Methods

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overriden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.