Create a gist now

Instantly share code, notes, and snippets.

Embed
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.

Show comment
Hide comment
@crazyhitty

crazyhitty Feb 15, 2016

Thanks, works like a charm !

Thanks, works like a charm !

@vishalkale

This comment has been minimized.

Show comment
Hide comment
@vishalkale

vishalkale 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.

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.

Show comment
Hide comment
@krishnameena

krishnameena 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 ?

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.

Show comment
Hide comment
@tiwiz

tiwiz Aug 17, 2016

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

tiwiz commented Aug 17, 2016

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

@malenalbc

This comment has been minimized.

Show comment
Hide comment
@malenalbc

malenalbc Sep 28, 2016

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

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

@HugoCastelani

This comment has been minimized.

Show comment
Hide comment
@HugoCastelani

HugoCastelani Dec 6, 2016

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

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

@him407

This comment has been minimized.

Show comment
Hide comment
@him407

him407 Dec 30, 2016

Thank you, it is work for my apps.

him407 commented Dec 30, 2016

Thank you, it is work for my apps.

@diceroll123

This comment has been minimized.

Show comment
Hide comment
@diceroll123

diceroll123 Jan 8, 2017

This is amazing.

This is amazing.

@ishroid

This comment has been minimized.

Show comment
Hide comment
@ishroid

ishroid Jun 15, 2017

amazing! Thank you very much :-) 👍 💯

ishroid commented Jun 15, 2017

amazing! Thank you very much :-) 👍 💯

@nancym1718

This comment has been minimized.

Show comment
Hide comment
@nancym1718

nancym1718 Jul 18, 2017

what is the offset value that u mentioned?

what is the offset value that u mentioned?

@vivekpanchal

This comment has been minimized.

Show comment
Hide comment
@vivekpanchal

vivekpanchal Jun 16, 2018

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

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

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