Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 216
Vasil Valchev Avatar asked Oct 15 '12 15:10

Vasil Valchev


People also ask

How do I change the color of my paint on Android?

What you need to do is set the color between drawing. paint. setColor(color. RED); // Will apply to first path.

What is paint Anti_alias_flag?

ANTI_ALIAS_FLAG. Paint flag that enables antialiasing when drawing.


4 Answers

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.

like image 132
olshevski Avatar answered Oct 05 '22 18:10

olshevski


first get your color from xml file

int color = context.getResources().getColor(R.color.colorPrimary); // old

is deprecated now, use this instead

int color = ContextCompat.getColor(context, R.color.colorPrimary); // new

set color

paint.setColor(color);

xml file preview: res/values/color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>
like image 44
Vasil Valchev Avatar answered Oct 05 '22 19:10

Vasil Valchev


Try using color.white:

paint.setColor(Color.white)
like image 35
Hani Hussein Avatar answered Oct 05 '22 20:10

Hani Hussein


paint.setColor(Color.parseColor("#FFFFFF"))
like image 20
Michael Rhodes Avatar answered Oct 05 '22 18:10

Michael Rhodes