Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android background string resource

I am working on a layout XML file, and I want to set a background color for a LinearLayout. This bit, which I am now using, works perfectly:

android:background="#1a64b7"

However, I would much prefer to break that out. In my strings.xml file I have

<string name="bg_blue">#1a64b7</string>

but when I use that in the following manner:

android:background="@string/bg_blue"

it shows up great in the Graphical Layout Preview in Eclipse, but the app crashes as soon as it opens. Any ideas? Thanks a ton.

Nick

like image 342
Nick Avatar asked Feb 23 '11 22:02

Nick


2 Answers

This is the right way to do it!

For example you need some resources xml with lines like:

<resources>
    <color name="candidate_normal">#FF000000</color>
    <color name="candidate_recommended">#FFE35900</color>
    <color name="candidate_other">#ff808080</color>
    <color name="candidate_background">#bbffffff</color>
</resources>
like image 94
Kiril Kirilov Avatar answered Sep 23 '22 09:09

Kiril Kirilov


It is best practise to keep your colors defined in colors.xml, and reference them as "@color/bg_blue". Keep your strings.xml for language copy only.

http://developer.android.com/guide/topics/resources/more-resources.html#Color

like image 44
Jeff Gilfelt Avatar answered Sep 20 '22 09:09

Jeff Gilfelt