tags:

views:

70

answers:

2

When I look at misc. Android tutorials and examples when it comes to specifying colors I very often see constants like @color/red or @color/black etc. being used. For some strange reason that NEVER works for me! I always need to specify colors using the "#RGB", #ARGB, ..., #AARRGGBB notation.

As soon, as I try to use any of those mnemonic constants like e.g. "@color/red" I am getting error messages like these:

[...] C:\Users\mmo\Test\res\drawable\edit_text.xml:5: error: Error: No resource found that matches the given name (at 'drawable' with value '@color/orange').
[...] C:\Users\mmo\Test\res\drawable\myDrawable.xml:3: error: Error: No resource found that matches the given name (at 'drawable' with value '@color/black').
[...] C:\Users\mmo\Test\res\drawable\myDrawable.xml:4: error: Error: No resource found that matches the given name (at 'drawable' with value '@color/black').
[...] C:\Users\mmo\Test\res\drawable\myDrawable.xml:5: error: Error: No resource found that matches the given name (at 'drawable' with value '@color/green').
[...] C:\Users\mmo\Test\res\drawable\myDrawable.xml:6: error: Error: No resource found that matches the given name (at 'drawable' with value '@color/black').

Why is that so? Why can't I use these predefined constants? Do I need to prefix them with some package name (I tried @android:color/red but that only caused a different error)? Do I need to specify these colors myself? If so: how and where? Any ideas or suggestions?

Michael

+2  A: 

If you want to use the colors pre-defined in the Android platform, the syntax is @android:color/white. The "android:" at the beginning indicates that the resource is not part of your application.

Romain Guy
Nope, as I already wrote above: that doesn't work - at least not for me. Is there any special setting or "import" (or whatever) required to make this working?
mmo
Actually, I just had to learn, that this DOES works, but apparently only the "colors" @android:color/white and @android:color/black are defined. All others I tried (like @android:color/red, @android:color/green, @android:color/blue, etc.) yielded errors. Very odd!
mmo
It's only odd if you expect these colors to be defined by the platform, they are not. Usually if you see a sample app with @color/ that means the color was defined in the app itself.
Romain Guy
+1  A: 

Is "colors.xml" added to your res/values folder where these color constants are defined?

dwarkesh
I created such a file in my app now and defined a bunch of default color there and now I can indeed use such named colors. Apparently all those examples I have seen assumed the existance of such a file without ever explicitly stating that. Thus I falsely had come to the conclusion that these values were predefined.
mmo