6

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.

7
  • 1
    what did you try yet?
    – koem
    Commented Jun 24, 2013 at 17:18
  • At this point, I haven't really tried anything other than defining some defining some default color values and using them in my layout files, as you see above. I have been unable to find a way to do it as I described in my question, so I haven't tried anything like that yet.
    – RocketGuy3
    Commented Jun 24, 2013 at 17:26
  • 1
    No you cant change. You can use sharedpreferences to save the color and use them.
    – Tarun
    Commented Jun 24, 2013 at 17:26
  • i don't think its possible. you can't. Commented Jun 24, 2013 at 17:28
  • well... the only thing I can think of is the normal theming stuff android provides.
    – koem
    Commented Jun 24, 2013 at 17:33

2 Answers 2

1

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.

2
  • Our team decided we didn't want to do it at build time because they didn't like the somewhat messy scripting that would be required to copy over the color values in the colors.xml file. Also, we want the configuration to be as build-independent as possible so that the only thing we need to provide is a merchant settings file.
    – RocketGuy3
    Commented Jun 25, 2013 at 15:43
  • 1
    You could treat the bulk of the app as a shared library, then have each individual merchant instance of a 'real' app override the colors.xml file. See developer.android.com/tools/projects/index.html#LibraryProjects for more info. That would end up requiring more than 'one merchant settings file', but would solve the problem and be the 'supported' solution per Android SDK designs.
    – Jon Adams
    Commented Jun 25, 2013 at 16:02
0

I ended up doing something somewhat similar to what I think Jon may be describing. I created a class extending Fragment (or SherlockFragment in my case). That class has a new method called "setCustomColors" that just takes the fragment view and searches all of its children for certain types of views that need to be customized. I then extended that class for all of my Fragments, and in the onCreateView method, I call the function, passing in the current fragment view. Let me know if that explanation is unclear (although maybe this isn't a problem very many people will have).

Not the answer you're looking for? Browse other questions tagged or ask your own question.