First of all, this question asks a very similar question. However, my question has a subtle difference.
What I'd like to know is whether it is possible to programmatically change the colorPrimary
attribute of a theme to an arbitrary color?
So for example, we have:
<style name="AppTheme" parent="android:Theme.Material.Light">
<item name="android:colorPrimary">#ff0000</item>
<item name="android:colorAccent">#ff0000</item>
</style>
At runtime, the user decides he wants to use #ccffff
as a primary color. Ofcourse there's no way I can create themes for all possible colors.
I don't mind if I have to do hacky stuff, like relying on Android's private internals, as long as it works using the public SDK.
My goal is to eventually have the ActionBar
and all widgets like a CheckBox
to use this primary color.
I've created some solution to make any-color themes, maybe this can be useful for somebody. API 9+
1. first create "res/values-v9/" and put there this file: styles.xml and regular "res/values" folder will be used with your styles.
2. put this code in your res/values/styles.xml:
3. in to AndroidManifest:
4. create a new class with name "ThemeColors.java"
5. and before calling setContentView(R.layout.activity_main), just add:
to change color replace Random with your RGB:
USE A TOOLBAR
You can set a custom toolbar item color dynamically by creating a custom toolbar class:
then refer to it in your layout file. Now you can set a custom color using
Sources:
I found the information to do this here: How to dynamicaly change Android Toolbar icons color
and then I edited it, improved upon it, and posted it here: GitHub:AndroidDynamicToolbarItemColor