Hello Guys, Here We are going to learn Android Simple RecyclerView Example. In this, we will see how to add an array list to RecyclerView.

The Array List we use here contains two Textview and one Imageview. First Textview is the Main Text and Second Textview is Sub Text of the RecyclerView item. And the ImageView is displayed to the Left of each RecyclerView item.

Refer to this Official RecyclerView Documentation

So Without Getting Let’s go to Coding to build a Simple RecyclerView Example.

Open Android Studio and Start a New Project for your Simple RecyclerView Example.

First we will write code for RecyclerView in the activity_main.xml.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="4dp"
        tools:listitem="@layout/example_item"
        android:scrollbars="vertical" />

</RelativeLayout>

Here we write code for our RecyclerView Item with Two TextViews to display Text and Sub Text of the Item and one ImageView of the Item. The Imageview is placed to the left of the Textview and the subtext is placed below the main text using layout_toEndOf and layout_below.

example_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.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"
    app:cardCornerRadius="4dp">




    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:padding="15dp" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/imageView"
            android:text="Text"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_toRightOf="@+id/imageView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:layout_marginStart="8dp"
            android:layout_toEndOf="@+id/imageView"
            android:text="Sub Text"
            android:textSize="13sp"
            android:layout_marginLeft="3dp"
            android:layout_toRightOf="@+id/imageView" />

    </RelativeLayout>

</androidx.cardview.widget.CardView>

Now here we will write model class for the RecyclerView. Create a new Java Class with a name ExampleItem and start writing the below code.

ExampleItem.java
package com.codeworld.recyclerviewexample;

public class ExampleItem {
    private int mImageResource;
    private String mText1;
    private String mText2;
    public ExampleItem(int imageResource, String text1, String text2) {
        mImageResource = imageResource;
        mText1 = text1;
        mText2 = text2;
    }
    public int getImageResource() {
        return mImageResource;
    }
    public String getText1() {
        return mText1;
    }
    public String getText2() {
        return mText2;
    }
}

Now We will Write an Adapter for Recyclerview using our layout item example_item.xml. Create a new Java class with the name ExampleAdapter. Now write the code as below.

ExampleAdapter.java
package com.codeworld.recyclerviewexample;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;


public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
    private ArrayList<ExampleItem> mExampleList;

    public static class ExampleViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImageView;
        public TextView mTextView1;
        public TextView mTextView2;

        public ExampleViewHolder(View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.imageView);
            mTextView1 = itemView.findViewById(R.id.textView);
            mTextView2 = itemView.findViewById(R.id.textView2);
        }
    }

    public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
        mExampleList = exampleList;
    }

    @Override
    public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
        ExampleViewHolder evh = new ExampleViewHolder(v);
        return evh;
    }

    @Override
    public void onBindViewHolder(ExampleViewHolder holder, int position) {
        ExampleItem currentItem = mExampleList.get(position);

        holder.mImageView.setImageResource(currentItem.getImageResource());
        holder.mTextView1.setText(currentItem.getText1());
        holder.mTextView2.setText(currentItem.getText2());
    }

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

See Our Python Projects

MainActivity.java
package com.codeworld.recyclerviewexample;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

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

        ArrayList<ExampleItem> exampleList = new ArrayList<>();
        exampleList.add(new ExampleItem(R.drawable.ic_android, "Text 1", "Sub Text 1"));
        exampleList.add(new ExampleItem(R.drawable.ic_audio, "Text 2", "Sub Text 2"));
        exampleList.add(new ExampleItem(R.drawable.ic_sun, "Text 3", "Sub Text 3"));
        exampleList.add(new ExampleItem(R.drawable.ic_account, "Text 4", "Sub Text 4"));
        exampleList.add(new ExampleItem(R.drawable.ic_adb, "Text 5", "Sub Text 5"));
        exampleList.add(new ExampleItem(R.drawable.ic_photo, "Text 6", "Sub Text 6"));
        exampleList.add(new ExampleItem(R.drawable.ic_alarm, "Text 7", "Sub Text 7"));
        exampleList.add(new ExampleItem(R.drawable.ic_alert, "Text 8", "Sub Text 8"));
        exampleList.add(new ExampleItem(R.drawable.ic_airplanemode, "Text 9", "Sub Text 9"));
        exampleList.add(new ExampleItem(R.drawable.ic_contact_mail, "Text 10", "Sub Text 10"));


        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new ExampleAdapter(exampleList);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }
}

Finally, you learned how to create an Android Simple RecyclerView Example, and you can now build real-time Recyclerview apps using this Simple RecyclerView Example.