Skip to content

Instantly share code, notes, and snippets.

@yqritc
Last active January 25, 2025 17:43
Show Gist options
  • Select an option

  • Save yqritc/ccca77dc42f2364777e1 to your computer and use it in GitHub Desktop.

Select an option

Save yqritc/ccca77dc42f2364777e1 to your computer and use it in GitHub Desktop.
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
Copy link
Copy Markdown

Thanks, works like a charm !

@vishalkale
Copy link
Copy Markdown

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
Copy link
Copy Markdown

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
Copy link
Copy Markdown

tiwiz commented Aug 17, 2016

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

@malenalbc
Copy link
Copy Markdown

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

@hcbpassos
Copy link
Copy Markdown

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

Copy link
Copy Markdown

ghost commented Dec 30, 2016

Thank you, it is work for my apps.

@diceroll123
Copy link
Copy Markdown

This is amazing.

@ishroid
Copy link
Copy Markdown

ishroid commented Jun 15, 2017

amazing! Thank you very much :-) 👍 💯

@nancym1718
Copy link
Copy Markdown

what is the offset value that u mentioned?

@vivekpanchal
Copy link
Copy Markdown

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

@rahmatwaisi
Copy link
Copy Markdown

Thanks, It's Awesome!

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

@DaxeshV
Copy link
Copy Markdown

DaxeshV commented Jul 7, 2018

i need only right side space.please help me

@raxhaxor
Copy link
Copy Markdown

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

@deshario
Copy link
Copy Markdown

what is the value of R.dimen.item_offset

@SanjaGrgurevic
Copy link
Copy Markdown

This worked like a charm! Thank you 👍

@EduMaxwell
Copy link
Copy Markdown

U rock!

@hellaandrew
Copy link
Copy Markdown

What a nice class you made. Good work!

@ranjansingh1991
Copy link
Copy Markdown

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

@ShahoodulHassan
Copy link
Copy Markdown

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!

@AlexSuvorov2k
Copy link
Copy Markdown

So simple.. Thank you!

@Ravslee
Copy link
Copy Markdown

Ravslee commented Jan 3, 2019

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

@Syjgin
Copy link
Copy Markdown

Syjgin commented Mar 13, 2019

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

@jihadkahil
Copy link
Copy Markdown

Thank You! Works Like a charm!

@sidthakkar12
Copy link
Copy Markdown

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
Copy link
Copy Markdown

Awesome-worked perfect!

@alok8bb
Copy link
Copy Markdown

alok8bb commented Jul 21, 2021

Thanks!

@manju1375
Copy link
Copy Markdown

manju1375 commented Jan 29, 2022

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

@xinpengfei520
Copy link
Copy Markdown

Worked-perfect! Thank you very much!

@ykrapiva
Copy link
Copy Markdown

this is not something that can be called "equally spaced items". You get your offset on the left and on the right only. Between the items it is doubled. Take a look at YouTube in landscape orientation on tablet, this is what is called equally spaced items.

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