How to add dividers and spaces between items in RecyclerView
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Android, spacing and dividers in a RecyclerView are usually implemented with ItemDecoration. That is the right extension point because it lets you add offsets and drawing behavior without cluttering the adapter or hard-coding margins into every item layout.
Use ItemDecoration for Spacing
If you want even spacing between list items, create a custom ItemDecoration and override getItemOffsets. Here is a simple vertical spacing decoration in Kotlin:
Attach it like this:
This keeps the adapter focused on data binding rather than visual polish.
Use DividerItemDecoration for Standard Lines
If you just need a normal divider between rows in a vertical list, Android already provides DividerItemDecoration:
This is the fastest solution when the built-in line style is good enough for the screen.
Drawing a Custom Divider
When you need a specific color, thickness, or inset, write your own decoration and draw the divider yourself:
Then register it:
That gives you exact control without editing every row XML.
Spacing in a Grid Layout
Grids need more care because left and right spacing must be balanced across columns. A list-style spacing class usually produces uneven gaps in a GridLayoutManager.
A common grid decoration looks like this:
The offset math avoids double-width gaps between neighboring columns.
Why Decorations Are Better Than Item Margins
Margins inside the item layout can work for simple lists, but decorations are easier to centralize and reuse. They also let the same row layout appear in different screens with different spacing rules.
That separation matters in large apps, where UI spacing should be adjusted at the list level rather than copied into many XML files.
Common Pitfalls
The biggest mistake is double spacing. If every item gets both top and bottom padding, the gap between two rows becomes larger than intended. Usually you apply one side consistently and add the opposite side only for the first item or first row.
Another pitfall is assuming DividerItemDecoration solves grid spacing. It is mainly useful for linear layouts and does not automatically create balanced spacing across columns.
Developers also sometimes mix item margins and decorations, then wonder why the list looks too loose. Pick one spacing strategy unless you intentionally want both.
Finally, complex drawing logic in onDraw can hurt scroll performance. Decorations should stay lightweight.
Summary
- Use
ItemDecorationto add spacing and dividers toRecyclerViewcleanly. - '
DividerItemDecorationis the simplest choice for standard linear lists.' - Custom decorations are best when you need exact spacing, inset, or color rules.
- Grid layouts need column-aware offset calculations to stay visually balanced.
- Avoid mixing margins and decorations unless the combined effect is deliberate.

