views:

77

answers:

1

Here is my color XML

<resources>
    <drawable name="red">#7f00</drawable>
    <drawable name="blue">#770000ff</drawable>
    <drawable name="green">#7700ff00</drawable>
    <drawable name="yellow">#77ffff00</drawable>

    <drawable name="screen_background_black">#ff000000</drawable>
    <drawable name="translucent_background">#e0000000</drawable>
    <drawable name="transparent_background">#00000000</drawable>

    <color name="solid_red">#ED1C24</color>
    <color name="solid_blue">#0000ff</color>
    <color name="solid_green">#39B54A</color>
    <color name="solid_yellow">#ffffff00</color>

</resources>

Here is my Java code:

if (floatedChange < 0)
                            changeText.setTextColor(R.color.solid_red); //red
                        else
                            changeText.setTextColor(R.color.solid_green); //green

This works when I use Color.RED or Color.GREEN, but when I use my own colors. The color doesn't show up.

Why?

+1  A: 

Do like this:

In your XML-file, use style:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="YourCustomText">
        <item name="android:textColor">#FFFF00</item>
    </style>
</resources>

then, in your .java code:

//Here you assign the style that is defined in YourCustomText
yourTextView.setTextAppearance(this, R.style.YourCustomText);
Charlie Sheen
mine is located in /res/values/color.xml
Sheehan Alam
Do you have the "<?xml..." tag in the beginning?
Charlie Sheen
I do have the <?xml tag in the beginning. I changed my code to: changeText.setTextColor(R.style.solid_red); //but solid_red can't be found
Sheehan Alam
Sec, will check this out myself.
Charlie Sheen
Look at my edited post soon...
Charlie Sheen
But I can't access R.style.solid_red because it is part of R.color.solid_red
Sheehan Alam
Look at my updated code.
Charlie Sheen
Updated to your code, still the same results :(
Sheehan Alam
Well, updated again. I have checked this solution on the emulator.
Charlie Sheen
yep, that worked great. seems a bit overkill to go through this much work, just to change the text color. Is this normal?
Sheehan Alam
It's smart to do if you want to have a collection with colors. Personally, I usually define it directly to the TextView. You can also use your collection in an XML file using "style="@style/YourCustomText"".
Charlie Sheen