Search code examples
androidsharedpreferencesdivider

Change color to dividers between settings groups


My preferences.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.v7.preference.PreferenceCategory
    android:key="themeCategoryKey"
    android:title="Theme">

    <SwitchPreferenceCompat
        android:key="themeKey"
        android:title="Turn on dark theme" />

</android.support.v7.preference.PreferenceCategory>

<android.support.v7.preference.PreferenceCategory
    android:key="locationKeyCategory"
    android:title="Location">

    <EditTextPreference
        android:name="EditText Preference2"
        android:key="locationKey"
        android:summary="Enter default location for home screen"
        android:title="Location" />

</android.support.v7.preference.PreferenceCategory>

</PreferenceScreen>

What i am trying to achieve is to change the color of dividers between preference groups. I need that because when i change theme color from dark to some light theme with white background color, dividers are not noticeable because they are white, although dividers are noticeable at dark theme because background color is black. In the following section i have provided style.xml.

<resources>

<!-- Dark Theme -->
<style name="BlackTheme" parent="Theme.AppCompat">
    <item name="colorPrimaryDark">@color/backgroundDarkTheme</item>
    <item name="colorPrimary">#171717</item>
    <item name="colorAccent">@color/colorAccentDarkTheme</item>
    <item name="android:windowBackground">@color/colorPrimaryDarkerDark</item>
    <item name="android:itemBackground">@color/colorPrimaryDarkerDark</item>
    <item name="android:textColor">@color/textColorDarkTheme</item>
    <item name="android:divider">@null</item>
</style>

<!-- Orange Theme Default One -->
<style name="OrangeTheme" parent="Theme.AppCompat">  <!-- this is light theme -->
    <item name="colorPrimaryDark">#e95304</item>
    <item name="colorPrimary">@color/colorAccentOrangeTheme</item>
    <item name="colorAccent">@color/colorAccentOrangeTheme</item>
    <item name="android:windowBackground">@color/backgroundOrangeTheme</item>
    <item name="android:itemBackground">@color/toolbarItemBackgroundLight</item>
    <item name="android:itemTextAppearance">@style/menu_item_color</item>
    <item name="android:textColor">@color/textColorOrangeTheme</item>
    <item name="android:textColorSecondary">@color/textColorOrangeTheme</item>
    <item name="android:divider">@color/textColorOrangeTheme</item>  <!-- this is black color btw -->
</style>

<!-- Menu Item Text Color Orange Theme -->
<style name="menu_item_color">
    <item name="android:textColor">@color/backgroundOrangeTheme</item>
</style>

</resources>

Here i tried with this but with no luck.

  <item name="android:divider">@color/textColorOrangeTheme</item>

Hope you guys understand what i am planning to do and how can i accomplish my goal. For those of you who dont know exactly which dividers i wanted to style, picture is down below

enter image description here

Here is the picture without divider

enter image description here


Solution

  • Could you please try this, in place of 'OrangeTheme' in your styles.xml. This works for me, just check if this is what you need?

    <style name="OrangeTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <item name="colorPrimaryDark">#e95304</item>
            <item name="colorPrimary">@color/colorAccentOrangeTheme</item>
            <item name="colorAccent">@color/colorAccentOrangeTheme</item>
            <item name="android:itemTextAppearance">@style/menu_item_color</item>
            <item name="android:textColor">@color/textColorOrangeTheme</item>
            <item name="android:textColorSecondary">@color/textColorOrangeTheme</item>
            <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
        </style>