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 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 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 and pass true.
To change CardView's elevation in a backward compatible way, use setCardElevation. 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 when CardView is initialized.
ref androidx.cardview.R.styleable#CardView_cardBackgroundColor
ref androidx.cardview.R.styleable#CardView_cardCornerRadius
ref androidx.cardview.R.styleable#CardView_cardElevation
ref androidx.cardview.R.styleable#CardView_cardMaxElevation
ref androidx.cardview.R.styleable#CardView_cardUseCompatPadding
ref androidx.cardview.R.styleable#CardView_cardPreventCornerOverlap
ref androidx.cardview.R.styleable#CardView_contentPadding
ref androidx.cardview.R.styleable#CardView_contentPaddingLeft
ref androidx.cardview.R.styleable#CardView_contentPaddingTop
ref androidx.cardview.R.styleable#CardView_contentPaddingRight
ref androidx.cardview.R.styleable#CardView_contentPaddingBottom
Summary
Public constructors |
|---|
CardView(@NonNull Context context, @Nullable AttributeSet attrs) |
CardView( |
Public methods |
|
|---|---|
@NonNull ColorStateList |
Returns the background color state list of the CardView. |
float |
Returns the backward compatible elevation of the CardView. |
@Px int |
Returns the inner padding before the Card's bottom edge |
@Px int |
Returns the inner padding after the Card's left edge |
@Px int |
Returns the inner padding before the Card's right edge |
@Px int |
Returns the inner padding after the Card's top edge |
float |
Returns the backward compatible maximum elevation of the CardView. |
boolean |
Returns whether CardView should add extra padding to content to avoid overlaps with rounded corners on pre-Lollipop platforms. |
float |
Returns the corner radius of the CardView. |
boolean |
Returns whether CardView will add inner padding on platforms Lollipop and after. |
void |
Updates the background ColorStateList of the CardView |
void |
setCardBackgroundColor(@ColorInt int color)Updates the background color of the CardView |
void |
setCardElevation(float elevation)Updates the backward compatible elevation of the CardView. |
void |
setContentPadding(@Px int left, @Px int top, @Px int right, @Px 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 Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Inherited methods |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Public constructors
Public methods
getCardBackgroundColor
public @NonNull ColorStateList getCardBackgroundColor()
Returns the background color state list of the CardView.
| Returns | |
|---|---|
@NonNull 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 |
| See also | |
|---|---|
setCardElevation |
|
getMaxCardElevation |
getContentPaddingBottom
public @Px int getContentPaddingBottom()
Returns the inner padding before the Card's bottom edge
| Returns | |
|---|---|
@Px int |
the inner padding before the Card's bottom edge |
getContentPaddingLeft
public @Px int getContentPaddingLeft()
Returns the inner padding after the Card's left edge
| Returns | |
|---|---|
@Px int |
the inner padding after the Card's left edge |
getContentPaddingRight
public @Px int getContentPaddingRight()
Returns the inner padding before the Card's right edge
| Returns | |
|---|---|
@Px int |
the inner padding before the Card's right edge |
getContentPaddingTop
public @Px int getContentPaddingTop()
Returns the inner padding after the Card's top edge
| Returns | |
|---|---|
@Px int |
the inner padding after the Card's top edge |
getMaxCardElevation
public float getMaxCardElevation()
Returns the backward compatible maximum elevation of the CardView.
| Returns | |
|---|---|
float |
Maximum elevation of the CardView |
| See also | |
|---|---|
setMaxCardElevation |
|
getCardElevation |
getPreventCornerOverlap
public 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 |
getRadius
public float getRadius()
Returns the corner radius of the CardView.
| Returns | |
|---|---|
float |
Corner radius of the CardView |
| See also | |
|---|---|
getRadius |
getUseCompatPadding
public boolean getUseCompatPadding()
Returns whether CardView will add inner padding on platforms Lollipop and after.
| Returns | |
|---|---|
boolean |
|
setCardBackgroundColor
public void setCardBackgroundColor(@Nullable ColorStateList color)
Updates the background ColorStateList of the CardView
ref androidx.cardview.R.styleable#CardView_cardBackgroundColor
| Parameters | |
|---|---|
@Nullable ColorStateList color |
The new ColorStateList to set for the card background |
setCardBackgroundColor
public void setCardBackgroundColor(@ColorInt int color)
Updates the background color of the CardView
ref androidx.cardview.R.styleable#CardView_cardBackgroundColor
| Parameters | |
|---|---|
@ColorInt int color |
The new color to set for the card background |
setCardElevation
public void setCardElevation(float elevation)
Updates the backward compatible elevation of the CardView.
ref androidx.cardview.R.styleable#CardView_cardElevation
| Parameters | |
|---|---|
float elevation |
The backward compatible elevation in pixels. |
| See also | |
|---|---|
getCardElevation |
|
setMaxCardElevation |
setContentPadding
public void setContentPadding(@Px int left, @Px int top, @Px int right, @Px 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.
ref androidx.cardview.R.styleable#CardView_contentPadding
ref androidx.cardview.R.styleable#CardView_contentPaddingLeft
ref androidx.cardview.R.styleable#CardView_contentPaddingTop
ref androidx.cardview.R.styleable#CardView_contentPaddingRight
ref androidx.cardview.R.styleable#CardView_contentPaddingBottom
setMaxCardElevation
public 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.
ref androidx.cardview.R.styleable#CardView_cardMaxElevation
| Parameters | |
|---|---|
float maxElevation |
The backward compatible maximum elevation in pixels. |
| See also | |
|---|---|
setCardElevation |
|
getMaxCardElevation |
setPreventCornerOverlap
public 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.
ref androidx.cardview.R.styleable#CardView_cardPreventCornerOverlap
| Parameters | |
|---|---|
boolean preventCornerOverlap |
Whether CardView should add extra padding to content to avoid overlaps with the CardView corners. |
| See also | |
|---|---|
setUseCompatPadding |
setRadius
public void setRadius(float radius)
Updates the corner radius of the CardView.
ref androidx.cardview.R.styleable#CardView_cardCornerRadius
| Parameters | |
|---|---|
float radius |
The radius in pixels of the corners of the rectangle shape |
| See also | |
|---|---|
setRadius |
setUseCompatPadding
public 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.
ref androidx.cardview.R.styleable#CardView_cardUseCompatPadding
| Parameters | |
|---|---|
boolean useCompatPadding |
|