Skip to content

Most visited

Recently visited

navigation

CardView

public class CardView
extends FrameLayout

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.support.v7.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.

Summary

XML attributes

android.support.v7.cardview:cardBackgroundColor Background color for CardView. 
android.support.v7.cardview:cardCornerRadius Corner radius for CardView. 
android.support.v7.cardview:cardElevation Elevation for CardView. 
android.support.v7.cardview:cardMaxElevation Maximum Elevation for CardView. 
android.support.v7.cardview:cardPreventCornerOverlap Add padding to CardView on v20 and before to prevent intersections between the Card content and rounded corners. 
android.support.v7.cardview:cardUseCompatPadding Add padding in API v21+ as well to have the same measurements with previous versions. 
android.support.v7.cardview:contentPadding Inner padding between the edges of the Card and children of the CardView. 
android.support.v7.cardview:contentPaddingBottom Inner padding between the bottom edge of the Card and children of the CardView. 
android.support.v7.cardview:contentPaddingLeft Inner padding between the left edge of the Card and children of the CardView. 
android.support.v7.cardview:contentPaddingRight Inner padding between the right edge of the Card and children of the CardView. 
android.support.v7.cardview:contentPaddingTop Inner padding between the top edge of the Card and children of the CardView. 

Inherited XML attributes

From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View

Inherited constants

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

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

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)

Sets the minimum height of the view.

void setMinimumWidth(int minWidth)

Sets the minimum width of the view.

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 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)

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

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.

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

Related methods:

android.support.v7.cardview:cardCornerRadius

Corner radius for CardView.

May 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), and mm (millimeters).

Related methods:

android.support.v7.cardview:cardElevation

Elevation for CardView.

May 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), and mm (millimeters).

Related methods:

android.support.v7.cardview:cardMaxElevation

Maximum Elevation for CardView.

May 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), and mm (millimeters).

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.

May be a boolean value, such as "true" or "false".

Related methods:

android.support.v7.cardview:cardUseCompatPadding

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

May be a boolean value, such as "true" or "false".

Related methods:

android.support.v7.cardview:contentPadding

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

May 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), and mm (millimeters).

Related methods:

android.support.v7.cardview:contentPaddingBottom

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

May 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), and mm (millimeters).

Related methods:

android.support.v7.cardview:contentPaddingLeft

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

May 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), and mm (millimeters).

Related methods:

android.support.v7.cardview:contentPaddingRight

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

May 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), and mm (millimeters).

Related methods:

android.support.v7.cardview:contentPaddingTop

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

May 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), and mm (millimeters).

Related methods:

Public constructors

CardView

CardView (Context context)

Parameters
context Context

CardView

CardView (Context context, 
                AttributeSet attrs)

Parameters
context Context
attrs AttributeSet

CardView

CardView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context
attrs AttributeSet
defStyleAttr int

Public methods

getCardBackgroundColor

ColorStateList getCardBackgroundColor ()

Returns the background color state list of the CardView.

Returns
ColorStateList The background color state list of the CardView.

getCardElevation

float getCardElevation ()

Returns the backward compatible elevation of the CardView.

Returns
float Elevation of the CardView

See also:

getContentPaddingBottom

int getContentPaddingBottom ()

Returns the inner padding before the Card's bottom edge

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

getContentPaddingLeft

int getContentPaddingLeft ()

Returns the inner padding after the Card's left edge

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

getContentPaddingRight

int getContentPaddingRight ()

Returns the inner padding before the Card's right edge

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

getContentPaddingTop

int getContentPaddingTop ()

Returns the inner padding after the Card's top edge

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

getMaxCardElevation

float getMaxCardElevation ()

Returns the backward compatible maximum elevation of the CardView.

Returns
float Maximum elevation of the CardView

See also:

getPreventCornerOverlap

boolean getPreventCornerOverlap ()

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

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

getRadius

float getRadius ()

Returns the corner radius of the CardView.

Returns
float Corner radius of the CardView

See also:

getUseCompatPadding

boolean getUseCompatPadding ()

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

Returns
boolean true if CardView adds inner padding on platforms Lollipop and after to have same dimensions with platforms before Lollipop.

setCardBackgroundColor

void setCardBackgroundColor (int color)

Updates the background color of the CardView

Related XML Attributes:

Parameters
color int: The new color to set for the card background

setCardBackgroundColor

void setCardBackgroundColor (ColorStateList color)

Updates the background ColorStateList of the CardView

Related XML Attributes:

Parameters
color ColorStateList: The new ColorStateList to set for the card background

setCardElevation

void setCardElevation (float elevation)

Updates the backward compatible elevation of the CardView.

Related XML Attributes:

Parameters
elevation float: The backward compatible elevation in pixels.

See also:

setContentPadding

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

Related XML Attributes:

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

setMaxCardElevation

void setMaxCardElevation (float maxElevation)

Updates the backward compatible maximum elevation of the CardView.

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

Related XML Attributes:

Parameters
maxElevation float: The backward compatible maximum elevation in pixels.

See also:

setMinimumHeight

void setMinimumHeight (int minHeight)

Sets the minimum height of the view. It is not guaranteed the view will be able to achieve this minimum height (for example, if its parent layout constrains it with less available height).

Parameters
minHeight int: The minimum height the view will try to be.

setMinimumWidth

void setMinimumWidth (int minWidth)

Sets the minimum width of the view. It is not guaranteed the view will be able to achieve this minimum width (for example, if its parent layout constrains it with less available width).

Parameters
minWidth int: The minimum width the view will try to be.

setPadding

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 int: the left padding in pixels
top int: the top padding in pixels
right int: the right padding in pixels
bottom int: the bottom padding in pixels

setPaddingRelative

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 int: the start padding in pixels
top int: the top padding in pixels
end int: the end padding in pixels
bottom int: the bottom padding in pixels

setPreventCornerOverlap

void setPreventCornerOverlap (boolean preventCornerOverlap)

On pre-Lollipop platforms, 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 Lollipop and above does not have any effect unless you have enabled compatibility padding.

Related XML Attributes:

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

See also:

setRadius

void setRadius (float radius)

Updates the corner radius of the CardView.

Related XML Attributes:

Parameters
radius float: The radius in pixels of the corners of the rectangle shape

See also:

setUseCompatPadding

void setUseCompatPadding (boolean useCompatPadding)

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

This may cause Cards to have different sizes between Lollipop and before Lollipop. 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 Lollipop and after.

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

Related XML Attributes:

Parameters
useCompatPadding boolean: true> if CardView should add padding for the shadows on platforms Lollipop and above.

Protected methods

onMeasure

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 overridden 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 int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.