How to set PreferenceFragmentCompat accentColor?

981 views Asked by At

I'd like to override the accentColor used by v14 PreferenceFragmentCompat.

I'm using a pink accent color for the outer frame of my Android app. This creates problems in many situations as it causes standard controls to use an accent color that's close enough to red that the effect is disturbing. Be that as it may, I like the effect of having a pink FAB and button controls on the frame.

For child activities, I use a them with the standard teal accent color. However, I have a PreferenceFragment compat in a drawer on the main activity, and I cannot figure out how to override the main activity's accent color.

Things I have tried (none of which work):

Setting a theme on the frame of the fragment that receives the PreferenceFragmentCompat (doesn't work):

<FrameLayout android:id="@+id/preferenceFragmentFrame" 
    android:layout_width="match_parent" android:layout_height="0dp"
    android:layout_weight="1"
    android:theme="@style/AppTheme.TealAccentColor"
    />

where the AppTheme.TealAccentColor style provides an explicit teal acccentColor.

Setting the accentColor in a preference theme (doesn't work):

  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        ...
     <item name="preferenceTheme">@style/MyPreferenceThemeOverlay</item>
    </style>
    <style name=MyPreferenceThemeOverlay parent="PreferenceThemeOverlay.v14.Material>
         <item name="colorAccent">@color/colorAccentTeal</item>
    </style>

Adding an accent color to preference-v14's PreferenceThemeOverlay (doens't work):

  <!-- use the library's theme-->
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        ...
     <item name="preferenceTheme">PreferenceThemeOverlay.v14.Material</item>
   </style>
   <!-- but add an accentColor item to the library's theme -->
   <style name="PreferenceThemeOverlay.v14">
        <item name="colorAccent">@color/colorAccentTeal</item>
   </style>

No matter what I do, PreferenceFragmentCompat seems to take the pink accent color from the Activity's theme instead.

I'm sure it has something to do with a disconnect between the Activity's theme and a Fragment's theme. But there's no xml element for the fragment, since PreferenceFragmentCompat provides its own layout.

Maybe there's a way to do it programmatically with an override in the class that extends PreferenceFragmentCompat, but if there is, I can't imagine what it would be. Most of the attack points I can think of either have access to the internally-created layout, or have access to the layout after it has been created, which is too late.

A picture might help:

enter image description here

1

There are 1 answers

3
Eugen Pechanec On

Have you tried overriding android:colorAccent instead of colorAccent?

Use the method you described as "Setting the accentColor in a preference theme", just change the attribute name.

The preference support library doesn't take into consideration appcompat at all, so

  1. forget about appcompat attributes
  2. color* attributes will only work on API 21, I think

If you want consistent behavior you can use my library Android Support Preference which aims to connect preference and appcompat support libraries.

Then you original style with colorAccent (unprefixed) will work as expected.