Android CardView With Image And Text Example

Android CardView is a component that is used to implement the card layout effect. It is provided in the appcompat-v7 library. Use CardView, you can add a circle corner and shadow effect to the card frame.

In this example, we will list some car images with text in a recycler view using grid layout manager. Each recycler view item is implemented with a CardView. When you click the car image, a Snackbar will pop up at the screen bottom.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/wANU2z9b5kc

  1. Before coding, you should add below dependency library in the project build.gradle file dependencies section.
    compile 'com.android.support:cardview-v7:26.+'
    compile 'com.android.support:recyclerview-v7:26.+'
    

1. CardView Example Java File Structure.

  1. There are four java files in this example. You can see the file structures below.
    ./
    ├── app
    │   ├── build.gradle
    │   ├── proguard-rules.pro
    │   └── src
    │       ├── main
    │       │   ├── AndroidManifest.xml
    │       │   ├── java
    │       │   │   └── com
    │       │   │       └── dev2qa
    │       │   │           └── example
    │       │   │               ├── material_design
    │       │   │               │   ├── cardview
    │       │   │               │   │   ├── CarRecyclerViewDataAdapter.java
    │       │   │               │   │   ├── CarRecyclerViewItem.java
    │       │   │               │   │   ├── CarRecyclerViewItemHolder.java
    │       │   │               │   │   └── CardViewActivity.java
    │       │   ├── res
    │       │   │   ├── layout
    │       │   │   │   ├── activity_card_view.xml
    │       │   │   │   ├── activity_card_view_item.xml
    

2. Main Activity.

  1. CardViewActivity.java
    package com.dev2qa.example.material_design.cardview;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.GridLayoutManager;
    import android.support.v7.widget.RecyclerView;
    
    import com.dev2qa.example.R;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class CardViewActivity extends AppCompatActivity {
    
        private List<CarRecyclerViewItem> carItemList = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_card_view);
    
            setTitle("dev2qa.com - Android CardView Example.");
    
            initializeCarItemList();
    
            // Create the recycler view object.
            RecyclerView carRecyclerView = (RecyclerView)findViewById(R.id.card_view_recycler_list);
            // Create the grid layout manager with 2 columns.
            GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
            // Set layout manager.
            carRecyclerView.setLayoutManager(gridLayoutManager);
    
            // Create car recycler view data adapter with car item list.
            CarRecyclerViewDataAdapter carDataAdapter = new CarRecyclerViewDataAdapter(carItemList);
            // Set data adapter.
            carRecyclerView.setAdapter(carDataAdapter);
    
        }
    
        /* Initialise car items in list. */
        private void initializeCarItemList()
        {
            if(carItemList == null)
            {
                carItemList = new ArrayList<CarRecyclerViewItem>();
                carItemList.add(new CarRecyclerViewItem("Audi", R.drawable.car_audi));
                carItemList.add(new CarRecyclerViewItem("BMW", R.drawable.car_bmw));
                carItemList.add(new CarRecyclerViewItem("Benz", R.drawable.car_benz));
                carItemList.add(new CarRecyclerViewItem("Jeep", R.drawable.car_jeep));
                carItemList.add(new CarRecyclerViewItem("Land Rover", R.drawable.car_land_rover));
                carItemList.add(new CarRecyclerViewItem("Future", R.drawable.car_future));
            }
        }
    }

3. Car RecyclerView Data Adapter.

  1. CarRecyclerViewDataAdapter.java
    package com.dev2qa.example.material_design.cardview;
    
    import android.support.design.widget.Snackbar;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.dev2qa.example.R;
    
    import java.util.List;
    
    public class CarRecyclerViewDataAdapter extends RecyclerView.Adapter<CarRecyclerViewItemHolder> {
    
        private List<CarRecyclerViewItem> carItemList;
    
        public CarRecyclerViewDataAdapter(List<CarRecyclerViewItem> carItemList) {
            this.carItemList = carItemList;
        }
    
        @Override
        public CarRecyclerViewItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // Get LayoutInflater object.
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            // Inflate the RecyclerView item layout xml.
            View carItemView = layoutInflater.inflate(R.layout.activity_card_view_item, parent, false);
    
            // Get car title text view object.
            final TextView carTitleView = (TextView)carItemView.findViewById(R.id.card_view_image_title);
            // Get car image view object.
            final ImageView carImageView = (ImageView)carItemView.findViewById(R.id.card_view_image);
            // When click the image.
            carImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Get car title text.
                    String carTitle = carTitleView.getText().toString();
                    // Create a snackbar and show it.
                    Snackbar snackbar = Snackbar.make(carImageView, "You click " + carTitle +" image", Snackbar.LENGTH_LONG);
                    snackbar.show();
                }
            });
    
            // Create and return our custom Car Recycler View Item Holder object.
            CarRecyclerViewItemHolder ret = new CarRecyclerViewItemHolder(carItemView);
            return ret;
        }
    
        @Override
        public void onBindViewHolder(CarRecyclerViewItemHolder holder, int position) {
            if(carItemList!=null) {
                // Get car item dto in list.
                CarRecyclerViewItem carItem = carItemList.get(position);
    
                if(carItem != null) {
                    // Set car item title.
                    holder.getCarTitleText().setText(carItem.getCarName());
                    // Set car image resource id.
                    holder.getCarImageView().setImageResource(carItem.getCarImageId());
                }
            }
        }
    
        @Override
        public int getItemCount() {
            int ret = 0;
            if(carItemList!=null)
            {
                ret = carItemList.size();
            }
            return ret;
        }
    }

