Android ListView Divider
Asked Answered
E

12

98

I have this code:

<ListView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/cashItemsList"
     android:cacheColorHint="#00000000"
     android:divider="@drawable/list_divider"></ListView>

where @drawable/list_divider is:

<shape
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="line">
 <stroke
   android:width="1dp"
   android:color="#8F8F8F"
   android:dashWidth="1dp"
   android:dashGap="1dp" />
</shape>

but I can't see any divider.

Eadwine answered 20/10, 2010 at 14:56 Comment(6)
I don't know why but the code is missing. here it is again:Eadwine
<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cashItemsList" android:cacheColorHint="#00000000" android:divider="@drawable/list_divider"></ListView>Eadwine
and the list divider is: <shape xmlns:android="schemas.android.com/apk/res/android" android:shape="line"> <stroke android:width="1dp" android:color="#8F8F8F" android:dashWidth="1dp" android:dashGap="1dp" /> </shape>Eadwine
use the code block (the 101010 icon) for inserting codes, especially XML/HTML/SGML code. I've fixed your post for now.Colorful
Maybe the reason of your problem is in your ListAdapter. Just try to return true of adapter's areAllItemsEnabled(). Or watch #5588326Unamerican
Color should be: android:color="#FF8F8F8F" <-- extra FF for alphaTopknot
I
179

Folks, here's why you should use 1px instead of 1dp or 1dip: if you specify 1dp or 1dip, Android will scale that down. On a 120dpi device, that becomes something like 0.75px translated, which rounds to 0. On some devices, that translates to 2-3 pixels, and it usually looks ugly or sloppy

For dividers, 1px is the correct height if you want a 1 pixel divider and is one of the exceptions for the "everything should be dip" rule. It'll be 1 pixel on all screens. Plus, 1px usually looks better on hdpi and above screens

"It's not 2012 anymore" edit: you may have to switch over to dp/dip starting at a certain screen density

Idem answered 21/8, 2012 at 19:21 Comment(5)
Wow. Saved my life. Should be part of Android's official guide to use "dip"Sirrah
I agree. They should at least mention that rule about px as an example of why they have it.Idem
On ldpi, 1dp = 0.75 pixels, therefore rounds down to 0. The divider then does not get drawn, which can pose problems for others. This also refers to the other statements in this thread complaining about using px altogether. This may or may not be the solution to his problem and it's up to him to declare whether or not it isIdem
1px is going to be incredibly small on an xxhdpi device, and at some point (as devices continue to get higher density) will be too small to see at all. Dip avoids this, and another solution for ldpi devices is to use 1px in the values-ldpi folder, and 1dip for higher densities.Abuse
When I posted that response, xxhdpi I think was just coming out. However, your post makes sense, especially with xxxhdpi and possibly xxxxhdpi on the horizonIdem
P
56

This is a workaround, but works for me:

Created res/drawable/divider.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#ffcdcdcd" android:endColor="#ffcdcdcd" android:angle="270.0" />
</shape>

And in styles.xml for listview item, I added the following lines:

    <item name="android:divider">@drawable/divider</item>
    <item name="android:dividerHeight">1px</item>

