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.
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.
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.
Then apply it:
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.
Apply it like this:
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.
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.
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
DividerItemDecorationfor simple divider lines in aRecyclerView. - Use a custom
ItemDecorationwhen 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
ItemDecorationrather than scattering margin logic across item layouts.
Related reading
- How to add Done button to Numpad in iOS using Swift?
- How to add HeaderView in UICollectionView like UITableView's tableHeaderView
- How to add leading padding to view added inside an UIStackView
- How to add leading padding to view added inside an UIStackView
- How to add 'libs' folder in Android Studio?
- How to add line break for UILabel?
- How to add manifest permission to an application?
- How to add minutes to current time in swift
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.