How to set RecyclerView app:layoutManager="" from XML?

AndroidXmlAndroid RecyclerviewAndroidx

Android Problem Overview


How to set RecyclerView layoutManager from XML?

    <android.support.v7.widget.RecyclerView
        app:layoutManager="???"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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);

Solution 3 - Android

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"

Solution 4 - Android

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

Solution 5 - Android

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

Solution 6 - Android

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

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
QuestionIlya GazmanView Question on Stackoverflow
Solution 1 - AndroidGabriele MariottiView Answer on Stackoverflow
Solution 2 - AndroidMina FawzyView Answer on Stackoverflow
Solution 3 - AndroidMaxView Answer on Stackoverflow
Solution 4 - Androidandroid developerView Answer on Stackoverflow
Solution 5 - AndroidAkinyemi JamiuView Answer on Stackoverflow
Solution 6 - AndroidG GaneshView Answer on Stackoverflow