Suragch Suragch - 1 year ago 180
Android Question

How to indent the divider in a linear layout RecyclerView (ie, add padding, margin, or an inset only to the ItemDecoration)

Following this answer I was able to get a divider between the items of a vertical RecyclerView. However, I also wanted to slightly indent the divider lines.

I was able to do it by hard coding in an INDENT value in the


int INDENT = 20;

public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

int left = parent.getPaddingLeft() + INDENT;
int right = parent.getWidth() - parent.getPaddingRight() - INDENT;

// ...

divider.setBounds(left, top, right, bottom);

// ...

However, then I would have had to also mess with density independant pixels.

I finally found a solution similar to how it was done with ListView so I am sharing that as an answer below.

Answer Source

Use inset


<inset xmlns:android=""
       android:insetRight="40dp" >

        <size android:height="1dp"/>
        <solid android:color="@color/recyclerview_divider" />


Using the constructor that takes a resource id as shown in this answer, we can supply the id of our custom divider xml file.

        new DividerItemDecoration(getActivity(), R.drawable.my_divider));

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download