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

114,903

Solution 1

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

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

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

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

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"/>
Share:
114,903

Related videos on Youtube

Ilya Gazman
Author by

Ilya Gazman

My latest app Facetocall it had a site too: facetocall.com Developed with my latest SDK

Updated on September 16, 2020

Comments

  • Ilya Gazman
    Ilya Gazman almost 4 years

    How to set RecyclerView layoutManager from XML?

        <android.support.v7.widget.RecyclerView
            app:layoutManager="???"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    • dieter_h
      dieter_h over 8 years
      See documentation: recyclerview:layoutManager
    • Ilya Gazman
      Ilya Gazman over 8 years
      @dieter_h can you provide an answer with GridLayoutManager example?
    • thetonrifles
      thetonrifles over 8 years
      You can use app:layoutManager="android.support.v7.widget.GridLayoutManag‌​er". 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
  • androidguy
    androidguy over 7 years
    And you can use android:orientation on the RecyclerView element to control the orientation of e.g. LinearLayoutManager
  • Ilya Gazman
    Ilya Gazman over 7 years
    What is your addition to Gabriele Mariotti answer?
  • Mina Fawzy
    Mina Fawzy over 7 years
    explain what equivalent to in code but with anther example LinearLayout
  • Fadli
    Fadli over 7 years
    @Gabriele Mariotti I checked the implementation and it uses reflection. Is that okay from performance wise?
  • Tony Chan
    Tony Chan over 6 years
    As of this comment it seems the docs no longer have that useful explanation. So frustrating that @Google can't even keep their docs correct.
  • Tony Chan
    Tony Chan over 6 years
    Regarding my previous comment I've created an issue ticket for it here.
  • rf43
    rf43 over 6 years
    @UdiOshi to make it horizontal, you need to add android:orientation="horizontal" to the RecyclerView xml
  • android developer
    android developer over 6 years
    Are there more available attributes in case I use GridLayoutManager ? Is it possible to set its number of columns (spanCount) ?
  • jauser
    jauser about 6 years
    @androiddeveloper Yes, e.g. to set it to three columns use app:spanCount="3"
  • frapeti
    frapeti almost 6 years
    androidx.recyclerview.widget.LinearLayoutManager for androidX
  • Aaditya Brahmbhatt
    Aaditya Brahmbhatt almost 6 years
    is there a way to define number of columns?
  • Pratik Butani
    Pratik Butani over 5 years
    You can set no. of columns when you use GridLayoutManager by adding app:spanCount="2"
  • mhemdan
    mhemdan over 5 years
    @nimi0112 use this app:layoutManager="androidx.recyclerview.widget.GridLayoutMa‌​nager"
  • Abhinav Saxena
    Abhinav Saxena over 5 years
    Please switch to AndroidX.
  • Abhinav Saxena
    Abhinav Saxena over 5 years
    future compatible approach.
  • Robin Hood
    Robin Hood about 5 years
    Same here... Androidx about to take off 🚀
  • Omkar
    Omkar about 5 years
    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
    Omkar about 5 years
    thanks, but still it is wrong namespace. It should be android for orientation. It doesn't work with app namespace everything else is perfect.
  • android developer
    android developer about 5 years
    @Omkar Correct again. Sorry. Updated.
  • Jimit Patel
    Jimit Patel about 5 years
    Thanks I was looking for spanCount found it here. Thats why upvote to the answer :)
  • mochadwi
    mochadwi over 4 years
    you can also use tools:layoutManager, tools:spanCount (even though the auto-complete doesn't show it) and if you don't want to override any of your recyclerview setup (or prefer programatically)