CardView in Android Studio: A Comprehensive Guide

Erim Onat Gokaslan
3 min readMay 15, 2024

--

Introduction

As an Software developer, creating visually appealing and user-friendly interfaces is crucial. CardView is a versatile component that helps achieve modern and elegant UI designs. This guide will walk you through everything you need to know about CardView, from basic setup to advanced customizations and real-world applications.

What is CardView?

CardView is a layout container that allows you to create a card-like UI design. It offers a consistent look and feel across different devices and enhances the user experience with customizable features like rounded corners, elevation, and shadows.

CardView is a layout container in Android that provides a consistent look and feel across different devices. It allows for easy customization, such as rounded corners and shadows, to create a sense of depth and elevation. This enhances the user experience by making UI elements more engaging and interactive.

Setting Up Your Project

Open your build.gradle file (usually located in the app module).

Add the following dependency:

dependencies {
implementation 'androidx.cardview:cardview:1.0.0'
}

Next, sync your project to ensure the dependency is added correctly.

Creating a Simple CardView

Let’s create a simple CardView with rounded corners and a slight elevation.

  1. Open your layout XML file (e.g., activity_main.xml).
  2. Add the following XML code:
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="4dp"
android:layout_margin="8dp">

<!-- Content inside the CardView -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello CardView!"
android:padding="16dp"/>
</androidx.cardview.widget.CardView>

This layout creates a CardView with rounded corners and a slight elevation, containing a simple TextView with some padding.

Advanced CardView Customizations

CardView can be customized in various ways to fit your design needs. You can add images, buttons, and other views inside the CardView. For example, to add an image and a button:

Update your layout XML file with the following code:

<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="4dp"
android:layout_margin="8dp">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">

<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/sample_image"
android:scaleType="centerCrop"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CardView Example"
android:textSize="18sp"
android:paddingTop="8dp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Action"/>
</LinearLayout>
</androidx.cardview.widget.CardView>

This example demonstrates a CardView containing an ImageView, a TextView, and a Button, all arranged vertically within a LinearLayout.

Using CardView in RecyclerView

CardView is often used within a RecyclerView to create a list of card items. Here’s how you can do it:

Create a layout for the RecyclerView item (e.g., card_view_item.xml):

<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="4dp"
android:layout_margin="8dp">

<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"/>
</androidx.cardview.widget.CardView>

Create the RecyclerView adapter:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<MyItem> itemList;

public static class MyViewHolder extends RecyclerView.ViewHolder {
public CardView cardView;
public TextView textView;
public MyViewHolder(View v) {
super(v);
cardView = v.findViewById(R.id.card_view);
textView = v.findViewById(R.id.text_view);
}
}

public MyAdapter(List<MyItem> itemList) {
this.itemList = itemList;
}

@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_item, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.textView.setText(itemList.get(position).getText());
}

@Override
public int getItemCount() {
return itemList.size();
}
}

Set up the RecyclerView in your Activity:

RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

List<MyItem> items = new ArrayList<>();
// Populate the list with items
MyAdapter adapter = new MyAdapter(items);
recyclerView.setAdapter(adapter);

Conclusion

CardView is a powerful tool for creating beautiful and interactive UI components in Android. By following the tips and examples in this guide, you can start incorporating CardView into your apps to enhance their visual appeal and user experience.

--

--