How to change the color of the PreferenceCategory divider in PreferenceScreen
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">
You have two options:
- 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>
- 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>