Skip to content

Instantly share code, notes, and snippets.

Last active February 29, 2024 09:58
Show Gist options
  • Save polbins/e37206fbc444207c0e92 to your computer and use it in GitHub Desktop.
Save polbins/e37206fbc444207c0e92 to your computer and use it in GitHub Desktop.
Simple RecyclerView Divider

Simple RecyclerView Divider

Simple Horizontal Divider Item Decoration for RecyclerView

    mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(

NOTE: Add item decoration prior to setting the adapter

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=""
android:height="1dp" />
<solid android:color="@color/dark_gray" />
public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
public SimpleDividerItemDecoration(Context context) {
mDivider = context.getResources().getDrawable(R.drawable.line_divider);
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
Copy link

fazlurr commented Feb 6, 2015

Works like a charm, Thanks!

Copy link

It works, thanks

Copy link

Is there any way to make this work with a grid layout?

Copy link

bohbra commented Mar 13, 2015

I notice the divider is being drawn on top of the scrollbar, anyway around this?

Copy link

Leaking commented Mar 17, 2015

the onDrawover() method will draw the divider on the top of the item view。as a result, it also draws on the top of the scrollbar

Copy link

PsyGik commented Apr 22, 2015

The proposed code above adds a divider to every item including the last item.

So one can even use

  android:background="@color/holo_gray_light" />

in the layout for the item to get the same effect.

As a workaround, in the adapter I check if the child is the last item and change the visibility of the divider accordingly.

    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
       if(position==getItemCount()) //check if this is the last child, if yes then hide the divider

Any suggestions on this are welcome

Copy link

It works like a charm! I wanted to use only the onDraw method so I added this other override to add space for the drawable:

public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    outRect.bottom = mDivider.getIntrinsicHeight();

I hope it helps someone. Thanks!

Copy link


A couple quick comments: because the views are recycled, you'll want to make it visible if it's not the last item. You'll also never trigger this because position will never equal getItemCount() (as long as you're returning something like List.size()). Otherwise looks like a solid alternative that'll work with ListViews too :)

Here's a quick update to your code:

int visible = (position == getItemCount()-1) ? View.GONE : View.VISIBLE;

Copy link

Thanks, works well.
Though getDrawable(int) is deprecated now. Use this instead:

ContextCompat.getDrawable(context, R.drawable.line_divider);

Copy link

Thank's so much... it's work ....

Copy link

bangiqi commented Oct 6, 2015

@jonasbleyl : where is ContextCompat declaration?

Copy link

ContextCompat is in the support library. ContextCompat.getDrawable(context, resourceID)

Copy link

yosisah commented Nov 10, 2015

Thanks, works great.

Copy link


Copy link

Gnzlt commented Jan 4, 2016

Nice Work!
I've also included the default Android listDivider drawable inside the constructor if you don't want to implement a custom one:

public SimpleDividerItemDecoration(Context context) {
    final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.listDivider});
    mDivider = a.getDrawable(0);

Copy link

aantthony commented Apr 23, 2016

I used this:

ResourcesCompat.getDrawable(context.getResources(), R.drawable.line_divider, context.getTheme());

Copy link

I am not able to set divider to my RecyclerView can somebody help me for this?......

ResourcesCompat.getDrawable(context.getResources(), R.drawable.line_divider, context.getTheme());

where should i used this above code?please specify the location

Copy link

Tried many, but this works like magic.


Copy link

it works
thanks alot

Copy link

Works perfectly. Thank you

Copy link


Copy link

I think custom drawable is not work

Copy link

axellebot commented Mar 7, 2017

Maybe use

public SimpleDividerItemDecoration(Context context) { mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider); }

Copy link

Thanks a lot

Copy link

Thanks,It worked!!

Copy link

how can i made this?

Copy link

val linearLayoutManager = LinearLayoutManager(this)
val dividerItemDecoration = DividerItemDecoration(recyclerView.context, linearLayoutManager.orientation)

// Init recyclerView
recyclerView.layoutManager = linearLayoutManager

Copy link

Geet-Thakur01 commented May 9, 2018

Use "card view" for item layout and set "margin top" according to your requirement.

Copy link

jkhin commented Sep 4, 2018

It works beauty... Thanks!

Copy link

vsg24 commented Jan 9, 2019

If you don't want the last item to have a divider line after it, simply change for (int i = 0; i < childCount; i++) { to for (int i = 0; i < childCount - 1; i++) { and it will skip the last item divider.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment