ListView divider not showing in Android 5
Asked Answered
F

4

9

I have a simple listview for which I have defined a custom drawable for the divider. I have defined the divider height to be 1dp. The listview is within a fragment.

<shape
    android:shape="line" >
    <stroke
        android:color="@color/custom_color" />

    <gradient android:height="1dp" />

</shape>

It works great for all Android versions except L.

Anything I'm missing?

Fumarole answered 12/11, 2014 at 0:2 Comment(8)
I would try to "2dp", but otherwise I have no idea what could be missing.Obaza
I tried 4dp too. Didn't help.Fumarole
listview.setdivider(R.drawable.line);Oder
try to use px instead of dp, because for ldpi, 1dp = 0.75 pixels so it rounds down to 0 and the divider does not get drawn.Lauralauraceous
Good point. But I'm running this on a Nexus 5 (which uses xxhdpi assets)Fumarole
Use recyclerView instead of listView https://mcmap.net/q/27078/-how-to-add-dividers-and-spaces-between-items-in-recyclerviewBennybenoit
You have to set a stroke width.Ragamuffin
try to force software layer type with setLayerType() method. stroke lines doesnt work properly with hardware acceleration enabled on some devicesLatonya
R
9

You should use android:shape="rectangle" instead of android:shape="line" to make it work on every android version... (also change stroke to solid)

<shape
    android:shape="rectangle" >
    <solid android:color="@color/custom_color" />
    <gradient android:height="1dp" />
</shape>

Have fun!

Runyan answered 23/11, 2014 at 19:38 Comment(0)
V
9

By any chance, is your list item disabled via overriding isEnabled() to return false? There is a change (bug?) in Android L that causes list item dividers to be hidden if an item is disabled. I ran into this same issue, my list dividers working in everything but L, and this turned out to be the cause.

Here are some more posts where this has been discussed, as well as an issue opened up with Google:

Comments here: Disappearing divider in ListView when ArrayAdapter.isEnabled returns false

How to add dividers between disabled items in ListView? - Lollipop

https://code.google.com/p/android/issues/detail?id=83055

If this is the case, it sounds like you may need to manually draw the divider with a custom view and set the divider to null on the list. I will be trying the same.

Virginiavirginie answered 27/2, 2015 at 13:44 Comment(0)
C
7

Updated answer

After further testing it appears that the dividers will only show if the height of the divider is strictly less than the dividerHeight set for the ListView. For instance:

custom_divider.xml (Note that the divider height is specified by android:width)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >
    <stroke
        android:width="1dp"
        android:color="$ffff0000" />
</shape>

Layout xml

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

...Will work. But this will not:

custom_divider.xml (Note that the divider height is specified by android:width)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >
    <stroke
        android:width="1dp"
        android:color="$ffff0000" />
</shape>

Layout xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:divider="@drawable/custom_divider"
    android:dividerHeight="1dp"/>

My guess is that Google messed up with the optimization for drawing Listview dividers and will simply not draw them if there is not enough room.

Original post

Looks like you need to both set the dividerHeight on the ListView and the stroke width of the divider drawable for this to work on Android 5.

Example:

custom_divider.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >
    <stroke
        android:width="10dp"
        android:color="$ffff0000" />

    <gradient android:height="1dp" />
</shape>

Layout xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:divider="@drawable/custom_divider"
    android:dividerHeight="20dp"/>
Capping answered 18/11, 2014 at 6:34 Comment(0)
F
1

Height attribute is not a property under gradient tag. Use size attr like below.

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="@android:color/holo_blue_dark" />

    <size android:height="1px" />

</shape>
Featherstitch answered 28/5, 2015 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.