How to set width of ListView divider?

jul picture jul · Nov 4, 2011 · Viewed 19.6k times · Source

How can I set the width of my custom ListView divider, in order to have it smaller than my row width?

Answer

Suragch picture Suragch · Aug 15, 2014

A RecyclerView is generally preferred over using a ListView now. See this Q&A for how to set the width of the divider in a RecyclerView.

Use <inset>

drawable/list_divider.xml

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="10dp"
    android:insetRight="10dp" >

    <shape android:shape="rectangle" >
        <solid android:color="@color/list_divider_color" />
    </shape>

</inset>

And in your layout:

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:cacheColorHint="#00000000"
    android:divider="@drawable/list_divider"
    android:dividerHeight="1dp" >
</ListView>

enter image description here

Sources: