How do I change the color of the Android ListView divider?

I want to change the color of the ListView divider. Any help will be appreciated.

#1 building

For monochrome lines use:

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

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

#2 building

You can also get colors from resources using the following methods:

dateView.setDivider(new ColorDrawable(_context.getResources().getColor(R.color.textlight)));
dateView.setDividerHeight(1);

#3 building

For ListView, use android:dividerHeight="2px" and "ff0000".

<ListView 
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#0099FF"
android:dividerHeight="2px"/>

#4 building

Apply to @Asher Aslan's XML version is cool.

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

The name of the shape is: list driver.xml under the 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" />

#5 building

You can set this value in the layout xml file using Android: provider = (ff0000). If you want to change the color / paintable, you must also set / reset the height of the separator.

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

Tags: Android xml encoding

Posted on Sun, 09 Feb 2020 13:23:52 -0500 by Azu