ListView with CardView and Open New Activity When items are clicked

Hi, Welcome to another ListView tutorial. In this Tutorial we shall develop a custom listview with cardview, imageview, text and Open New Activity When items are clicked. The items in this tutorial will be images and names of various fruits. When an item is clicked we shall start another activity which will display image and name of the clicked fruit item.

  • Let’s get started with coding

Create a new Android studio project

  • Open your android studio click File => New Project and give it a name in this tutorial i named it ListViewCard.
  • Click next select Empty Activity => Finish.

Adding CardView to Our project

  • Click File => Project Structure => App => Dependency => + => Library Dependency => Search for card and then add CardView as Shown below.
  • Custom ListView with CardView

Adding ListView to our Activity

  • Open activity_main.xml file, Copy and paste the code below.

Downloading Fruits Images used in this project

  • Use the link below to download fruit images used used in this tutorial.
  • download images

Creating Layout Resource File for holding listview data

  • Right click layout => New => Layout Resource File and name it row_data.
  • Copy and Paste the code below to your row_data.xml file.
  • preview from row_data.
  • Custom ListView with CardView, ImageView, Text and Open New Activity When items are clicked

Creating activity to receive and display clicked item

  • Right click on Layout => New => Activity => Empty Activity => name it in this case i have named it activity_item.
  • Open your activity_item, Copy and Paste the code below.

Receiving and displaying clicked items

  • We are going to use intents to send and receive clicked item.
  • To send data between activities through intent we use.
  • To receive data through intent we use.
  • Open your ItemActivity, Copy and Paste the code below for receiving and displaying clicked item.

Initializing ListView and Setting ListView Data

  • Open your MainActivity.java class
  • We shall start with initializing our listview and then set data list view data.
  • To set list view data we need to create custom Adapter that extends base Adapter.
  • Copy and Paste the code below in your MainActivity.java.

Running our app

  • Run your app and you will see the output below.
  • Custom ListView with CardView, ImageView, Text and Open New Activity When items are clicked

  • To download source code click here
  • That’s all if you have a question or need help please comment below.