Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing hex color values in strings.xml

I'm trying to store the hex color value of my text in strings.xml so all the layout files will refer to that (to be able to quickly change all layout text for the project easily) however I'm having trouble referring to it.

Using android:textColor="#FFFFFF" in my xml layout works fine. However using android:textColor="@strings/textColor" gives me an error both when I include a # and not include.

When I don't include the # it asks for the #. When I do add the # DDMS reports:

07-13 04:35:22.870: ERROR/AndroidRuntime(331): Caused by: android.content.res.Resources$NotFoundException: File #FF0000 from drawable resource ID #0x7f040003: .xml extension required

Does anyone know how I can combine statements in the layout file? eg textColor="#"+"@strings/textColor and then just set the string to "FFFFFF" for example.

like image 945
jblz Avatar asked Jul 13 '11 04:07

jblz


1 Answers

You need to create a set of styles in your xml (regularly in res/values/styles.xml)

<color name="gray">#eaeaea</color>
<color name="titlebackgroundcolor">#00abd7</color>
<color name="titlecolor">#666666</color>

In the layout files you can call to the colors or styles:

android:textColor="@color/titlecolor"

Checkout some examples:

http://developer.android.com/guide/topics/ui/themes.html

like image 144
chroman Avatar answered Sep 21 '22 00:09

chroman