Java – SwitchPreference default color

SwitchPreference default color… here is a solution to the problem.

SwitchPreference default color

I’m working on an application I’ve set the major/dark/accent colors to the colors I want, and they appear in the right place (as expected). However, I have a preference activity that I am using, and I expect the colors of the preferenceswitch I am using to be rendered in accent colors. Instead, they are rendered in Material blue-green. I wonder if this default behavior of Lollipop is blue like in Kitkit? I didn’t even reference #009688 colors anywhere in my code or colors.xml/styles.xml.

colors.xml

<resources>
    <color name="primary">#00BCD4</color>
    <color name="primary_dark">#0097A7</color>
    <color name="accent">#FFD740</color>
</resources>

styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorPrimary">@color/primary</item>
        <item name="android:colorPrimaryDark">@color/primary_dark</item>
        <item name="android:colorAccent">@color/accent</item>
    </style>
</resources>

Any ideas? I will provide more information. I’ve seen something here about creating custom content, but is it really necessary?

preferenceActivity.java

public class PreferenceActivity extends Activity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

PrefFrag prefFragment = new PrefFrag();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(android. R.id.content, prefFragment);
        fragmentTransaction.commit();
    }
}

Solution

When you use

AppCompat, you should use a non-prefixed version of each property – this ensures that they are available at all API levels (unlike android: for example, only for API21+):

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>

Related Problems and Solutions