9

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?

Rohitesh
  • 967
  • 2
  • 14
  • 29

3 Answers3

10

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

Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
4

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

Community
  • 1
  • 1
code_mc
  • 171
  • 10
  • Yes. That is what I am currently doing (overriding getResources()). This seems to be the best way forward, as of now. But I am not too keen about making changes to EVERY element (like TextView, EditText, Layout), in EVERY activity, programmatically. Wish there was a simpler way. – Rohitesh Dec 14 '16 at 03:49
  • Usually widgets use the Activity theme context for styling. So the getResources method for a TextView will actually get the resources from the activity. – code_mc Dec 14 '16 at 14:23
  • This works great for calls to `getResources().getColor()` across the app, but is there any way to hook into direct references to colors from inside layout resources? – tmm1 Feb 09 '21 at 23:47
0

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

Community
  • 1
  • 1
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55
  • did you use sharedpreferences before this ? if no read http://stackoverflow.com/a/23024962/4741746 : if yes Just get color from your server save that in sharedpreferences and when you want to change color get it from sharedpreferences an set to your app – Sushant Gosavi Dec 12 '16 at 13:44
  • Thank you Sushant. This is the solution I had in mind. But the problem (and the reason why I am not too keen to use this), is that it requires me to set the color programatically in every activity, for every visual element (button, textbox etc) – Rohitesh Dec 12 '16 at 15:30
  • than you need to create every widgets dynamically like edittext ,textview ,tabLayout etc and than set to your activity view ,i suggest you to go for Burhanuddin Rashid way – Sushant Gosavi Dec 13 '16 at 05:46