Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am developing an application that will be themed with different colors and images for different clients. While I have the option to re-write the colors.xml file with the custom colors at build time, I am leaning towards setting up the colors at runtime. What I am wondering is if that is some way to programmatically change the value of a color defined in the colors.xml file and have that new value take effect in ALL places where it is used in the layout definition.

So in other words 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"/>

Can I change the value of headercolor and backgroundColor in Java and have it take place in all Views that use those values? Or will I have to change each relevant View color individually whenever I display those views?

Thanks in advance.

like image 323
RocketGuy3 Avatar asked Jun 24 '13 17:06

RocketGuy3


1 Answers

It would help if you described why and how you are using this dynamic color, and why you want to do this at runtime instead of build time. But assuming it has to be the way you asked...

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

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 on every activity/fragment that needs it and set the color attribute(s) on the appropriate views as needed. (With View.setBackgroundColor(...), etc.)

However, to allow this to work in XML settings and/or development layout previews, you would have to write a custom view class to call that helper function too. Depending on where and how you will be using this color, it may not be worth it.

This solution isn't very elegant and requires a lot of calling this custom getMyColor helper function in every activity/fragment that needs it. If it is only set one or two places though, it's probably not a big deal. Again, knowing why you want to do this instead of asking us how to do it, may yield a better alternative for you.

For example—this isn't an answer to your question—but have you thought about themes? It will still have the problem of having to set them at build time unless you want all of the above, but depending on how you're using this color, it might be better than the mess I outlined above.

like image 61
Jon Adams Avatar answered Oct 05 '22 12:10

Jon Adams