Android Simple RecyclerView Adapter with CardView example tutorial

How to create multiple CardView dynamically using RecyclerView step by step example with code download.

In our previous tutorials we have discuss and learn about RecyclerView and CardView . Now we are moving forward to more advance tutorials in which we are going to develop an android app with the use of both RecyclerView and CardView . In this tutorial we have create multiple cardview dynamically one by one and each of them holds a unique value with indexing format handles by RecyclerViewAdapter.java  class. So here is the complete step by step tutorial for Android Simple RecyclerView Adapter with CardView example tutorial.

android-project-download-code-button

Note: Read below steps very carefully to add CardView library inside your current project.

1. Open your project’s build.gradle ( Module : app ) file.

build-gradle

2. Please add below code inside your build.gradle ( Module : app ) file.

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.0.+'
compile 'com.android.support:recyclerview-v7:23.0.+'

3. Screenshot of build.gradle ( Module : app ) file after adding above code.

Add Import CardView RecyclerView support library in Android Studio

Here your go now both libraries is successfully imported into our project now next step is to start coding.

Android Simple RecyclerView Adapter with CardView example tutorial.

Code for MainActivity.java file.

package com.android_examples.recyclerviewwithcardview_android_examplescom;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<String> SubjectNames;
    RecyclerView recyclerview;
    RecyclerView.LayoutManager RecyclerViewLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerview = (RecyclerView)findViewById(R.id.recyclerview1);

        RecyclerViewLayoutManager = new LinearLayoutManager(getApplicationContext());

        recyclerview.setLayoutManager(RecyclerViewLayoutManager);

        AddItemsToRecyclerViewArrayList();

        RecyclerView.Adapter adapter = new RecyclerViewAdapter(SubjectNames);

        recyclerview.setAdapter(adapter);

    }

    public void AddItemsToRecyclerViewArrayList(){

        SubjectNames = new ArrayList<>();
        SubjectNames.add("ANDROID");
        SubjectNames.add("PHP");
        SubjectNames.add("BLOGGER");
        SubjectNames.add("CSS");
        SubjectNames.add("MATHS");
        SubjectNames.add("ASP.NET");
        SubjectNames.add("JAVA");
        SubjectNames.add("C++");
        SubjectNames.add("HTML");
        SubjectNames.add("PHOTOSHOP");

    }

}

Code for activity_main.xml layout file.

<?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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.recyclerviewwithcardview_android_examplescom.MainActivity"
    android:id="@+id/relativelayout1">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview1"
        android:scrollbars="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</RelativeLayout>

Code for RecyclerViewAdapter.java file.

package com.android_examples.recyclerviewwithcardview_android_examplescom;

/**
 * Created by JUNED on 6/9/2016.
 */
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>  {

    ArrayList<String> SubjectNames;

    View view1;

    public RecyclerViewAdapter(ArrayList<String> SubjectNames1) {

        this.SubjectNames = SubjectNames1;
    }

    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        view1  = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_item, viewGroup, false);

        return new ViewHolder(view1);
    }

    @Override
    public void onBindViewHolder(RecyclerViewAdapter.ViewHolder Viewholder, int i) {

        Viewholder.SubjectTextView.setText(SubjectNames.get(i));
    }

    @Override
    public int getItemCount() {

        return SubjectNames.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        TextView SubjectTextView;
        public ViewHolder(View view) {

            super(view);

            SubjectTextView = (TextView)view.findViewById(R.id.textview1);
        }
    }

}

Code for cardview_item.xml layout file.

<android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/cardview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardCornerRadius="6dp"
            card_view:cardElevation="6dp"
            card_view:contentPadding="7dp"
            card_view:cardBackgroundColor="#2196F3"
            >

                <TextView
                    android:id="@+id/textview1"
                    android:layout_gravity="center"
                    android:textSize="20dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_marginTop="3dp"
                    android:layout_marginBottom="3dp"
                    android:textColor="#E3F2FD"/>

        </android.support.v7.widget.CardView>

Screenshot :

Android Simple RecyclerView Adapter with CardView example tutorial

Click here to download Android Simple RecyclerView Adapter with CardView example tutorial project with source code.

Leave a Reply

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