~/blog / recyclerview-last-item-bottom-margin.md
Add Extra Bottom Margin to Last Item in RecyclerView
A quick tip on how to add extra bottom margin to the last item in a RecyclerView using ItemDecoration.
When you have a FAB or bottom navigation, the last item in your RecyclerView might get hidden. Here's how to fix it.
The Problem
The last item in a RecyclerView can be obscured by:
- Floating Action Button (FAB)
- Bottom Navigation Bar
- Bottom App Bar
The Solution: ItemDecoration
Create a custom ItemDecoration that adds extra margin to the last item:
kotlin
class LastItemMarginDecoration(
private val bottomMargin: Int
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
val position = parent.getChildAdapterPosition(view)
val itemCount = state.itemCount
if (position == itemCount - 1) {
outRect.bottom = bottomMargin
}
}
}Usage
kotlin
val margin = resources.getDimensionPixelSize(R.dimen.fab_margin)
recyclerView.addItemDecoration(LastItemMarginDecoration(margin))Alternative: ClipToPadding
Another approach is using clipToPadding:
xml
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="80dp"
android:clipToPadding="false" />Conclusion
Both methods work well. Use ItemDecoration when you need more control, or clipToPadding for a simpler solution.