Sunday, February 13, 2011

TextView background is black when setting colour from xml?

I had a colour defined in a 'colours.xml' file in my project's 'values' folder like so:

<?xml version="1.0" encoding="utf-8"?>
    <color name="tvBackground">#337700</color>

Which, for those of you who cannot transpose directly from hex, looks like this:

My code looked like this:

   TextView tv = new TextView(getApplicationContext());

Seems reasonable doesn't it?

But when I ran my code, my shiny new little TextView came out with the background colour of this:

(that's #000000 in hex, or 'black' btw)

What was going on here?
Turns out, I wasn't using the correct method to set the background colour so that it comes out correctly, what I should have used was either of these:

- or -

.. Not especially well documented, but easily fixed.

Anyhow, now my TextView shows with the background colour of:

.. So all is now well with the world. Thought I'd just post this in case it helps someone out there (or myself when I forget next time)

until that next time, bye bye.

No comments:

Post a Comment