Previewing horizontal recyclerview in android studio

AndroidAndroid StudioAndroid Recyclerview

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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"
    />

Solution 2 - Android

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"
    />

Solution 3 - Android

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"
    ../>

Solution 4 - Android

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

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

Solution 5 - Android

Otherwise you can use in java file :

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfedepaolView Question on Stackoverflow
Solution 1 - AndroidL.VelazquezView Answer on Stackoverflow
Solution 2 - AndroidNicola GallazziView Answer on Stackoverflow
Solution 3 - AndroidGabriele MariottiView Answer on Stackoverflow
Solution 4 - AndroidManzur AlahiView Answer on Stackoverflow
Solution 5 - AndroidSumit RathodView Answer on Stackoverflow