I need to change the colorPrimary and colorAccent programmatically. The color values are fetched from a web service once we login to the application. These values will be any, because it is setting from a web application. So the same theme should be displayed in the mobile application too.
This is my theme
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
I searched everywhere and the answers are like, I need to create a number of different themes and switch between. But in my scenario, it is perfectly dynamic.
first create "res/values-v9/" and put there this file: styles.xml and regular "res/values" folder will be used with your styles.
put this code in your res/values/styles.xml:
To change color, just replace Random with your RGB, Hope this helps.
enter image description [here]
There is a complete example: ColorTest.zip
You can have a look at this GitHub project from Rumit Patel.