The Wayback Machine - https://web.archive.org/web/20241117071942/https://www.geeksforgeeks.org/cardview-in-android-with-example/
Open In App

CardView in Android With Example

Last Updated : 08 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner layout along with a specific elevation. CardView is the view that can display views on top of each other. The main usage of CardView is that it helps to give a rich feel and look to the UI design. This widget can be easily seen in many different Android Apps. CardView can be used for creating items in listview or inside Recycler View. The best part about CardView is that it extends Framelayout and it can be displayed on all platforms of Android. Now we will see the simple example of CardView implementation. 

Implementation: CardView

CardView helps create beautiful UI components in Android apps. If you want to explore how to implement UI design elements effectively using Kotlin, the Android Mastery with Kotlin: Beginner to Advanced course provides practical lessons on modern UI design.”

Step 1: Create a new Android Studio Project. 

For creating a new Android Studio Project. Click on File>New>New Project. Make sure to keep your language as JAVA and select Empty Activity.

Step 2: Add material dependency in build.gradle file.

Navigate to Gradle Scripts then to build.gradle(app) and then add below dependency to it. 

implementation 'com.google.android.material:material:1.2.1'

After adding this dependency you will get to see a popup option at top right corner which displays sync now. Click on that option to sync your project. 

Step 3: Add google repository in the build.gradle file of the application project if by default it is not there

