RecyclerView
Android Development
UI Design
ItemDecoration
Android Studio

How to add dividers and spaces between items in RecyclerView

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

In RecyclerView, the usual way to add visual separation between items is ItemDecoration. That gives you a clean separation between item layout code and list-level decoration, whether you want simple dividers, pure spacing, or both at the same time.

Use DividerItemDecoration for Basic Dividers

If you only need a standard divider line, Android already provides DividerItemDecoration.

kotlin
1val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
2
3val divider = DividerItemDecoration(
4    recyclerView.context,
5    DividerItemDecoration.VERTICAL
6)
7
8recyclerView.addItemDecoration(divider)

This is the quickest solution for a vertically scrolling list.

The orientation must match your layout manager. For a horizontal list, use the horizontal divider orientation instead.

Use a Custom Drawable for Divider Appearance

If the default divider does not match your design, provide your own drawable.

xml
1<!-- res/drawable/custom_divider.xml -->
2<shape xmlns:android="http://schemas.android.com/apk/res/android"
3    android:shape="rectangle">
4    <size android:height="1dp" />
5    <solid android:color="#D0D0D0" />
6</shape>

Then apply it:

kotlin
1val divider = DividerItemDecoration(
2    recyclerView.context,
3    DividerItemDecoration.VERTICAL
4)
5
6ContextCompat.getDrawable(this, R.drawable.custom_divider)?.let {
7    divider.setDrawable(it)
8}
9
10recyclerView.addItemDecoration(divider)

This keeps the divider styling in resources, which is easier to maintain than drawing pixels directly in code for a simple case.

Add Space With a Custom ItemDecoration

If what you really want is spacing instead of a visible line, create a custom decoration that adjusts item offsets.

kotlin
1import android.graphics.Rect
2import android.view.View
3import androidx.recyclerview.widget.RecyclerView
4
5class VerticalSpaceDecoration(
6    private val spacePx: Int
7) : RecyclerView.ItemDecoration() {
8
9    override fun getItemOffsets(
10        outRect: Rect,
11        view: View,
12        parent: RecyclerView,
13        state: RecyclerView.State
14    ) {
15        outRect.bottom = spacePx
16    }
17}

Apply it like this:

kotlin
val space = resources.getDimensionPixelSize(R.dimen.list_item_spacing)
recyclerView.addItemDecoration(VerticalSpaceDecoration(space))

This is a common pattern when cards or list items already have their own backgrounds and you only want breathing room between them.

Avoid Extra Space After the Last Item

A small refinement is to skip the last item so the list does not end with unnecessary bottom spacing.

kotlin
1class VerticalSpaceDecoration(
2    private val spacePx: Int
3) : RecyclerView.ItemDecoration() {
4
5    override fun getItemOffsets(
6        outRect: Rect,
7        view: View,
8        parent: RecyclerView,
9        state: RecyclerView.State
10    ) {
11        val position = parent.getChildAdapterPosition(view)
12        val last = state.itemCount - 1
13
14        if (position != last) {
15            outRect.bottom = spacePx
16        }
17    }
18}

That small check often makes the list look more polished.

Combining Dividers and Spacing

You can attach more than one decoration to the same RecyclerView.

kotlin
recyclerView.addItemDecoration(divider)
recyclerView.addItemDecoration(VerticalSpaceDecoration(space))

This works, but be intentional. Divider plus spacing is not always visually necessary, and stacking decorations can easily make the list feel too busy.

A good rule is:

  • use a divider when you want a line-based separation
  • use spacing when you want visual breathing room
  • combine both only when the design clearly benefits from it

Grid and Horizontal Cases

For grids, spacing logic usually needs to consider the span count and each item’s column. A decoration written for a vertical linear list often looks wrong in a grid.

Likewise, a horizontal RecyclerView should add left or right offsets rather than bottom offsets.

So do not assume one decoration class fits all layouts. ItemDecoration is flexible, but the offset logic must match the layout manager.

Common Pitfalls

The most common pitfall is adding margins directly inside each item view instead of using ItemDecoration for list-level separation. That makes spacing logic harder to reuse and maintain.

Another mistake is forgetting the layout orientation when using DividerItemDecoration.

A third issue is applying bottom spacing to every item and accidentally leaving an awkward trailing gap after the last element.

Finally, some developers stack multiple decorations without checking the final visual result carefully. Small spacing and divider values can add up quickly.

Summary

  • Use DividerItemDecoration for simple divider lines in a RecyclerView.
  • Use a custom ItemDecoration when you want spacing instead of a line.
  • Skip the last item if you do not want trailing space at the end of the list.
  • Match the decoration logic to the layout orientation or grid structure.
  • Keep list separation in ItemDecoration rather than scattering margin logic across item 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.