Apps often need to display data in similarly styled containers. These containers
are often used in lists to hold each item's information. The system provides the
CardView API as an easy way for you show
information inside cards that have a consistent look across the platform. These
cards have a default elevation above their containing view group, so the system
draws shadows below them. Cards provide an easy way to contain a group of views
while providing a consistent style for the container.
Figure 1. Card examples
Add the dependencies
The CardView
widget is part of the v7 Support
Libraries. To use it in your project, add the following dependency
to your app module's build.gradle file:
dependencies {
implementation 'com.android.support:cardview-v7:28.0.0'
}
Create Cards
In order to use the CardView you need to add it to your layout
file. Use it as a view group to contain other views. In this example, the
CardView contains a single TextView
to display some information to the user.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
... >
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.CardView>
</LinearLayout>
The cards are drawn to the screen with a default elevation, which causes the system to draw a
shadow underneath them. You can provide a custom elevation for a card with the
card_view:cardElevation attribute. This will draw a more pronounced shadow with a
larger elevation, and a lower elevation will result in a lighter shadow.
CardView uses real elevation and dynamic shadows on Android 5.0
(API level 21) and above and falls back to a programmatic shadow implementation on earlier versions.
Use these properties to customize the appearance of the
CardView widget:
- To set the corner radius in your layouts, use the
card_view:cardCornerRadiusattribute. - To set the corner radius in your code, use the
CardView.setRadiusmethod. - To set the background color of a card, use the
card_view:cardBackgroundColorattribute.
For more information, see the API reference for CardView.

