Alternative to setColorFilter(int,Mode)

Viewed 45

I have seen that the following method is deprecated : public void setColorFilter (int color, PorterDuff.Mode mode). In the Android documentation I have seen the following recommendation:

This method was deprecated in API level 29.
use setColorFilter(android.graphics.ColorFilter) with an instance of BlendModeColorFilter

I am trying to replace that method to a non-deprecated method but I have not achieved it. I have seen similar posts to this one but they are kotlin version. How could I replace this deprecated method for a non-deprecated method in Java?

Thanks

edit:

What I had using the deprecated method:

 public static void setcol(TextView dLabel) {
       
dLabel.getBackground().setColorFilter(color,android.graphics.PorterDuff.Mode.MULTIPLY);
}

what I have changed to use non-deprecated method that is apparently working to replace the above deprecated method:

 public static void setcol(TextView dLabel) {
           
     dLabel.getBackground().setColorFilter(
                BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
                        color,
                        BlendModeCompat.SRC_OVER
                )
        );
}
1 Answers

For me, this works in Java:

@SuppressWarnings("deprecation")
public static void setcol(TextView dLabel) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
        dLabel.getBackground().setColorFilter(
            color, 
            android.graphics.PorterDuff.Mode.MULTIPLY
        );
    } else {
        dLabel.getBackground().setColorFilter(
            BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
                color,
                BlendModeCompat.MULTIPLY
            )
        );
    }
}

Change the color to you desired color and the some_drawable_id to your drawable.

Related