Home » Android » Dynamically change values of colors.xml to change look and feel of android app

Dynamically change values of colors.xml to change look and feel of android app

Posted by: admin June 15, 2020 Leave a comment

Questions:

Problem :
I am trying to change the look and feel of my Android app, on the fly. Something like, the app starts up and gets a set of values from the server. These values are the elements that typically go inside colors.xml. What I am looking for, is a way to dynamically change the elements inside the colors.xml and update it with these new values received from the server. My understanding is that normally, this cannot be done directly. But has anyone found a workaround?

What I want to avoid if possible :
I would like to avoid setting color values inside each activity’s onCreate() method for each element in that view. If at all possible, I would like to avoid this.

Any thoughts?

How to&Answers:

You can achive this change by newly introduce firebase remote config which provide remote config to change theme color or any other values necessary for app like promotion,updates etc

You can refer this Example

Answer:

Unfortunately all color values (and other resources) inside the resources directory are hardcoded as static final ints. This means there is no way to change the values at runtime. You can however use one of the previously suggested solutions or have a look at this excellent explanation: https://stackoverflow.com/a/33992017/3662251

For a nice workaround that overrides the activity’s getResources method and implements a custom Resources class which is in my opinion the most seamless solution: https://stackoverflow.com/a/34178187/3662251

Answer:

I have did that in my app getting Hex color code like #06FF67 from my server and stored in sharedpreferences – https://stackoverflow.com/a/23024962/4741746

And when need to set new value that coming from server just override same shared preferences value with new data and set to app

Or you can use Random Color genrater also –https://stackoverflow.com/a/5280929/4741746