How can I programmatically change the color value in colors.xml?

I am developing an application that will be thematic with different colors and images for different clients. Although I have the option to overwrite the colors.xml file with custom colors at build time, I tend to adjust colors at runtime. I am wondering if this is some way to programmatically change the color value defined in the colors.xml file, and this new value will take effect in ALL places where it is used in the layout definition.

So, if I have:

<?xml version="1.0" encoding="UTF-8"?> <resources> <color headerColor="white">#FFF</color> <color backgroundColor="black">#000</color> </resources> 

And a layout file with something like:

 <TextView android:id="@+id/listItemHeaderActivity" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="12sp" android:textColor="@color/headerColor" android:background="@color/backgroundColor" android:text="@string/listTextHeaderActivity"/> 

Is it possible to change the value of headercolor and backgroundColor in Java and have this place in all views that use these values? Or will I have to change each corresponding kind of color separately when I show these views?

Thanks in advance.

+6
source share
2 answers

This will help if you described why and how you use this dynamic color, and why you want to do this at runtime instead of build time. But assuming this should be as you requested ...

I suggest writing a helper function that all places can call and set.

 public int getMyColor(...) { // figure out which color to use, via a database call, // an asset load, some algorithm, or whatever you need ... // once color chosen, create an RGBA integer for it final int myColor = ... return myColor; } 

Now call this for each required action / fragment and set the color attributes (a) in the respective views as necessary. (With View.setBackgroundColor(...) etc.)

However, to allow this to work in the XML preferences and layout preview, you need to write your own view class to call this helper function. Depending on where and how you use this color, it may not be worth it.

This solution is not very elegant and requires a lot of calling this custom helper function getMyColor in every action / fragment that it needs. If it is installed in only one or two places, this probably does not matter much. Again, knowing why you want to do this, and not ask us how to do this, may give you a better alternative.

For example, this is not the answer to your question, but have you thought about that? It will still have a problem installing them during the build, if you do not want all of the above, but depending on how you use this color, it may be better than the mess described above.

+1
source

In the end, I did something similar to what I think John can describe. I created a class extending the fragment (or SherlockFragment in my case). This class has a new method called "setCustomColors" that simply takes a fragment view and looks for all its children for the specific types of views that need to be configured. Then I extended this class to all of my fragments, and in the onCreateView method I call the function, passing the current view of the fragment. Let me know if this explanation is unclear (although perhaps this is not a problem, so many people will have).

0
source

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


All Articles