Android RecyclerView Tutorial

Google was introduced the Material Design in Lollipop 5.0 API 21 which RecyclerView is one of the Material design.   Android RecyclerView is much more better than old generation ListView because it is more advanced and more flexible version of ListView. Besides, it can control the layout of the list by using the LayoutManager unlike ListView are always remain the vertical list. Moreover, the CardView widget is a new component that display the item in RecyclerView to make it look better.

In this tutorial, I will teach you some simple steps to coding the RecyclerView in your application. You will just take a little time to complete and make you easy to understand.

Creating a new Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “RecyclerViewExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Project” and Click Next button
5. Lastly, press finish button.

Add Dependency in build.gradle

To activate the RecyclerView in your project, you should add dependency in your build.gradle (Module:app). Just add “compile ‘′” in your dependencies.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile ''
    compile ''

Create a new class

After add your dependency, create a new java class and name it as MyAdapter. This class is the adapter of data set.

Android RecyclerView pic 1

Edit class

In the class extends RecyclerView.Adapter to get the RecycleView method in the class. This class will handler the views (eg. textview) of recyclerview.

RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
– It will automatically called when the RecyclerView need the ViewHolder to represent an item.

void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
– It called by the RecyclerView to display data in specific position.

int getItemCount()
– It return a total item in the dataset.

import android.view.ViewGroup;
import android.widget.TextView;

 * Created by HP on 10/5/2016.
class MyAdapter extends RecyclerView.Adapter {

    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv;

        public ViewHolder(TextView textView) {
            tv = textView;

        public TextView getTv() {
            return tv;

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(new TextView(parent.getContext()));

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;
        vh.getTv().setText("Item " + position);

    public int getItemCount() {
        return 10;

Edit activity.main.xml layout

In the layout, open activity.main.xml and add recyclerview widget to your layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

        android:scrollbars="vertical" />

Edit class

After that, copy below code to your This activity set RecyclerView to linear layout and set the adapter that you created above “MyAdapter” class. You also able to set your layout to Grid View by using GridLayoutManager. In this example, I am using LinearLayoutManager to display the recyclerview.

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    private RecyclerView rv;
    protected void onCreate(Bundle savedInstanceState) {

        rv = (RecyclerView)findViewById(;

        rv.setLayoutManager(new LinearLayoutManager(this));

        rv.setAdapter(new MyAdapter());



Run you Project

Now, you can start your project in your emulator or android device. RecyclerView is very easy to implement, you just need more practice and understanding the source code.

(Android RecyclerView)

Source Code

(Visited 586 times, 1 visits today)

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

2 Responses

  1. BestVada says:

    I see you don’t monetize, don’t waste your traffic, you can earn extra cash every month with new monetization method.
    This is the best adsense alternative for any type of website (they approve all websites), for more details simply search in gooogle:
    murgrabia’s tools

  1. November 17, 2016

    […] previous tutorial, I have implement a simple recyclerview tutorial to let you understand how its work. The recyclerview only display the static content is […]

Leave a Reply

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