Dynamically change colors.xml values ​​to change the appearance of an android application.

Problem: I'm trying to change the look of an Android application on the fly. Something like, the application starts and receives a set of values ​​from the server. These values ​​are elements that usually go inside colors.xml. I'm looking for this is a way to dynamically change elements inside colors.xml and update it with these new values ​​received from the server. I understand that this is normal, this cannot be done directly. But did anyone find a workaround?

What I want to avoid if possible: I would like to avoid setting color values ​​inside each action of the onCreate () method for each element in this view. If at all possible, I would like to avoid it.

Any thoughts?

+6
source share
3 answers

You can achieve this change by introducing a new firebase configuration that provides a remote config for changing the theme color or any other values ​​needed for the application, such as promotion, updates, etc.

You can reference this example.

+8
source

Unfortunately, all color values ​​(and other resources) inside the resource directory are hard-coded as static final ints. This means that it is not possible to change values ​​at run time. However, you can use one of the previously proposed solutions or take a look at this excellent explanation: fooobar.com/questions/103234 / ...

For a nice workaround that overrides the getResources method and implements the custom Resources class, which, in my opinion, is the easiest solution: fooobar.com/questions/103234 / ...

+2
source

I did this in my application, getting the Hex color code, for example # 06FF67, from my server and stored in sharedpreferences - fooobar.com/questions/40589 / ...

And when you need to set a new value coming from the server, simply override the values ​​of the general settings with the new data and install in the application

Alternatively, you can use Random Color genrater - fooobar.com/questions/98710 / ...

0
source

Source: https://habr.com/ru/post/1013086/


All Articles