Listview divider margin
Asked Answered
S

5

33

I'm trying to set a margin to a listview divider.
The divider is a dashed line:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >
    <stroke
        android:dashGap="1dp"
        android:dashWidth="1.5dp"
        android:width="1dp"
        android:color="#FF404040" />

    <size android:height="3dp" />

</shape>

and a listview where i set the divider

<ListView
    android:id="@+id/lv_news_feed_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:divider="@drawable/ch_dashed_line_divider"
    />

but I want a divider margin left and right. I also tried to set a padding to the shape, but the listview is ignoring the padding.

    <padding
        android:bottom="15dp"
        android:left="15dp"
        android:right="15dp"
        android:top="15dp" />

Is there any possibility to set a margin to the listview divider - except in the getView() of the Adapter?

Smoke answered 19/6, 2012 at 7:28 Comment(0)
R
88

Inset is the way to go

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

    <shape
        android:shape="line" >
        <stroke
            android:dashGap="1dp"
            android:dashWidth="1.5dp"
            android:width="1dp"
            android:color="#FF404040" />

            <size android:height="3dp" />

    </shape>

</inset>
Replay answered 6/12, 2012 at 18:14 Comment(2)
Easily the best answer here. This is what the inset xml structure is for.Ironclad
If we just want a line, use <shape android:shape="rectangle" > <solid android:color="@color/list_divider" /> </shape> and set the property "android:dividerHeight" in the ListViewCountryandwestern
R
16

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...

Reynold answered 28/6, 2013 at 7:52 Comment(1)
You can place the <size android:height="0.5dp" /> element directly into the shape to avoid repeating the height for every listview.Assumpsit
G
5

You can use the following idea:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="@color/table_background"/>
    </shape>
</item>
<item android:left="2dp" android:right="2dp">
    ... your shape here ...
</item> </layer-list>

It works for me. Hope it will help.

Gunderson answered 13/10, 2012 at 15:7 Comment(0)
T
3

U can use Gradient to get Right and Left margin instead of stroke.. Try this sample it starts and ends with black, in centre u'll get White.. It appears like u've given margin

 <gradient android:startColor="#000000" android:centerColor="#ffffff"
    android:endColor="#000000" />
Tupi answered 19/6, 2012 at 7:46 Comment(1)
This is a good solution, but i need the stroke for the dashed line in the divider, so i can't use a gradient.Smoke
R
1

I don't believe it is possible :/ Although if you add your own drawable at the bottom of each list item and remove the ListViews divider you can customize it however you want :)

Create the folder res/drawable and create a new xml:

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

    <solid android:color="#666666"></solid>

</shape>

..Then use that drawable in you list item.

Or a really quick and dirty way would ofc to create a thin LinearLayout (or some other layout) with colored background at the bottom of the list item to fake a customizable ListView divider..

Not a proper sollution, just some fixit ideas ;)

Rigidify answered 19/6, 2012 at 7:56 Comment(2)
This was my first idea to. Attach it yourself in the Adapter but i don't know if this is peformant.Smoke
Try it out and see ;) Or make your own custom ListView, though i doubt it would be worth the trouble.Rigidify

© 2022 - 2024 — McMap. All rights reserved.