4. Car RecyclerView Item DTO.

  1. CarRecyclerViewItem.java
    package com.dev2qa.example.material_design.cardview;
    
    public class CarRecyclerViewItem {
    
        // Save car name.
        private String carName;
    
        // Save car image resource id.
        private int carImageId;
    
        public CarRecyclerViewItem(String carName, int carImageId) {
            this.carName = carName;
            this.carImageId = carImageId;
        }
    
        public String getCarName() {
            return carName;
        }
    
        public void setCarName(String carName) {
            this.carName = carName;
        }
    
        public int getCarImageId() {
            return carImageId;
        }
    
        public void setCarImageId(int carImageId) {
            this.carImageId = carImageId;
        }
    }

5. Car RecyclerView Item Holder.

  1. CarRecyclerViewItemHolder.java
    package com.dev2qa.example.material_design.cardview;
    
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.dev2qa.example.R;
    
    public class CarRecyclerViewItemHolder extends RecyclerView.ViewHolder {
    
        private TextView carTitleText = null;
    
        private ImageView carImageView = null;
    
        public CarRecyclerViewItemHolder(View itemView) {
            super(itemView);
    
            if(itemView != null)
            {
                carTitleText = (TextView)itemView.findViewById(R.id.card_view_image_title);
    
                carImageView = (ImageView)itemView.findViewById(R.id.card_view_image);
            }
        }
    
        public TextView getCarTitleText() {
            return carTitleText;
        }
    
        public ImageView getCarImageView() {
            return carImageView;
        }
    }

6. Main Layout XML File.

  1. The main layout XML file is saved in the app/res/layout folder.
  2. activity_card_view.xml
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/card_view_recycler_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </android.support.design.widget.CoordinatorLayout>

7. RecyclerView Item Layout XML File.

  1. android.support.v7.widget.CardView is the XML tag. It has the below attributes.
  2. app:cardCornerRadius : The card corner radius size.
  3. app:cardElevation: The card elevation from the screen surface.
  4. The layout XML file name is activity_card_view_item.xml, and it is saved in the app/res/layout folder.
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:cardCornerRadius="8dp"
        app:cardElevation="10dp">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <ImageView
                android:id="@+id/card_view_image"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:scaleType="centerCrop"/>
    
            <TextView
                android:id="@+id/card_view_image_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_gravity="center_horizontal"
                android:textSize="20dp"/>
    
    
        </LinearLayout>
    
    </android.support.v7.widget.CardView>

8. Android Manifest Xml File.

  1. AndroidManifest.xml
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.dev2qa.example">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            
            <activity android:name=".material_design.cardview.CardViewActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>

22 thoughts on “Android CardView With Image And Text Example”

  1. Great tutorial! The step-by-step explanation made it easy to follow along. I loved how you integrated the image and text in the CardView. Looking forward to more tips and tricks like this!

  2. Great post! The example of using CardView with both image and text is really helpful. I appreciate the step-by-step approach; it made implementing it in my own project much easier. Thanks for sharing these Windows tricks!

  3. Great tutorial! I found the steps easy to follow, and the example was really helpful. The CardView layout looks fantastic with both image and text. Thanks for sharing these tips!

  4. Great tutorial! The step-by-step instructions made it easy to implement CardView with images and text. I appreciate the clear examples and code snippets. Thanks for sharing these Windows tricks!

  5. Great post! The step-by-step instructions on implementing CardView with image and text were really helpful. I love how it enhances the UI of my Android app. Looking forward to trying out the additional tips you provided!

  6. Great post! I found the example of integrating CardView with images and text really helpful. The step-by-step instructions made it easy to follow along. Can’t wait to implement this in my own app!

  7. Great post! The step-by-step instructions on implementing CardView with images and text were really helpful. I appreciate the clear examples and how you explained the XML layouts. Looking forward to trying this out in my own projects!

  8. Great post! The example makes it easy to understand how to implement CardView with both image and text in Android. I appreciate the step-by-step instructions—very helpful for my project! Thanks for sharing!

  9. Great post! The example on implementing CardView with images and text was really clear and helpful. I appreciate the step-by-step instructions—you made it easy to follow along. Thanks for sharing!

  10. Great post! The example you provided for implementing CardView with image and text was really helpful. I appreciate the clear steps and the visuals; it made the process much easier to follow. Keep up the good work!

  11. Great tutorial! The step-by-step instructions made it really easy to implement CardView with images and text in my Android app. Thanks for sharing those helpful tips!

  12. Great post! The step-by-step guide on implementing CardView with images and text was super helpful. I especially appreciated the clear explanations and examples provided. Can’t wait to try this out in my own projects!

  13. This tutorial was super helpful! The step-by-step instructions made it easy to implement CardView in my own project. I especially liked the inclusion of images with text—it really enhances the layout. Thanks for sharing!

  14. Great example of using CardView! The combination of images and text creates a clean and modern look for Android apps. I appreciate the step-by-step instructions; they made it easy to follow along. Looking forward to trying this out in my own projects!

  15. Hi, I was rejected by this project as I write or order how each one will be unlocked by Activity.
    if (position == 0) startActivity(1)
    if (position == 1) startActivity(2)
    if (position == 2) startActivity(3)
    How do I order?
    please help

  16. Hi, Thanks for the great article. I have a weird problem. when I completely copied your code into a new project, it worked perfectly. But when I copied your code into my own project, It did not work.
    I checked everything, all the resources all the same.
    The only problem in my project is the image inside the card view does not have the round edge.

    1. Sir,how I can add content inside the card view so that when a user clicks some description text will come with image.

  17. I want to play audio with MediaPlayer when I click on any of the car pictures.

    if(carTitleView.getText().toString()==”Audi”)
    {
    MediaPlayer medPlayer=MediaPlayer.create(getApplicationContext,R.raw.audisound);
    medPlayer.start();
    }

    I tried this code but it did not work. Please help me.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.