Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setColorFilter not working with resource color

Tags:

android

If I use setColorFilter like this, it works and paint my image with the chosen color:

int color = Color.parseColor("#FF0000"); 
viewHolder.Icon.setColorFilter(color);

But if I try to use a resource color, it does nothing:

viewHolder.Icon.setColorFilter(R.color.colorPrimary);

where the resource is defined like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#FF5722</color>
</resources>
like image 211
rbasniak Avatar asked Nov 06 '15 23:11

rbasniak


3 Answers

Answer marked as "right" use getColor() method which is deprecated. That's why here is up-to-date answer:

int color = ResourcesCompat.getColor(getResources(), R.color.my_color, null);
like image 123
Chuck Avatar answered Nov 19 '22 15:11

Chuck


try this

int actionBarBackground = getResources().getColor(R.color.actionBarBackground);

and you set actionBarBackground in the method setColorFilter thats all

like image 33
brayan camilo villate leon Avatar answered Nov 19 '22 16:11

brayan camilo villate leon


please add PorterDuff.Mode.MULTIPLY See below example

viewHolder.Icon.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );

like image 1
Patel vaishu Avatar answered Nov 19 '22 15:11

Patel vaishu