RecyclerView
Android Development
Divider Line
Mobile App Development
Android UI

How can a divider line be added in an Android RecyclerView?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

RecyclerView does not have a built-in divider property like the old ListView, so separators are added through item decoration or item layout design. The simplest solution is DividerItemDecoration, but custom decorations are often better when spacing, insets, or section-aware behavior matter. The right choice depends on how much control you need over the line’s appearance and placement.

Use DividerItemDecoration for the Standard Case

If you want a normal divider between rows in a vertical list, use the platform-provided item decoration.

kotlin
1import android.os.Bundle
2import androidx.appcompat.app.AppCompatActivity
3import androidx.recyclerview.widget.DividerItemDecoration
4import androidx.recyclerview.widget.LinearLayoutManager
5import androidx.recyclerview.widget.RecyclerView
6
7class MainActivity : AppCompatActivity() {
8    override fun onCreate(savedInstanceState: Bundle?) {
9        super.onCreate(savedInstanceState)
10
11        val recyclerView = RecyclerView(this)
12        recyclerView.layoutManager = LinearLayoutManager(this)
13        recyclerView.adapter = SimpleAdapter(listOf("One", "Two", "Three"))
14
15        val divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
16        recyclerView.addItemDecoration(divider)
17
18        setContentView(recyclerView)
19    }
20}

This works well when you just need a plain line between items.

Apply a Custom Divider Drawable

The default divider uses the current theme, which may not match your design. You can provide your own drawable.

Create res/drawable/recycler_divider.xml:

xml
1<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
2    <size android:height="1dp" />
3    <solid android:color="@android:color/darker_gray" />
4</shape>

Then apply it:

kotlin
1import androidx.core.content.ContextCompat
2import androidx.recyclerview.widget.DividerItemDecoration
3
4val divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
5ContextCompat.getDrawable(this, R.drawable.recycler_divider)?.let { drawable ->
6    divider.setDrawable(drawable)
7}
8recyclerView.addItemDecoration(divider)

This is usually enough for design-system alignment.

Create a Custom ItemDecoration for More Control

If you need insets, conditional drawing, or no divider after the last item, write a custom decoration.

kotlin
1import android.graphics.Canvas
2import android.graphics.Paint
3import androidx.recyclerview.widget.RecyclerView
4
5class SimpleLineDecoration(
6    color: Int,
7    private val heightPx: Float,
8    private val insetStartPx: Float = 0f,
9    private val insetEndPx: Float = 0f,
10) : RecyclerView.ItemDecoration() {
11
12    private val paint = Paint().apply {
13        this.color = color
14        style = Paint.Style.FILL
15    }
16
17    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
18        val childCount = parent.childCount
19
20        for (i in 0 until childCount - 1) {
21            val child = parent.getChildAt(i)
22            val top = child.bottom.toFloat()
23            val bottom = top + heightPx
24            val left = parent.paddingLeft + insetStartPx
25            val right = parent.width - parent.paddingRight - insetEndPx
26            c.drawRect(left, top, right, bottom, paint)
27        }
28    }
29}

This lets you skip the last divider and match complex UI spacing.

Add Spacing Along With the Divider

If the line has thickness, account for item offsets so content does not overlap it.

kotlin
1import android.graphics.Rect
2import android.view.View
3import androidx.recyclerview.widget.RecyclerView
4
5override fun getItemOffsets(
6    outRect: Rect,
7    view: View,
8    parent: RecyclerView,
9    state: RecyclerView.State
10) {
11    if (parent.getChildAdapterPosition(view) != state.itemCount - 1) {
12        outRect.bottom = heightPx.toInt()
13    }
14}

Without offsets, custom dividers can draw over item content or appear clipped.

Horizontal Lists and Grids

Divider strategy must match layout manager. For horizontal lists, use horizontal orientation. For grids, simple linear dividers usually look wrong because internal cell boundaries are more complex.

For grids, consider:

  • item background borders
  • custom grid-aware decoration
  • spacing-only separators instead of explicit lines

Do not force DividerItemDecoration into a grid layout unless you have tested the visual result carefully.

Material and Theme Considerations

In Material-based apps, divider color should come from theme resources rather than hardcoded colors. That keeps contrast acceptable in light and dark mode. Also test item decorations with edge-to-edge layouts and animated item changes because decorations can look fine in static lists but wrong during transitions.

Common Pitfalls

One common mistake is forgetting to match divider orientation with the layout manager. Another is using the default theme divider and assuming it matches app design. Custom decorators also frequently skip item offsets, causing visual overlap. Developers often draw a divider after the last item when the design does not want one. Finally, grid layouts are treated like simple vertical lists, which usually produces awkward separators.

Summary

  • Use DividerItemDecoration for the simplest vertical or horizontal list divider.
  • Apply a custom drawable when the default divider style is not sufficient.
  • Write a custom ItemDecoration when you need insets, skipping rules, or custom drawing.
  • Add item offsets when custom dividers consume space.
  • Test divider behavior with the actual layout manager and theme.
  • Treat grids and animated lists as special cases, not as simple line-list variants.

Course illustration
Course illustration

All Rights Reserved.