Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawable.setColorFilter marked as deprecated

Compiler gives me that setColorFilter is deprecated!

 tab?.icon?.setColorFilter(
                ContextCompat.getColor(requireActivity(), R.color.colorLogoGrey),
                PorterDuff.Mode.SRC_IN
            )
like image 316
A. Albrg Avatar asked Feb 26 '20 11:02

A. Albrg


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))
like image 52
Nicola Gallazzi Avatar answered Oct 09 '22 03:10

Nicola Gallazzi


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

BlendModeColorFilterCompat.createBlendModeColorFilterCompat(ContextCompat.getColor(yourContext, yourColor), BlendModeCompat.SRC_ATOP)
like image 42
Lucas Orso Avatar answered Oct 09 '22 03:10

Lucas Orso