buildscript {

repositories {

   google()

   mavenCentral()

}

All Jetpack components are available in the Google Maven repository, include them in the build.gradle file

allprojects {

repositories {

   google()

  mavenCentral()

}

}

Step 4: Now we will create a simple CardView. 

Navigate to app>res>layout>activity_main.xml then create new CardView widget to it. 

XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="10dp"
    app:cardCornerRadius="20dp"
    android:layout_margin="10dp"
    app:cardBackgroundColor="@color/white"
    app:cardMaxElevation="12dp"
    app:cardPreventCornerOverlap="true"
    app:cardUseCompatPadding="true"
    >

    <!--
    In the above cardview widget
    cardelevation property will give elevation to your card view
    card corner radius will provide radius to your card view
    card background color will give background color to your card view
    card max elevation will give the cardview maximum elevation
    card prevent corner overlap will add padding to CardView on v20 and before to prevent intersections between the Card content and rounded corners.
    card use compact padding will add padding in API v21+ as well to have the same measurements with previous versions.
    below are the two widgets imageview and text view we are displaying inside our card view.
    -->
    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:src="@drawable/gfgimage"
        android:layout_margin="10dp"
        android:id="@+id/img"
        android:contentDescription="@string/app_name" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:textSize="20sp"
        android:textStyle="bold"
        />



</androidx.cardview.widget.CardView>

</RelativeLayout>

Step 5: Now run your app on your emulator or device you will get to see the output. 

Output: 

Some important attributes of Cardview are :

1. cardBackgroundColor : Used for setting up the background-color of the card.
2. cardElevation : Defines the elevation (the process of moving to a higher place or more important position) of the card. It’s value should not be quite large else the design may not look good.
3. cardCornerRadius : It sets radius around the corners of the card. More the value of this attribute more circular the edges would appear in the card.
4. cardUseCompactPadding : It has two values true and false. By default, the cardview is set to (0,0) top left corner of the screen. And if this attribute is set to true then the card will set padding for itself so that our UI looks good. This case is helpful in the scenarios when our gravity is not set to center or any other parameters.



Similar Reads

CardView using RecyclerView in Android with Example
RecyclerView is an extended version of ListView and GridView. It works on the ViewHolder design pattern. With the help of RecyclerView, we can add many extra features to our list of data. Before starting our example on the implementation of CardView in RecyclerView. We should know what CardView and RecyclerView mean. CardView: CardView is an extend
9 min read
How to Add Line ProgressBar with CardView using Buttons in Android?
Android ProgressBar is a graphical representation or view indicator that displays a bar or ring representing the completion of the task. It gives the user an idea of the time to finish the task. Some basic attributes used in ProgressBar are: android:minHeight android:minWidthandroid:progressstyle There are many types of ProgressBar in android: Step
2 min read
How to Create an Expandable CardView in Android?
Expandable Cardview provides to create expansion panels without too much hassle and without writing boilerplate code. An expandable card view becomes quite useful when it comes to an efficient and systematic presentation of data or information on the screen. It is used in a variety of apps, for example, the Contacts app or the Gallery app. We are g
6 min read
How to Create Circular ImageView in Android using CardView?
Displaying an Image in Android is done easily using ImageView. But what if one wants to display a circular image? It is seen that many Android apps use CircularImageView to show the profile images, status, stories, and many other things but doing this with a normal ImageView is a bit difficult. This article will help to create a circular image usin
3 min read
How to Add Gradient to CardView in Android?
UI stands for User Interface, which represents the graphical interface that allows the user to interact with software or any other devices. It comprises some visual elements like Buttons, icons, text, and Images that allow users to steer and interact with the software. UI basically concentrate on creating instinctive and User-Friendly interfaces th
2 min read
How to Change CardView Color When Selected in Android?
CardView is a UI component in Android Studio that provides a simple way to display content with a raised or elevated appearance. It's part of the Android Support Library, which means it's compatible with Android devices running Android 5.0 (API level 21) or higher. A sample video is given below to get an idea about what we are going to do in this a
2 min read
How to Make CardView Checkable In Android?
In Android, We can make a CardView checkable, which can be really a useful feature. If we want the user to select some items and want to display the items that the user has chosen then this one is the most important feature for us. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to im
4 min read
How to Use Fast Android Networking Library in Android with Example?
In Android, we know multiple networking libraries like Retrofit, Volley. We use these libraries specifically for making networking requests like performing actions on API. But Besides that, there is another library called Fast Networking Library which has a few advantages over other libraries. In this tutorial, we will be specifically focusing on l
4 min read
Android Jetpack - WorkManager with Example
WorkManager is an Android Jetpack library that provides a flexible and efficient way to schedule and manage background tasks in your app. It is a powerful and recommended tool for managing background processing in Android applications. With WorkManager, you can schedule asynchronous, deferred, and periodic tasks that need to run even when the app i
5 min read
Android BottomSheet Example in Kotlin
The Bottom Sheet is seen in many of the applications such as Google Drive, Google Maps and most of the applications used the Bottom Sheet to display the data inside the application. In this article, we will take a look at implementing a Bottom Sheet in the Android app using Kotlin in Android Studio. What is Bottom Sheet? Bottom Sheet is a component
5 min read
PhotoView in Android with Example
In this article, PhotoView is added to android. PhotoView aims to help produce an easily usable implementation of a zooming Android ImageView using multi-touch and double-tap. Besides that, it has many more features like it notifying the application when the user taps on the photo or when the displayed matrix gets changed. It provides smooth scroll
2 min read
NestedScrollView in Android with Example
NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. It is enabled by default. NestedScrollView is used when there is a need for a scrolling view inside another scrolling view. You have seen this in many apps for example when we open a pdf file and when
5 min read
Activity State Changes In Android with Example
Prerequisites: Activity lifecycle in android As it is known that every Android app has at least one activity associated with it. When the application begins to execute and runs, there are various state changes that activity goes through. Different events some user-triggered and some system triggered can cause an activity to do the transition from o
6 min read
Theming Floating Action Buttons in Android with Example
Prerequisite: Floating Action Button (FAB) in Android with ExampleExtended Floating Action Button in Android with Example Android application developers want to seek the attention of the users by customizing and theming the android application widgets and keep more traffic of customers only by the design of the application. In this article, it has
12 min read
ArrayAdapter in Android with Example
The Adapter acts as a bridge between the UI Component and the Data Source. It converts data from the data sources into view items that can be displayed into the UI Component. Data Source can be Arrays, HashMap, Database, etc. and UI Components can be ListView, GridView, Spinner, etc. ArrayAdapter is the most commonly used adapter in android. When y
3 min read
SlidingDrawer in Android with Example
SlidingDrawer is a common requirement in android mobiles to view the content whenever needed by sliding horizontally as well as vertically. In this article, let us check out Sliding Drawer which is used to hide the content out from the screen and allows the user to drag a handle to bring the content on the screen whenever needed in a very user-frie
9 min read
ViewStub in Android with Example
In Android, ViewStub is a zero-sized invisible View and very flexible in that we can lazily inflate layout resources at runtime. It is a dumb and lightweight view and it has zero dimension. Depending upon the requirement, whenever in need we can inflate at runtime. This is a classic example to handle when there are complicated views to be handled.
6 min read
Include and Merge Tags in Android with Example
Android apps are growing and one important aspect of it is reusability. Sometimes, the complexity of app design will be more and during that time, Android providing very efficient reusable features by means of the <include/> and <merge/> tags. Under <merge/> tags, we can specify a part of the layout that has to come in the main la
4 min read
Theming of Material Design EditText in Android with Example
Prerequisite: Material Design EditText in Android with Examples In the previous article Material Design EditText in Android with Examples Material design Text Fields offers more customization and makes the user experience better than the normal text fields. For example, Icon to clear the entire EditText field, helper text, etc. In this article, it'
4 min read
Theming Material Design Snackbars in Android with Example
In the previous article, it's been discussed Snackbar Material Design Components in Android. In this article, it's been discussed how we can theme material design Snackbar and increase the User Experience. Theming Example 1:This method is done using the styles.xml file. Where we need to override the default style of the Snackbar.Have a look at the
5 min read
Fresco Image Loading Library in Android with Example
Fresco is one of the famous image loading libraries from URLs in Android. It is a powerful library for displaying and managing images from URLs. This library can load images from Users' devices, servers, and other local sources. The most important feature of this library is to show a placeholder image when the image from the URL takes so much time
3 min read
SimpleAdapter in Android with Example
In Android, whenever we want to bind some data which we get from any data source (e.g. ArrayList, HashMap, SQLite, etc.) with a UI component(e.g. ListView, GridView, etc.) then Adapter comes into the picture. Basically Adapter acts as a bridge between the UI component and data sources. Here Simple Adapter is one type of Adapter. It is basically an
7 min read
Interpolator in Android with Example
An interpolator is a function (in the mathematical sense) that outputs values "interpolated" between a range of values that are given to it as input. Interpolation is simply a method to generate new data points between two fixed data points. The exact values of these generated data points are determined by the kind of interpolation is performed. Fo
8 min read
Dagger 2 Android Example using Retrofit
Dagger 2 Android implementation is easier and it is based on Dependency Injection Architecture. Dependency Injection is a design pattern, which is a concept of Object-Oriented Programming, where we don’t create an object of another class inside a class using the new keyword (for Java). Instead, we supply the needed object from the outside. It is a
6 min read
Input Events in Android with Example
In Android, there's quite a method to intercept the events from a user's interaction with your application. When considering events within your interface, the approach is to capture the events from the precise View object that the user interacts with. The View class provides the means to try to do so. Within the varied View classes that you're goin
7 min read
TreeView in Android with Example
If you are looking for new UI designs to represent huge data, then there are so many ways to represent this type of data. You can use pie charts, graphs, and many more view types to implement these views. For displaying such huge data then we can prefer using a TreeView. TreeView is similar to that of a tree in which it has a parent node and inside
7 min read
ActionBar in Android with Example
In Android applications, ActionBar is the element present at the top of the activity screen. It is a salient feature of a mobile application that has a consistent presence over all its activities. It provides a visual structure to the app and contains some of the frequently used elements for the users. Android ActionBar was launched by Google in 20
8 min read
Progress Dialog in Android with Example
In Android, a Progress Dialog is a type of alert message that can be invoked to appear on the screen to display the progress of an action that is loading. By default, a Progress Dialog has a title, a message, and a circular progress bar that keeps rotating. A sample Progress Dialog is shown in the below image. In this article, we will show you how
2 min read
Selecting Colors with the Palette API in Android with Example
When we are making UI design for any of the applications the most important part which we should take care of while building the application is that we should be using the proper color combination in our app which should be sometimes the same as that of the image. Using this API we can update the colors of our widgets according to the colors which
5 min read
Expandable News Feed in Android with Example
Many news applications use the Expandable type of view for displaying the news along with the categories. This type of feature will help to make the UI look better and also provides additional content in the same UI. This type of UI is used when you display different types of content inside your application and filtering it with different categorie
10 min read
three90RightbarBannerImg