How to change color of Android ListView separator line?
Asked Answered
A

9

408

I want to change color of ListView separator line.

Apposite answered 3/3, 2010 at 15:16 Comment(0)
E
771

You can set this value in a layout xml file using android:divider="#FF0000". If you are changing the colour/drawable, you have to set/reset the height of the divider too.

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <ListView 
    android:id="@+id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:divider="#FFCC00"
    android:dividerHeight="4px"/>

</LinearLayout>
Enwreathe answered 3/3, 2010 at 15:45 Comment(13)
You should also be able to specify a Drawable resource in android:divider as well. The existing divider is a gradient.Lacrimator
where is the existing gradient divider?Talie
If you do it in XML make sure to see the height as well using android:dividerHeight otherwise you will get no lineIcj
From my experience, read "should reset the height of the divider" to "must set the height of the divider"Impolite
I wouldn't recommend using px unit to define sizes in Android, use dp insteadFreidafreight
I used to handle the separator line color by setting the background of the ListView. My method doesn't work when the list size is smaller than the screen size. Fills the screen with ugly colors. But this is great.Zitvaa
It seems like there might be a good reason to use px in this specific case. See: https://mcmap.net/q/87524/-android-listview-dividerPappose
Yes, because there it was 1px.Motch
The divider is an exception. It should be 1px and the color should have enough contrast. Because 1dp may vary between 1-2 px or even more on very high density displays. The appearance is a bit ugly IMO.Essentialism
As I amm using device with density over 500dpi for testing I would recommend 1dp as 1px is almost invisible.Meredi
If 1dp line looks thick then use 0.5dp but not 1pxAgosto
Can this be apply in ExpandableListView as well?Ponzo
I do recommend using px when you use 1px, because some devices won't be able to process 1dp as it will be less than 1px and so they won't draw anything at all.Whether
O
165

Or you can code it:

int[] colors = {0, 0xFFFF0000, 0}; // red for the example
myList.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
myList.setDividerHeight(1);
Obreption answered 10/10, 2010 at 13:22 Comment(2)
Perfect, my items was on a reddish gradient background and your effect made them magnificent !!Foresail
if you extend ListActivity, replace mylist with getListView()Christenson
W
90

For a single color line use:

list.setDivider(new ColorDrawable(0x99F10529));   //0xAARRGGBB
list.setDividerHeight(1);

It's important that DividerHeight is set after the divider, else you won't get anything.

Whether answered 28/6, 2012 at 23:45 Comment(2)
Thank you, I called setDividerHeight() before setDivider() and no divider was shown.Silencer
Very helpful comment about the order of operations. I just spent 2 hours trying to make it work. Nice design, Android.Davison
H
13

You can also get the colors from your resources by using:

dateView.setDivider(new ColorDrawable(_context.getResources().getColor(R.color.textlight)));
dateView.setDividerHeight(1);
Hulse answered 8/2, 2013 at 0:10 Comment(0)
V
11

XML version for @Asher Aslan cool effect.

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

    <gradient
        android:angle="180"
        android:startColor="#00000000"
        android:centerColor="#FFFF0000"
        android:endColor="#00000000"/>

</shape>

Name for that shape as: list_driver.xml under drawable folder

<ListView
        android:id="@+id/category_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:divider="@drawable/list_driver"
        android:dividerHeight="5sp" />
Verlie answered 17/2, 2014 at 16:49 Comment(0)
B
6

There are two ways to doing the same:

  1. You may set the value of android:divider="#FFCCFF" in layout xml file. With this you also have to specify height of divider like this android:dividerHeight="5px".

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
      <ListView 
      android:id="@+id/lvMyList"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:divider="#FFCCFF"
      android:dividerHeight="5px"/>
    
    </LinearLayout>
    
  2. You may also do this by programmatically...

    ListView listView = getListView();
    ColorDrawable myColor = new ColorDrawable(
        this.getResources().getColor(R.color.myColor)
    );
    listView.setDivider(myColor);
    listView.setDividerHeight();
    
Birgit answered 2/2, 2015 at 12:20 Comment(0)
A
2

Use below code in your xml file

<ListView 
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#000000" 
    android:dividerHeight="1dp">
</ListView> 
Anse answered 15/5, 2015 at 9:57 Comment(1)
It's best to explain a bit about why your solution works. Code only answers might fix the issue but that doesn't necessarily answer the asker's question.Orthotropic
R
1

using programetically

           // Set ListView divider color
            lv.setDivider(new ColorDrawable(Color.parseColor("#FF4A4D93")));

            // set ListView divider height
            lv.setDividerHeight(2);

using xml

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <ListView 
    android:id="@+id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:divider="#44CC00"
    android:dividerHeight="4px"/>

</LinearLayout>
Rosiarosicrucian answered 2/7, 2019 at 7:43 Comment(0)
C
0

Use android:divider="#FF0000" and android:dividerHeight="2px" for ListView.

<ListView 
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#0099FF"
android:dividerHeight="2px"/>
Christly answered 17/2, 2014 at 10:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.