drawable.setColorFilter marked as deprecated

Viewed 5995

Compiler gives me that setColorFilter is deprecated!

 tab?.icon?.setColorFilter(
                ContextCompat.getColor(requireActivity(), R.color.colorLogoGrey),
                PorterDuff.Mode.SRC_IN
            )
2 Answers

As you can see from the documentation, setColorFilter(int color, PorterDuff.Mode mode) is actually deprecated from API level 29. Probably in your gradle file you have:

compileSdkVersion 29

By the way you can use setColorFilter with an instance of BlendModeColorFilter:

tab?.icon?.colorFilter(BlendModeColorFilter(R.color.colorAccent, BlendMode.SRC_IN))

Just use BlendModeColorFilterCompat, to avoid crash on old Android APIs.

BlendModeColorFilterCompat.createBlendModeColorFilterCompat(ContextCompat.getColor(yourContext, yourColor), BlendModeCompat.SRC_ATOP)
Related