windows 7
Android tutorial: Horizontal RecyclerView with images and text example |

Android tutorial: Horizontal RecyclerView with images and text example

2017:02:09 / Science and Tutorials

Android code; learn how to create a custom horizontal recyclerview with images and text in Android studio. See an example

You can download this tutorial at the end of this post.

On my previous post, I talked about how to create a vertical list view which is scrollable along the y-axis using RecyclerView. For this Android tutorial, I will show how to create a horizontal list view of images bind with text which is scrollable along the x-axis in Android Studio as shown below. Also, display a toast message on item click.  

If you are a newbie to RecyclerView class, I will partially highlight some of the advantages of it to a listview. RecyclerView is an advancement of listView. listView does not directly support the horizontal listing of elements, unlike RecyclerView for which you can easily implement both vertical and horizontal listing of items on your android app.

To get started, navigate to the .gradle file and add the following RecyclerView library onto your project. Copy the following code and include inside your dependencies as shown below.

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


XML files

Contain the vertical adapter class and the list of images and text added to the vertical recycler view as shown below.

public class MainActivity extends Activity {     RecyclerView horizontal_recycler_view;     HorizontalAdapter horizontalAdapter;     private List<Data> data;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         horizontal_recycler_view= (RecyclerView) findViewById(;         data = fill_with_data();         horizontalAdapter=new HorizontalAdapter(data, getApplication());         LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);         horizontal_recycler_view.setLayoutManager(horizontalLayoutManager);         horizontal_recycler_view.setAdapter(horizontalAdapter);     }     public List<Data> fill_with_data() {         List<Data> data = new ArrayList<>();         data.add(new Data( R.drawable.img1, "Image 1"));         data.add(new Data( R.drawable.img2, "Image 2"));         data.add(new Data( R.drawable.img3, "Image 3"));         data.add(new Data( R.drawable.img1, "Image 1"));         data.add(new Data( R.drawable.img2, "Image 2"));         data.add(new Data( R.drawable.img3, "Image 3"));         data.add(new Data( R.drawable.img1, "Image 1"));         data.add(new Data( R.drawable.img2, "Image 2"));         data.add(new Data( R.drawable.img3, "Image 3"));         return data;     }     public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.MyViewHolder> {         List<Data> horizontalList = Collections.emptyList();         Context context;         public HorizontalAdapter(List<Data> horizontalList, Context context) {             this.horizontalList = horizontalList;             this.context = context;         }         public class MyViewHolder extends RecyclerView.ViewHolder {             ImageView imageView;             TextView txtview;             public MyViewHolder(View view) {                 super(view);                 imageView=(ImageView) view.findViewById(;                 txtview=(TextView) view.findViewById(;             }         }         @Override         public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {             View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.vertical_menu, parent, false);             return new MyViewHolder(itemView);         }         @Override         public void onBindViewHolder(final MyViewHolder holder, final int position) {             holder.imageView.setImageResource(horizontalList.get(position).imageId);             holder.txtview.setText(horizontalList.get(position).txt);             holder.imageView.setOnClickListener(new View.OnClickListener() {                 @Override                 public void onClick(View v) {                     String list = horizontalList.get(position).txt.toString();                     Toast.makeText(MainActivity.this, list, Toast.LENGTH_SHORT).show();                 }             });         }         @Override         public int getItemCount()         {             return horizontalList.size();         }     } }

gets, sets and returns images and text as shown below.

public class Data {     public int imageId;     public String txt;     Data( int imageId, String text) {         this.imageId = imageId;         this.txt=text;     } }


Layout for our list of images and text.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=""     android:orientation="vertical"     android:layout_marginTop="10dp"     android:layout_width="match_parent"     android:layout_height="match_parent">     <LinearLayout         android:layout_width="fill_parent"         android:orientation="vertical"         android:layout_height="fill_parent">     <ImageView         android:layout_width="wrap_content"         android:paddingRight="10dp"         android:layout_height="wrap_content"         android:id="@+id/imageview"/>     <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textColor="#fff"         android:id="@+id/txtview"/> </LinearLayout> </LinearLayout>


The main layout of our application. Contains the recyclerview widget responsible for listing of our data vertically.

<RelativeLayout xmlns:android=""     xmlns:tools="" android:layout_width="match_parent"     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">     <LinearLayout         android:layout_width="fill_parent"         android:background="#f00fff"         android:id="@+id/ln1"         android:layout_height="50dp">         <TextView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="Horizontal RecyclerView"             android:textSize="20dp"             android:textStyle="bold"             android:textColor="#fff"             />     </LinearLayout>     <LinearLayout         android:layout_width="fill_parent"         android:background="#27002b"         android:layout_below="@+id/ln1"         android:layout_height="wrap_content">         <             android:id="@+id/horizontal_recycler_view"             android:background="#27002b"             android:layout_width="wrap_content"             android:layout_height="fill_parent"             />     </LinearLayout> </RelativeLayout>

I hope this tutorial has significantly helped you. Please, you can comment below for more inquiry.

You may also like:

Leave a comment

Subscribe to get replies direct to your mailbox

Submit comment