0

I had a function that was using a deprecated method (public void setColorFilter (int color, PorterDuff.Mode mode)) and I have tried to change it using a non-deprecated method but the problem is that now using the non-deprecated method the MULTIPLY method needs API level 29:

"Field requires API level 29 (current min is 21): MULTIPLY

This is the function I was using with the deprecated method (here the MULTIPLY doesn't give that warning of required API level) ->

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

I have tried to replace the above function to stop using the deprecated method. This is what I have, but the problem is that using this alterntive, the MULTIPLY gives that warning I have said.

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

How could I solve it? I have thought of changing the MULTIPLY mode for a similar mode, but I don´t know how could I do it so that it is the same. Thanks in advance.

EDIT:

@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
            )
        );
    }
}
Lechius
  • 189
  • 7
  • What you should do is to use both methods. Check the SDK version, add an annotation to ignore warnings of this deprecated method and you're good to go. – Darkman Sep 22 '22 at 08:55
  • @Darkman what do you mean by adding an annotation? – Lechius Sep 22 '22 at 15:43
  • https://stackoverflow.com/questions/1554538/how-can-i-suppress-javac-warnings-about-deprecated-api – Darkman Sep 22 '22 at 15:46
  • @Darkman check out the edit I did, is the function I updated what you were referring ? Is that looking good? – Lechius Sep 22 '22 at 19:09
  • Yup. Looks good to me. At end of the day, you need to compile your project and test it. Good luck. – Darkman Sep 22 '22 at 19:13

0 Answers0