217

How to set RecyclerView layoutManager from XML?

    <android.support.v7.widget.RecyclerView
        app:layoutManager="???"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • See documentation: [recyclerview:layoutManager](http://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#attr_android.support.v7.recyclerview:layoutManager) – dieter_h Feb 28 '16 at 08:27
  • 1
    @dieter_h can you provide an answer with GridLayoutManager example? – Ilya Gazman Feb 28 '16 at 08:31
  • 4
    You can use `app:layoutManager="android.support.v7.widget.GridLayoutManager"`. Constructor with four arguments will be used (`Context`, `AttributeSet`, `int`, `int`). According to documentation this is the _constructor used when layout manager is set in XML by RecyclerView attribute layoutManager. If spanCount is not specified in the XML, it defaults to a single column_ – andrea.petreri Feb 28 '16 at 08:49

7 Answers7

360

As you can check in the doc:

Class name of the Layout Manager to be used.

The class must extend androidx.recyclerview.widget.RecyclerViewView$LayoutManager and have either a default constructor or constructor with the signature (android.content.Context, android.util.AttributeSet, int, int)

If the name starts with a '.', application package is prefixed. Else, if the name contains a '.', the classname is assumed to be a full class name. Else, the recycler view package (androidx.appcompat.widget) is prefixed

With androidx you can use:

<androidx.recyclerview.widget.RecyclerView
     xmlns:app="http://schemas.android.com/apk/res-auto"
     app:layoutManager="androidx.recyclerview.widget.GridLayoutManager">

With the support libraries you can use:

<android.support.v7.widget.RecyclerView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layoutManager="android.support.v7.widget.GridLayoutManager" >

Also you can add these attributes:

  • android:orientation = "horizontal|vertical": to control the orientation of the LayoutManager (eg:LinearLayoutManager)
  • app:spanCount: to set the number of columns for GridLayoutManager

Example:

<androidx.recyclerview.widget.RecyclerView
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    app:spanCount="2"
    ...>

or:

<androidx.recyclerview.widget.RecyclerView
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    android:orientation="vertical"
    ...>

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.

Community
  • 1
  • 1
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
94

if you want use it with LinearLayoutManager

<android.support.v7.widget.RecyclerView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager" >

that equivalent to

LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
94

And I came here looking for androidx version though it was pretty easy to figure out, here it is

LinearLayoutManager:

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

Example:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

GridLayoutManager:

app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"

Example:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:spanCount="2"
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"/>

As you can see in examples above you can control the orientation from within xml using

android:orientation="vertical"

and

android:orientation="horizontal"

And to set the number of columns for GridLayoutManager using

app:spanCount="2"
Atiq
  • 14,435
  • 6
  • 54
  • 69
18

The most common ones that I use are:

<androidx.recyclerview.widget.RecyclerView
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" 
    tools:listitem="@layout/grid_item"
    android:orientation="vertical" app:spanCount="3"/>

And:

<androidx.recyclerview.widget.RecyclerView
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:listitem="@layout/grid_item"
    android:orientation="vertical"/>

It's recommended to set listitem , so that you'd see how it could look in the preview of the layout editor.

If you want to have the order reversed though, I think you have to do it in code instead, and use "tools" in XML if you really want to see something...

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • which is `apps` and `app` namespaces you have used in above xml? because I am getting build time error `Android resource linking failed - AAPT: error: attribute orientation`, if I use `app:orientation`. – Omkar Apr 24 '19 at 12:39
  • thanks, but still it is wrong namespace. It should be `android` for `orientation`. It doesn't work with `app` namespace everything else is perfect. – Omkar Apr 25 '19 at 12:14
  • @Omkar Correct again. Sorry. Updated. – android developer Apr 25 '19 at 14:58
3

This worked for me - just add app:layoutManager="LinearLayoutManager" and you're good to go

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recordItemList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:clipToPadding="false"
        android:scrollbars="none"
        app:layoutManager="LinearLayoutManager"
        app:stackFromEnd="true"
        app:reverseLayout="true"/>
Akinyemi Jamiu
  • 431
  • 3
  • 10
1

You can set the layout manager for recyclerview like this, app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

kk.
  • 3,747
  • 12
  • 36
  • 67
G Ganesh
  • 103
  • 7
0

Example

implementation 'com.android.support:recyclerview-v7:28.0.0'
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
    />

layoutManager can be android.support.v7.widget.LinearLayoutManager, android.support.v7.widget.GridLayoutManager

[Read more here]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205