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.
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:
Then apply it:
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.
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.
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
DividerItemDecorationfor the simplest vertical or horizontal list divider. - Apply a custom drawable when the default divider style is not sufficient.
- Write a custom
ItemDecorationwhen 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.

