Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView divider not showing in Android 5

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?

like image 725
sr09 Avatar asked Nov 12 '14 00:11

sr09


4 Answers

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!

like image 115
avianey Avatar answered Nov 04 '22 00:11

avianey


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.

like image 33
Android3000 Avatar answered Nov 03 '22 23:11

Android3000


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"/>
like image 7
idunnololz Avatar answered Nov 04 '22 01:11

idunnololz


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>
like image 1
Prasad Avatar answered Nov 03 '22 23:11

Prasad