androidandroid-preferencesandroid-stylesdividerpreferencefragment

How to change the color of the PreferenceCategory divider in PreferenceScreen


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?


Solution

  • You have two options:

    1. Define the listSeparatorTextViewStyle in your app's theme

    Note that anything else which relies on this theme attribute will also change to use the style you define. If that's ok with you, it will look something like this:

    <style name="AppTheme" parent="android:...">
        ...
        <item name="android:listSeparatorTextViewStyle">@style/ListSeparatorText</item>
    </style>
    
    <style name="ListSeparatorText" parent="android:Widget.TextView"><!--parent is optional -->
        <item name="android:background">...</item>
        ...
    </style>
    
    1. Define a custom layout for your PreferenceCategories

    The default layout for a PreferenceCategory is just a TextView. You can make your layout as simple or as complicated as you like, but somewhere should be a TextView with android:id="@android:id/title" so that the title is bound automatically.

    Once you have a layout, use the android:layout attribute in your preference xml:

    <PreferenceCategory
        android:title="Cat2"
        android:key="pref_cat2"
        android:layout="@layout/my_pref_category">
        ...
    </PreferenceCategory>
    

    Alternatively, you can define the preferenceCategoryStyle in your app's theme, in which case you don't need to use android:layout at all in your preference xml.

    <style name="AppTheme" parent="android:...">
        ...
        <item name="android:preferenceCategoryStyle">@style/PreferenceCategoryStyle</item>
    </style>
    
    <style name="PreferenceCategoryStyle" parent="android:Preference.Category">
        <item name="android:layout">@layout/my_pref_category</item>
        ...
    </style>