Home > Blockchain >  Why does R.color.myColor return wrong color and getColor() return correct?
Why does R.color.myColor return wrong color and getColor() return correct?

Time:03-27

I have noticed that when I use R.color.myColor, I get a wrong color. But, at the same time, using getColor(R.color.myColor) gives the correct color. For example, I have a #3971e3 color in my app, it gives a purple color when using R.color.myColor but perfectly fine when using getColor(R.color.myColor) or Color.parseColor("#3971e3");.

Why is this so?

Thanks in advance

CodePudding user response:

R.color.myColor isn't the actual color code. It's a resource id that can be used to look up the color code via getColor. The reason for this is that the actual color of the resource can change based on theme, locale, or a dozen other options if you want it to. So if you need the actual color code, call getColor.

  • Related