CardView

open class CardView : FrameLayout
kotlin.Any
   ↳ 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. androidx.cardview.R.attr#cardBackgroundColor androidx.cardview.R.attr#cardCornerRadius androidx.cardview.R.attr#cardElevation androidx.cardview.R.attr#cardMaxElevation androidx.cardview.R.attr#cardUseCompatPadding androidx.cardview.R.attr#cardPreventCornerOverlap androidx.cardview.R.attr#contentPadding androidx.cardview.R.attr#contentPaddingLeft androidx.cardview.R.attr#contentPaddingTop androidx.cardview.R.attr#contentPaddingRight androidx.cardview.R.attr#contentPaddingBottom

Summary

Public constructors

<init>(@NonNull context: Context)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int)

Public methods

open ColorStateList

Returns the background color state list of the CardView.

open Float

Returns the backward compatible elevation of the CardView.

open Int

Returns the inner padding before the Card's bottom edge

open Int

Returns the inner padding after the Card's left edge

open Int

Returns the inner padding before the Card's right edge

open Int

Returns the inner padding after the Card's top edge

open Float

Returns the backward compatible maximum elevation of the CardView.

open Boolean

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

open Float

Returns the corner radius of the CardView.

open Boolean

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

open Unit

Updates the background color of the CardView

open Unit

Updates the background ColorStateList of the CardView

open Unit
setCardElevation(elevation: Float)

Updates the backward compatible elevation of the CardView.

open Unit
setContentPadding(@Px left: Int, @Px top: Int, @Px right: Int, @Px bottom: Int)

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

open Unit
setMaxCardElevation(maxElevation: Float)

Updates the backward compatible maximum elevation of the CardView.

open Unit
setMinimumHeight(minHeight: Int)

open Unit
setMinimumWidth(minWidth: Int)

open Unit
setPadding(left: Int, top: Int, right: Int, bottom: Int)

open Unit
setPaddingRelative(start: Int, top: Int, end: Int, bottom: Int)

open Unit
setPreventCornerOverlap(preventCornerOverlap: Boolean)

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

open Unit
setRadius(radius: Float)

Updates the corner radius of the CardView.

open Unit
setUseCompatPadding(useCompatPadding: Boolean)

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

Protected methods

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

Public constructors

<init>

CardView(@NonNull context: Context)

<init>

CardView(@NonNull context: Context, @Nullable attrs: AttributeSet?)

<init>

CardView(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int)

Public methods

getCardBackgroundColor

@NonNull open fun getCardBackgroundColor(): ColorStateList

Returns the background color state list of the CardView.

Return
ColorStateList: The background color state list of the CardView.

getCardElevation

open fun getCardElevation(): Float

Returns the backward compatible elevation of the CardView.

Return
Float: Elevation of the CardView

getContentPaddingBottom

@Px open fun getContentPaddingBottom(): Int

Returns the inner padding before the Card's bottom edge

Return
Int: the inner padding before the Card's bottom edge

getContentPaddingLeft

@Px open fun getContentPaddingLeft(): Int

Returns the inner padding after the Card's left edge

Return
Int: the inner padding after the Card's left edge

getContentPaddingRight

@Px open fun getContentPaddingRight(): Int

Returns the inner padding before the Card's right edge

Return
Int: the inner padding before the Card's right edge

getContentPaddingTop

@Px open fun getContentPaddingTop(): Int

Returns the inner padding after the Card's top edge

Return
Int: the inner padding after the Card's top edge

getMaxCardElevation

open fun getMaxCardElevation(): Float

Returns the backward compatible maximum elevation of the CardView.

Return
Float: Maximum elevation of the CardView

getPreventCornerOverlap

open fun getPreventCornerOverlap(): Boolean

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

Return
Boolean: True if CardView prevents overlaps with rounded corners on platforms before Lollipop. Default value is true.

getRadius

open fun getRadius(): Float

Returns the corner radius of the CardView.

Return
Float: Corner radius of the CardView

See Also

getUseCompatPadding

open fun getUseCompatPadding(): Boolean

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

Return
Boolean: true if CardView adds inner padding on platforms Lollipop and after to have same dimensions with platforms before Lollipop.

setCardBackgroundColor

open fun setCardBackgroundColor(color: Int): Unit

Updates the background color of the CardView

Parameters
color Int: The new color to set for the card background androidx.cardview.R.attr#cardBackgroundColor

setCardBackgroundColor

open fun setCardBackgroundColor(@Nullable color: ColorStateList?): Unit

Updates the background ColorStateList of the CardView

Parameters
color ColorStateList?: The new ColorStateList to set for the card background androidx.cardview.R.attr#cardBackgroundColor

setCardElevation

open fun setCardElevation(elevation: Float): Unit

Updates the backward compatible elevation of the CardView.

Parameters
elevation Float: The backward compatible elevation in pixels. androidx.cardview.R.attr#cardElevation

setContentPadding

open fun setContentPadding(@Px left: Int, @Px top: Int, @Px right: Int, @Px bottom: Int): Unit

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 android.view.View#setPadding(int, int, int, int).

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 androidx.cardview.R.attr#contentPadding androidx.cardview.R.attr#contentPaddingLeft androidx.cardview.R.attr#contentPaddingTop androidx.cardview.R.attr#contentPaddingRight androidx.cardview.R.attr#contentPaddingBottom

setMaxCardElevation

open fun setMaxCardElevation(maxElevation: Float): Unit

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.

Parameters
maxElevation Float: The backward compatible maximum elevation in pixels. androidx.cardview.R.attr#cardMaxElevation

setMinimumHeight

open fun setMinimumHeight(minHeight: Int): Unit

setMinimumWidth

open fun setMinimumWidth(minWidth: Int): Unit

setPadding

open fun setPadding(left: Int, top: Int, right: Int, bottom: Int): Unit

setPaddingRelative

open fun setPaddingRelative(start: Int, top: Int, end: Int, bottom: Int): Unit

setPreventCornerOverlap

open fun setPreventCornerOverlap(preventCornerOverlap: Boolean): Unit

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.

Parameters
preventCornerOverlap Boolean: Whether CardView should add extra padding to content to avoid overlaps with the CardView corners. androidx.cardview.R.attr#cardPreventCornerOverlap

setRadius

open fun setRadius(radius: Float): Unit

Updates the corner radius of the CardView.

Parameters
radius Float: The radius in pixels of the corners of the rectangle shape androidx.cardview.R.attr#cardCornerRadius

setUseCompatPadding

open fun setUseCompatPadding(useCompatPadding: Boolean): Unit

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.

Parameters
useCompatPadding Boolean: true> if CardView should add padding for the shadows on platforms Lollipop and above. androidx.cardview.R.attr#cardUseCompatPadding

Protected methods

onMeasure

protected open fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int): Unit