What is CardView in Android? Everything you should know

CardView was announced by Google in 2014 as a part of Material Design components in android. The main goal of CardView in the Material Design is to provide a united look to all of the card-based UIs making it easier for developers to create seamless interfaces.

The CardView is a FrameLayout with a rounded corner background and shadow.

In this blog, we will discuss what possibilities CardView provides on Android and how to use it properly in the application.

Let’s discuss it with an example. In the below image, you will notice a white card like view container containing an ImageView, two TextViews, and an ImageButton. This is our CardView. As I already mentioned, a CardView is a view container or ViewGroup that inherits its nature from FrameLayout which further inherits from ViewGroup itself.

The basic customization that a CardView provides includes CornerRadius, Elevation, MaxElevation, ContentPadding, CompatPadding, PreventCornerOverlap, and a dedicated CardBackgroundColor.

Below XML code will give you an idea of CardView and its customizations.

Let’s discuss the above XML code and CardView attributes

1. Adding CardView dependency to project

CardView library can be added to a project by adding the following dependency to in build.gradle (module: app),

2. Why RelativeLayout inside CardView?

As we already know that a CardView is a ViewGroup but it inherits from FrameLayout, which is the most simplistic form of ViewGroup created to handle a single frame at a time. That’s why we need to use a RelativeLayout to hold all of our views together while acting as a single frame to the CardView.

3. CardView Background Color

By default, a CardView comes with a white card background color which can be changed as per our requirement by using this property.

Sample declaration of cardBackgroundColor (white) in XML and Java looks like this,

4. CardView Elevation

CardView Elevation property simply elevates the cardview on the z-axis to a height equal to the passed value. Since elevation came with Android Lollipop, CardView Elevation on previous versions uses shadows to create a related effect. It adds padding on its content’s sides using

and on top and bottom using

and then applies shadows to that space.

In our example, we have set the elevation to 1dp and maxElevation to 1.5dp. Now, if you look at the Java part, you’ll see the values passed to the methods are different than the values to the XML properties. This is because we can define the nature of value with XML but not with java methods which expect the value to be in pixels (px) only and thus we have to pass the number of pixels that make up the same dp as XML values.

5. CardView Corner Radius

The CardView Corner Radius gives the CardView a rounded corner of radius equal to the value provided to it.

Sample code for 5dp corner radius:

6. CardView Content Padding

The CardView padding is used to create a space for drawing shadows and thus we use ContentPadding to add padding between the edges and child views. In our example, we have used content padding to set padding on all the edges to 10dp and then content padding-bottom to 0dp to make sure there is no padding at the bottom.

7. CardView Use Compat Padding

Since CardViews use padding for drawing shadows on platforms before Lollipop, the content area will change on platforms before and after Lollipop. To make sure the content area remains the same on all the platforms, Compat Padding is used which simply adds inner padding on platforms Lollipop and after.

8. CardView Prevent Corner Overlap

The CardView avoids clipping the child views on platforms before Lollipop due to the rare nature of clipping and rather gives an option to distance content from rounded corners with Prevent Corner Overlap. The definition in XML and Java looks like this,

That’s it for what is CardView and basic attributes of cardview. In the next blog, we will see how to implement RecyclerView and CardView at the same time.

Hi everyone, myself Golap an Android app developer with UI/UX designer.