setColorFilter is deprecated on API29
android
Solution
Try this:
public class MyDrawableCompat {
public static void setColorFilter(@NonNull Drawable drawable, @ColorInt int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
drawable.setColorFilter(new BlendModeColorFilter(color, BlendMode.SRC_ATOP));
} else {
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
}
}
And this:
MyDrawableCompat.setColorFilter(mydrawable.getBackground(), color);
UPDATE: Just use the latest version of the core androidx library and this code:
mydrawable.colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(color, BlendModeCompat.SRC_ATOP)
Problem
I use the following line to change the color of a VectorDrawable: `mydrawable.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP)` This works nice, though it is now deprecated. The documentation suggests that I use: `mydrawable.getBackground().setColorFilter(new BlendModeColorFilter(color, PorterDuff.Mode.SRC_ATOP))` Though, `BlendModeColorFilter` is only available on API29. After examining the source of the deprecated method, I have realized that it calls: `new PorterDuffColorFilter()` So, I went ahead and used: `mydrawable.getBackground().setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP))` The coloring worked. Is this the right replacement for the deprecated method or I must use BlendModeColorFilter on API29? Thank you.