Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting textColor in TextView in layout/main.xml main layout file not referencing colors.xml file. (It wants a #RRGGBB instead of @color/text_color)

Tags:

android

I'm trying to set some general colors for a program I'm writing. I created a colors.xml file and am trying to directly reference the colors from the layout.xml file. I believe I'm am doing this correctly however it's giving me the following error:

Color value '@colors/text_color' must start with #

Here is my res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="background_color">#888888</color>
    <color name="text_color">#00FFFF</color>
</resources>

Here is my res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical">
<TextView  
    android:layout_width="fill_parent" 
    android:text="@string/hello" 
    android:layout_height="wrap_content" 
    android:id="@+id/TextView01" 
    android:textColor="@colors/text_color"/>
</LinearLayout>

I looked at some references on the android developers site: More Resource Types : Color and found this code:

Example:XML file saved at res/values/colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="opaque_red">#f00</color>
   <color name="translucent_red">#80ff0000</color>
</resources>

This application code retrieves the color resource:

Resources res = getResources();
int color = res.getColor(R.color.opaque_red);

This layout XML applies the color to an attribute:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/translucent_red"
    android:text="Hello"/>

I think my two xml files follow this example pretty close - however the only difference is that I haven't used any application code to retrieve the color resource. I don't believe this is necessary (but it is a difference.) I thought I'd see if anyone else had similar problems or a solution? or is this a bug?

I did update all my android sdk (and Eclipse plugin) files last week so I believe them to be the latest.

like image 924
user657019 Avatar asked Mar 12 '11 23:03

user657019


3 Answers

A variation using just standard color code:

android:textColor="#ff0000"
like image 122
Gene Bo Avatar answered Oct 22 '22 20:10

Gene Bo


After experimenting on that case: android:textColor="@colors/text_color" is wrong since @color is not filename dependant. You can name your resource file foobar.xml, it doesn't matter but if you have defined some colors in it you can access them using @color/some_color.

Update:

file location: res/values/colors.xml The filename is arbitrary. The element's name will be used as the resource ID. (Source)

like image 27
Renaud Avatar answered Oct 22 '22 20:10

Renaud


You have a typo in your xml; it should be:

android:textColor="@color/text_color"

that's "@color" without the 's'.

like image 20
strangecargo Avatar answered Oct 22 '22 19:10

strangecargo