MMike
MMike

Reputation: 598

Difference between getResources().getColor(R.color.Black) and directly R.color.Black

i noticed a strange behavior of eclipse?

I I have a static variable:

public static colorID = R.color.Black;

Sometimes it matches with R.color.Black:

if(colorID == R.color.Black)
     //sometimes it works

sometimes i Need to use

if(colorID == getResources().getColor(R.color.Black));

It's really annoying to find the fault by testing if your code is big...

So i want to know why it sometimes work why not, and why the returning integer is sometimes different and sometimes not. They are both referencing to R.color.Black in my custom color.xml file in my values Folder. So why they are different sometimes?

Upvotes: 3

Views: 452

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93589

getColor returns the actual AARRGGBB color value. R.color.Black is an id that holds a color, which may or may not be black. One is the actual value, one is a reference to the color.

Upvotes: 3

Related Questions