Skip to content

Instantly share code, notes, and snippets.

Last active March 29, 2024 10:25
Show Gist options
  • Save yqritc/ccca77dc42f2364777e1 to your computer and use it in GitHub Desktop.
Save yqritc/ccca77dc42f2364777e1 to your computer and use it in GitHub Desktop.
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

Thanks, works like a charm !

Copy link

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

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

Copy link

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

This is amazing.

Copy link

ishroid commented Jun 15, 2017

amazing! Thank you very much :-) 👍 💯

Copy link

what is the offset value that u mentioned?

Copy link

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

Copy link

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

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

Copy link

what is the value of R.dimen.item_offset

Copy link

This worked like a charm! Thank you 👍

Copy link

U rock!

Copy link

What a nice class you made. Good work!

Copy link

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

Copy link

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

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

Thank You! Works Like a charm!

Copy link

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

Awesome-worked perfect!

Copy link

alok8bb commented Jul 21, 2021


Copy link

manju1375 commented Jan 29, 2022

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

Copy link

Worked-perfect! Thank you very much!

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