Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set width of ListView divider?

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

like image 227
jul Avatar asked Nov 04 '11 13:11

jul


3 Answers

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:

  • Listview divider margin
  • How to assign padding to Listview item divider line
like image 82
Suragch Avatar answered Oct 30 '22 04:10

Suragch


Make a 9 patch png that has transparent pixels on the left and right. For example, a 53x4 .9.png that has 25 transparent pixels either side (+ pixels to 9patch it) would stretch the 1 pixel out so there is 25 pixels either side of it.

like image 29
FunkTheMonk Avatar answered Oct 30 '22 04:10

FunkTheMonk


If you don't want to make 9 patch, then you can insert

<View android:layout_width="fill_parent" android:layout_height="1dp" android:layout_marginTop="4dp" android:background="#33B5E5" />

in your xml code of list_item. It creates a blue line and you can easily control the width of this line. To make this successful you would have to disable the divider of the listview . Which is given here

like image 4
eeishaan Avatar answered Oct 30 '22 04:10

eeishaan