3

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

Robin Davies
  • 7,547
  • 1
  • 35
  • 50
  • have you solved that in the meantime? – prom85 Jul 05 '16 at 19:10
  • My solution: do it the other way around, since I can't get the PreferenceFragmentCompat to behave. Set teal as the accent color in the app theme, and then set the accent color explicitly on individual controls in the outer frame. :-P – Robin Davies Jul 07 '16 at 17:49
  • My solution is to subclass the `Preference...` classes and theme them in there... I need a theme independent theming in the sense of that I don't define themes in the xml (only the base dark/light theme) and allow my users to select any color as primary/secondary/accent color... – prom85 Jul 07 '16 at 19:27

1 Answers1

1

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.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • Can't quite bring myself to give you an accepted answer checkmark since I've dealt with it in other ways. But it's a great answer, full of important relevant details. But I upvoted. – Robin Davies Jul 22 '18 at 21:25
  • That's OK, if you can remember exactly how you solved it, you can post it as your own answer to help others. – Eugen Pechanec Jul 22 '18 at 21:30
  • :-) I posted my not very happy solution as a comment about a couple of years ago (let the preferences take the app theme, and override the theme of everything else). If somebody else can confirm that android:colorAccent works, I would be happy to give you a well-deserved upvote. I'm just not in a positon to confirm it right now. – Robin Davies Jul 22 '18 at 21:37