How to assign padding to Listview item divider line

Solution 1:

Use 'inset'.....

(list_divider.xml)

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="50dp"
    android:insetRight="50dp" >

 <shape>
    <solid android:color="@color/orange" />
    <corners android:radius="2.0dip" />
</shape>

</inset>

and in your list view add like this...

<ListView
    android:dividerHeight="2dp"
    android:divider="@drawable/list_divider"
    ...
/>

you can set the inset value as desired...

UPDATE

As pointed out by @Giulio Piancastelli , If the background of list container is different from background of list item then you may use 'layer-list'...

(list_divider.xml)

  <?xml version="1.0" encoding="utf-8"?>
  <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
   <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/list_background" />
        </shape>
    </item>


    <item
        android:left="10dp"
        android:right="10dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/divider_color"/>
        </shape>
    </item>

        </layer-list>

and in your list view add like this...

<ListView
    android:dividerHeight="2dp"
    android:divider="@drawable/list_divider"
    ...
/>

Solution 2:

You need padding for dividers? Create your custom drawable-shape as:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:left="10dp"
        android:right="10dp">
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/black" />
        </shape>
    </item>

</layer-list>

And set as divider for your ListView in xml:

<ListView
    android:dividerHeight="2dp"
    android:divider="@drawable/custom_divider"
    ...
/>

UPD

I just have ListView in xml:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="2dp"
android:divider="@drawable/line"
/>

Divider as line.xml in drawable:

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
        android:left="10dp"
        android:right="10dp">
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/black" />
        </shape>
    </item>
</layer-list>

Do not make any modifications on the ListView in code. You can try to use Clean if resources are wrong builded.

Solution 3:

As @Giulio Piancastelli mentioned under @ASP answer, the <inset> fails when the background colour of the list container is not the same as the rows inside the list. One solution is to use <layer-list> like below:

//This item is to overlay the container colour with the row background colour
<item
    android:left="0dp"
    android:right="0dp">
    <shape android:shape="rectangle" >
        <solid android:color="@color/row_background" />
    </shape>
</item>

//This item is the divider, put it at bottom so it will overlay the background colour
<item
    android:left="92dp"
    android:right="0dp">
    <shape android:shape="rectangle" >
        <solid android:color="@color/divider
    </shape>
</item>

Then you can set it as divider as most answers suggest:

<ListView
    android:dividerHeight="2dp"
    android:divider="@drawable/layer_list"
    ...
/>