Crucial part was to include this 1px setting. Of course, drawable uses gradient (with 1px) and that's not the optimal solution. I tried using stroke but didn't get it to work. (You don't seem to use styles, so just add android:dividerHeight="1px" attribute for the ListView.

Pitapat answered 7/11, 2010 at 14:21 Comment(7)
Or use 1dp for best practice.Elledge
Why are you using an angle of 270? List dividers are horizontal lines. 270 is a vertical gradient.Outbalance
Isn't this a bug in Android? Shouldn't a line shape as divider just work?Stalwart
1px assets are the exception to the ruleIdem
@christopherPerry: You are wrong. 270 means that the gradient changes from one color being ABOVE another. startColor sits at the top of the line, endColor sits at the bottom. 0 means that the colors change from LEFT to RIGHT.Geaghan
@TristanWarner-Smith this is incorrect. you need to use 1px in this case. see the accepted answer.Schaumberger
Work for me by using <item name="android:listDivider:>@drawable/divider</item>Grosvenor
P
27

Add android:dividerHeight="1px" and it will work:

<ListView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/cashItemsList"
     android:cacheColorHint="#00000000"
     android:divider="@drawable/list_divider"
     android:dividerHeight="1px">
 </ListView>
Pender answered 11/12, 2011 at 11:29 Comment(0)
R
15

The problem you're having stems from the fact that you're missing android:dividerHeight, which you need, and the fact that you're trying to specify a line weight in your drawable, which you can't do with dividers for some odd reason. Essentially to get your example to work you could do something like the following:

Create your drawable as either a rectangle or a line, either works you just can't try to set any dimensions on it, so either:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
     <stroke android:color="#8F8F8F" android:dashWidth="1dp" android:dashGap="1dp" />
</shape>

OR:

<shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle">
     <solid android:color="#8F8F8F"/>
</shape>

Then create a custom style (just a preference but I like to be able to reuse stuff)

<style name="dividedListStyle" parent="@android:style/Widget.ListView">
    <item name="android:cacheColorHint">@android:color/transparent</item>
    <item name="android:divider">@drawable/list_divider</item>
    <item name="android:dividerHeight">1dp</item>
</style>

Finally declare your list view using the custom style:

<ListView
     style="@style/dividedListStyle"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/cashItemsList">
</ListView>

I'm assuming you know how to use these snippets, if not let me know. Basically the answer to your question is that you can't set the divider thickness in the drawable, you have to leave the width undefined there and use android:dividerHeight to set it instead.

Religiosity answered 8/7, 2012 at 19:17 Comment(0)
Y
8

From the doc:

public void setDivider(Drawable divider) on ListView

/**
 * Sets the drawable that will be drawn between each item in the list. If the drawable does
 * not have an intrinsic height, you should also call {@link #setDividerHeight(int)}
 *
 * @param divider The drawable to use.
 */

Looks like setDividerHeight() must be called in order for the divider to show up if it has no intrinsic height

Yusuk answered 18/11, 2011 at 19:4 Comment(0)
P
5

Your @drawable/list_divide should look like this:

<shape
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="line">
 <stroke
   android:height="1dp"
   android:color="#8F8F8F"
   android:dashWidth="1dp"
   android:dashGap="1dp" />
</shape>

In your version you provide an android:width="1dp", simply change it to an android:height="1dp" and it should work!

Plutonic answered 16/4, 2012 at 8:25 Comment(1)
android:height isn't a valid attribute for stroke, and width just means how wide the stroke is, not how long it is. The only reason your "solution" works is that Android doesn't recognize that height value.Religiosity
C
4

From the doc:

file location:

res/drawable/filename.xml

The filename is used as the resource ID.

basically, you'll need to put a file named list_divider.xml in res/drawable/ so you can access it as R.drawable.list_divider; if you can access it that way, then you can use android:divider="@drawable/list_divider" in the XML for ListView.

Colorful answered 20/10, 2010 at 15:14 Comment(1)
I work with eclipse, so if I hadn't done that, the code wouldn't compile. so, with the file in place, still seems that the list view ignores my custom divider.Eadwine
R
2

Some people might be experiencing a solid line. I got around this by adding android:layerType="software" to the view referencing the drawable.

Ricketts answered 26/2, 2014 at 4:4 Comment(0)
M
1

The android docs warn about things dissappearing due to round-off error... Perhaps try dp instead of px, and perhaps also try > 1 first to see if it is the round-off problem.

see http://developer.android.com/guide/practices/screens_support.html#testing

for the section "Images with 1 pixel height/width"

Muscat answered 4/3, 2011 at 3:11 Comment(1)
Yep. IF you are using 2 or more dp/dip, then you're okay. However, if you just want that 1-pixel divider, px is the way to go. You also get more screen real-estate with 1px, plus 1px usually looks betterIdem
R
1

I had the same issue. However making the view 1px didn't seem to work on my original Nexus 7. I noticed that the screen density was 213 which is less than the 240 used in xhdpi. So it was thinking the device was an mdpi density.

My solution was to make it so the dimens folder had a dividerHeight parameter. I set it to 2dp in the values-mdpi folder but 1dp in the values-hdpi etc folders.

Rhombohedral answered 13/5, 2014 at 20:56 Comment(0)
R
1

you forgot an "r" at the end of divider in your divider xml layout

you call the layout @drawable/list_divider but your divider xml is named "list_divide"

Reproductive answered 5/3, 2015 at 5:42 Comment(0)
S
-1

Set android:dividerHeight="1dp"

<ListView
    android:id="@+id/myphnview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@drawable/dividerheight"
    android:background="#E9EAEC"
    android:clickable="true"
    android:divider="@color/white"
    android:dividerHeight="1dp"
    android:headerDividersEnabled="true" >
</ListView>
Scruff answered 20/10, 2016 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.