Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Equal column spacing for Android RecyclerView GridLayoutManager by using custom ItemDecoration


public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {

    private int mItemOffset;

    public ItemOffsetDecoration(int itemOffset) {
        mItemOffset = itemOffset;

    public ItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId) {

    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
            RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.set(mItemOffset, mItemOffset, mItemOffset, mItemOffset);


In your source code, add ItemOffsetDecoration to your recyclerview.
Item offset value should be half size of the actual value you want to add as space between items.

mRecyclerView.setLayoutManager(new GridLayoutManager(context, NUM_COLUMNS);
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(context, R.dimen.item_offset);

Also, set item offset value as padding for its recyclerview, and specify android:clipToPadding=false.


DONE. You will get an equal spaces around items.

Copy link

crazyhitty commented Feb 15, 2016

Thanks, works like a charm !

Copy link

vishalkale commented May 3, 2016

Thanks, I need some help on other problem.
I want to make items equally spaced in a RecyclerView. Items are not displayed in Grid but are displayed Linearly.
Suppose screen can hold 5 items. Now if list contains less than 5 items then they should take up the whole screen by equally increasing space between them. And if no of items are greater than 5 then user can scroll and the minimum space is maintained between 2 items.
How can I achieve this.

Copy link

krishnameena commented Jul 5, 2016

It is working fine but I am calling new Activity from the recyclerview and when I am coming back to my previous activity the space automatically becomes 2 times every time. Any solution for that ?

Copy link

tiwiz commented Aug 17, 2016

@krishnameena: have you tried calling RecyclerView.removeItemDecoration() in the onStop method? :-)

Copy link

malenalbc commented Sep 28, 2016

@tiwiz You just saved me loads of fruitless searching. Thank you very much!

Copy link

hcbpassos commented Dec 6, 2016

Thanks! I spent whole day trying to find a solution.

Copy link

him407 commented Dec 30, 2016

Thank you, it is work for my apps.

Copy link

diceroll123 commented Jan 8, 2017

This is amazing.

Copy link

ishroid commented Jun 15, 2017

amazing! Thank you very much :-) 👍 💯

Copy link

nancym1718 commented Jul 18, 2017

what is the offset value that u mentioned?

Copy link

vivekpanchal commented Jun 16, 2018

R.dimen.item_offset what value should I put in this?

Copy link

Mr648 commented Jul 1, 2018

Thanks, It's Awesome!

@vivekpanchal you can put any value, bases on your design. I used 4dp!

Copy link

DaxeshV commented Jul 7, 2018

i need only right side space.please help me

Copy link

raxhaxor commented Jul 21, 2018

@daxeshvekariya make this change in getItemOffsets method
outRect.set(0, 0, mItemOffset, 0);

Copy link

deshario commented Jul 21, 2018

what is the value of R.dimen.item_offset

Copy link

SanjaGrgurevic commented Jul 27, 2018

This worked like a charm! Thank you 👍

Copy link

EduMaxwell commented Aug 3, 2018

U rock!

Copy link

hellaandrew commented Sep 7, 2018

What a nice class you made. Good work!

Copy link

ranjansingh1991 commented Oct 1, 2018

hey just i want to leave space below end of recyclerview with grid view.

Copy link

ShahoodulHassan commented Nov 13, 2018

God bless u! Perfect in case of LinearLayout as well.
@ygritc Can u please explain the whole code a bit more? We might be able to customize it then according to our needs.
Thanks a million!

Copy link

AlexSuvorov2k commented Dec 24, 2018

So simple.. Thank you!

Copy link

Ravslee commented Jan 3, 2019

When the orientation changes, things are not getting changed? help@

Copy link

Syjgin commented Mar 13, 2019

Don't works: GridLayout still adds default spacing (30dp) between elements

Copy link

jihadkahil commented Mar 27, 2019

Thank You! Works Like a charm!

Copy link

sidthakkar12 commented Aug 13, 2019

I have spanCount = 2, it still takes right spacing of left-side item and left spacing of right-side item which increases space between the two !!!

Copy link

GerganaT commented Oct 10, 2019

Awesome-worked perfect!

Copy link

alok8bb commented Jul 21, 2021


Copy link

manju1375 commented Jan 29, 2022

Perfect with (itemoffest = 10dp) !! Thank you !!

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