How to set paint.setColor(R.color.white)

Vasil Valchev picture Vasil Valchev · Oct 15, 2012 · Viewed 66k times · Source

I have a custom View that uses Paint and Canvas to draw objects. My question is how to set:

int color = R.color.white;
paint.setColor(color);

from my /res/valuse/color.xml which includes resources like

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
    ...
</resources>

Answer

Vitali Olshevski picture Vitali Olshevski · Oct 15, 2012
int color = ContextCompat.getColor(context, R.color.white);
paint.setColor(color);

The setColor() method takes a color number as int value, but not a resource id which is an int as well.