7

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

Nick
  • 6,900
  • 5
  • 45
  • 66

3 Answers3

10

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>
Kiril Kirilov
  • 11,167
  • 5
  • 49
  • 74
  • Ah, this is why I love StackOverflow. You guys are like magic. Thank you! – Nick Feb 23 '11 at 22:52
  • Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers/8259#8259) to include the essential parts of the answer here, and provide the link for reference. – Benjol Feb 24 '11 at 10:53
1

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

Jeff Gilfelt
  • 26,131
  • 7
  • 48
  • 47
0

You may need to escape that pound symbol.

http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

gdonald
  • 984
  • 2
  • 12
  • 23