Customizing the CardView

suggest change

CardView provides a default elevation and corner radius so that cards have a consistent appearance across the platforms.

You can customize these default values using these attributes in the xml file:

  1. card_view:cardElevation sets the elevation.
  2. card_view:cardBackgroundColor sets the background color.
  3. card_view:cardCornerRadius rounds the four corners.
  4. card_view:contentPadding adds padding between the card and its children.

card_view is a namespace on the card or a parent: xmlns:card_view="http://schemas.android.com/apk/res-auto".

Here is an example:

<android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardElevation="4dp"
        card_view:cardBackgroundColor="@android:color/white"
        card_view:cardCornerRadius="8dp"
        card_view:contentPadding="16dp">

        <!-- one child layout containing other layouts or views -->     

</android.support.v7.widget.CardView>

You can also do it programmatically using:

card.setCardBackgroundColor(Color.WHITE);
card.setCardElevation(4f);
card.setRadius(8f);
card.setContentPadding(16, 16, 16, 16);

Check the official javadoc for additional properties.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents