David David - 27 days ago 11
Android Question

getResources().getColor() is deprecated

Using:

buildToolsVersion "22.0.1"
,
targetSdkVersion 22
in my gradle file.

I found that the useful
getResources().getColor(R.color.color_name)
is deprecated.

What should I use instead?

Answer

It looks like the best approach is to use:

ContextCompat.getColor(context, R.color.color_name)

This will choose the Marshmallow two parameter method or the pre-Marshmallow method appropriately.

Comments