RecyclerView
Android
XML
layoutManager
app development

How to set RecyclerView applayoutManager from XML?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Yes, you can configure a RecyclerView layout manager directly from XML by using the app:layoutManager attribute. That works well when the layout choice is static, because the view is inflated with the right manager before your Activity or Fragment assigns the adapter.

The Basic XML Setup

For a standard vertical list, the XML is straightforward:

xml
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout
3    xmlns:android="http://schemas.android.com/apk/res/android"
4    xmlns:app="http://schemas.android.com/apk/res-auto"
5    android:layout_width="match_parent"
6    android:layout_height="match_parent"
7    android:orientation="vertical">
8
9    <androidx.recyclerview.widget.RecyclerView
10        android:id="@+id/recyclerView"
11        android:layout_width="match_parent"
12        android:layout_height="match_parent"
13        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
14        android:orientation="vertical" />
15
16</LinearLayout>

After that, your code can focus on the adapter:

kotlin
1class MainActivity : AppCompatActivity() {
2    override fun onCreate(savedInstanceState: Bundle?) {
3        super.onCreate(savedInstanceState)
4        setContentView(R.layout.activity_main)
5
6        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
7        recyclerView.adapter = MessageAdapter(loadMessages())
8    }
9}

You do not have to call setLayoutManager() in code if XML already created it.

Other Built-In LayoutManagers

Grid Layout

A grid needs app:spanCount:

xml
1<androidx.recyclerview.widget.RecyclerView
2    android:id="@+id/photoGrid"
3    android:layout_width="match_parent"
4    android:layout_height="match_parent"
5    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
6    app:spanCount="3" />

Staggered Grid Layout

A staggered grid typically needs both spanCount and orientation:

xml
1<androidx.recyclerview.widget.RecyclerView
2    android:id="@+id/staggeredGrid"
3    android:layout_width="match_parent"
4    android:layout_height="match_parent"
5    app:layoutManager="androidx.recyclerview.widget.StaggeredGridLayoutManager"
6    app:spanCount="2"
7    android:orientation="vertical" />

Those XML attributes are useful because they keep fixed UI structure in the layout file instead of scattering it through code.

XML in a Fragment Works the Same Way

The same attribute works when the RecyclerView lives inside a fragment layout. After inflating the fragment view, you normally just bind the adapter and any decorations:

kotlin
1override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
2    val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
3    recyclerView.adapter = ArticleAdapter(items)
4    recyclerView.addItemDecoration(DividerItemDecoration(requireContext(), RecyclerView.VERTICAL))
5}

The divider is configured in code because item decorations are behavior, while the layout manager choice is structural.

When XML Is a Good Fit

XML is the right choice when the screen always uses the same layout pattern. A chat screen that is always a vertical list or a gallery screen that is always a three-column grid benefits from that declarative setup.

It also makes previewing the layout easier, because the XML already describes the intended structure. You can even add tools:listitem for preview-only rendering in the layout editor without affecting runtime behavior.

When Code Is Better

If the layout manager depends on runtime conditions, code is usually clearer. For example, a tablet might use more columns than a phone:

kotlin
val spanCount = if (resources.configuration.smallestScreenWidthDp >= 600) 4 else 2
recyclerView.layoutManager = GridLayoutManager(this, spanCount)
recyclerView.adapter = ProductAdapter(products)

Trying to force that kind of dynamic decision into XML usually makes the screen harder to understand.

Custom LayoutManagers from XML

You are not limited to the built-in managers, but a custom one must support the constructor shape that inflation expects. If it cannot be instantiated from XML attributes, inflation fails before the UI appears.

That is why many teams use XML for LinearLayoutManager, GridLayoutManager, and StaggeredGridLayoutManager, but switch to code for custom layout manager logic.

Common Pitfalls

  • Forgetting xmlns:app="http://schemas.android.com/apk/res-auto" on the root layout prevents app:layoutManager and app:spanCount from being parsed.
  • Using an old support-library class name in an AndroidX project causes inflation errors.
  • Omitting app:spanCount for grid-based managers leads to invalid configuration.
  • Setting one layout manager in XML and replacing it later in code can make scrolling and decoration bugs harder to trace.
  • Treating android:orientation as a universal RecyclerView property is misleading; it matters only for layout managers that actually use orientation.

Summary

  • Use app:layoutManager to configure a RecyclerView layout manager directly in XML.
  • Keep the app namespace on the root element so the custom attributes are recognized.
  • Use fully qualified AndroidX class names such as androidx.recyclerview.widget.LinearLayoutManager.
  • Add supporting attributes such as app:spanCount when the chosen manager needs them.
  • Prefer XML for fixed layouts and programmatic configuration for runtime-dependent layouts.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.