83

I found out how to preview a list item by using

tools:listitem="@layout/my_item_layout"

Android studio however is previewing the recyclerview as a vertical list. Is there a way to tell Android Studio to display the layout preview in a horizontal fashion using LinearLayoutManager?

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
fedepaol
  • 6,834
  • 3
  • 27
  • 34

7 Answers7

156

Add a LayoutManager and set a horizontal orientation.

Here an example:

<android.support.v7.widget.RecyclerView
    android:id="@+id/homesRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
    android:layout_centerVertical="true"
    />
Floern
  • 33,559
  • 24
  • 104
  • 119
L.Velazquez
  • 1,696
  • 1
  • 11
  • 7
  • 30
    you can also add them using the tools namespace (i.e. `tools:orientation` and `tools:layoutManager`) and then it only impacts the IDE preview and you can continue setting those values in code. – gMale Jun 26 '17 at 03:58
  • 6
    Replace `app:layoutManager="android.support.v7.widget.LinearLayoutManager"` with `app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"` when using AndroidX libraries :) – stkent Aug 24 '21 at 17:12
29

If you are using androidx libraries:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/homesRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    android:layout_centerVertical="true"
    />
Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
  • This converted by RecyclerView to horizontal but `tools:listitem` did not work for me with `androidx.recyclerview:recyclerview:1.1.0-alpha01`. I could only see the horizontal list in design mode if I didn't use `tools:listitem` – Michael Osofsky May 15 '19 at 21:41
  • 1
    I figured out it was my fault because my list item layout specified `android:layout_width="match_parent"`. By changing it to `android:layout_width="wrap_content"` I can no scroll horizontally in Android Studio's design tab using `androidx.recyclerview:recyclerview:1.1.0-alpha01`. – Michael Osofsky May 15 '19 at 22:59
13

Just use in your layout the app:layoutManager and the android:orientation attributes and add them also using the tools namespace.
Something like:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:orientation="horizontal"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:listitem="@layout/ly_item"
    tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:orientation="horizontal"
    ../>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 4
    This should be the top answer. "tools" effects only the preview. "app:" actually wires your RV to a layout manager, which might not be the preferred behavior – PatchesDK Aug 31 '20 at 20:52
  • hi gabriele could help me out here -> https://stackoverflow.com/questions/76786965/facing-issue-while-validating-amongst-the-edittexts-in-recyclerview – android dev Jul 28 '23 at 11:20
7

the accepted answer works accurately. For androidx just use this in your recyclerView

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" android:orientation="horizontal"

Manzur Alahi
  • 1,870
  • 23
  • 19
0

AndroidX

<androidx.recyclerview.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
    tools:listitem="@layout/item"
    tools:orientation="horizontal"/>

[Read more here]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
0

To show the preview of the RecyclerView in Android, follow these steps:

  • Add this line in the parent layout widget:
xmlns:tools="http://schemas.android.com/tools"
  • Now add a property in the RecyclerView:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="horizontal"

Example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerQueueItems"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:itemCount="10"
        tools:listitem="@layout/item_queue_token_number" 
    />

</LinearLayout>
ankushlokhande
  • 870
  • 5
  • 20
-6

Otherwise you can use in java file :

mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));