Skip to content

Instantly share code, notes, and snippets.

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

ItemOffsetDecoration

public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {

    private int mItemOffset;

    public ItemOffsetDecoration(int itemOffset) {
        mItemOffset = itemOffset;
    }

    public ItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId) {
        this(context.getResources().getDimensionPixelSize(itemOffsetId));
    }

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

Implementation

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);
mRecyclerView.addItemDecoration(itemDecoration);

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

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview_grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:padding="@dimen/item_offset"/>

DONE. You will get an equal spaces around items.

@crazyhitty

This comment has been minimized.

Copy link

crazyhitty commented Feb 15, 2016

Thanks, works like a charm !

@vishalkale

This comment has been minimized.

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.

@krishnameena

This comment has been minimized.

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 ?

@tiwiz

This comment has been minimized.

Copy link

tiwiz commented Aug 17, 2016

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

@malenalbc

This comment has been minimized.

Copy link

malenalbc commented Sep 28, 2016

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

@hugocastelani

This comment has been minimized.

Copy link

hugocastelani commented Dec 6, 2016

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

@him407

This comment has been minimized.

Copy link

him407 commented Dec 30, 2016

Thank you, it is work for my apps.

@diceroll123

This comment has been minimized.

Copy link

diceroll123 commented Jan 8, 2017

This is amazing.

@ishroid

This comment has been minimized.

Copy link

ishroid commented Jun 15, 2017

amazing! Thank you very much :-) 👍 💯

@nancym1718

This comment has been minimized.

Copy link

nancym1718 commented Jul 18, 2017

what is the offset value that u mentioned?

@vivekpanchal

This comment has been minimized.

Copy link

vivekpanchal commented Jun 16, 2018

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

@Mr648

This comment has been minimized.

Copy link

Mr648 commented Jul 1, 2018

Thanks, It's Awesome!

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

@daxeshvekariya

This comment has been minimized.

Copy link

daxeshvekariya commented Jul 7, 2018

i need only right side space.please help me

@raxhaxor

This comment has been minimized.

Copy link

raxhaxor commented Jul 21, 2018

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

@deshario

This comment has been minimized.

Copy link

deshario commented Jul 21, 2018

what is the value of R.dimen.item_offset

@SanjaGrgurevic

This comment has been minimized.

Copy link

SanjaGrgurevic commented Jul 27, 2018

This worked like a charm! Thank you 👍

@EduMaxwell

This comment has been minimized.

Copy link

EduMaxwell commented Aug 3, 2018

U rock!

@hellaandrew

This comment has been minimized.

Copy link

hellaandrew commented Sep 7, 2018

What a nice class you made. Good work!

@ranjansingh1991

This comment has been minimized.

Copy link

ranjansingh1991 commented Oct 1, 2018

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

@ShahoodulHassan

This comment has been minimized.

Copy link

ShahoodulHassan commented Nov 13, 2018

God bless u! Perfect solution....works 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!

@AlexRU18

This comment has been minimized.

Copy link

AlexRU18 commented Dec 24, 2018

So simple.. Thank you!

@Ravslee

This comment has been minimized.

Copy link

Ravslee commented Jan 3, 2019

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

@Syjgin

This comment has been minimized.

Copy link

Syjgin commented Mar 13, 2019

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

@jihadkahil

This comment has been minimized.

Copy link

jihadkahil commented Mar 27, 2019

Thank You! Works Like a charm!

@sidthakkar12

This comment has been minimized.

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 !!!

@GerganaT

This comment has been minimized.

Copy link

GerganaT commented Oct 10, 2019

Awesome-worked perfect!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.