How to change PreferenceCategory separator color in PreferenceScreen

I have android.support.v4.preference.PreferenceFragment that 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 a PreferenceFragment, some separators are displayed between settings, but also under the name of each PreferenceCategory. While I can easily change the color of the separators between preferences by accessing the ListView's PreferenceFragment ListView, this does not affect the PreferenceCategory separators.

How can I also change the color of these separators?

+3


source to share


2 answers


You have two options:

  • Define listSeparatorTextViewStyle

    in the application theme

Note that everything that depends on this theme attribute will also change to use the style you define. If that's okay 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 preference categories


The default layout for PreferenceCategory

is just a TextView. You can make your layout as simple or as complex as you like, but there must be a TextView s somewhere android:id="@android:id/title"

for the title to be linked automatically.

Once you have the layout, use the attribute android:layout

in your xml preference:

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

      

Alternatively, you can define preferenceCategoryStyle

applications in your theme, in which case you do not need to use android:layout

xml at all in your preference.

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

      

+5


source


I had the same problem in my Prefernces

file. I solved it with the following steps:

1: Determine the filename 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 your xml style 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 file preferences_category.xml

as an attribute android:layout

in the preferences

xml file ::

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

      

+7


source







All Articles