How to change the color of the PreferenceCategory divider in PreferenceScreen

vitil picture vitil · Jul 15, 2015 · Viewed 11.2k times · Source

I have an android.support.v4.preference.PreferenceFragment which uses the following PreferenceScreen:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
    android:title="Cat1"
    android:key="pref_cat1">
    <ListPreference
        android:key="pref_list1"
        android:title="@string/pref_list1"
        android:dialogTitle="@string/pref_list1"
        android:entries="@array/pref_list1_entries"
        android:entryValues="@array/pref_list1_entries"
        android:defaultValue="@string/pref_list1_default"/>
    <EditTextPreference
        android:key="pref_text2"
        android:title="@string/pref_text2"
        />
</PreferenceCategory>
<PreferenceCategory
    android:title="Cat2"
    android:key="pref_cat2">
    <EditTextPreference
        android:key="pref_text3"
        android:title="@string/pref_text3"
        />
</PreferenceCategory>

When displaying the PreferenceFragment, some dividers are shown between the preferences, but also under the name of each PreferenceCategory. Though I can easily modify the color of the dividers between the preferences by accessing the PreferenceFragment's ListView, this has no effect on the PreferenceCategory dividers.

How to change also the color of such dividers?

Answer

yshahak picture yshahak · Sep 17, 2015

I had the same problem in my Prefernces file. I solved it by the following steps:

1: Define file name preferences_category.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+android:id/title"
        style="?android:attr/listSeparatorTextViewStyle"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical" />

    <View style="@style/Divider" />

</LinearLayout>

2: Define style in the style xml file:

<style name="Divider">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">1dp</item>
    <item name="android:background">@android:color/white</item>
</style>

3: add the preferences_category.xml file as android:layout attribute in the preferences xml file: :

 <PreferenceCategory 
    android:title="My title"
    android:layout="@layout/preferences_category">