krystian71115 krystian71115 - 16 days ago 5x
Android Question

Can i change accent color (AppCompat) programmatically?

I have:

<style name="AppTheme" parent="Theme.AppCompat">
<item name="colorAccent">@color/myColor</item>

But I want to allow the user to change the accent color. Can I do it with AppCompat?


No you can't, because the accent color is defined in the theme and themes are read-only in Android.

The only thing you can do is switch themes or set the color of each component manually.

Note: you can apply a theme to a portion of UI instead of the whole Activity in order to change the accent color (or other things) locally. To do so, you can use the android:theme attribute in your XML layout with the AppCompat library, or you can inflate a layout by providing a ContextThemeWrapper as context to your LayoutInflater.