Did you know: ContextCompat

Lately I’ve been refactoring some old code and one of the common issues I found was about deprecated calls to methods in the Resources class. I use the getDrawable(int) and getColor(int) calls in a lot of places . The suggestion from lint is that I use the matching method calls with an extra Theme parameter for styling purposes.

If you’d rather have the system handle that extra parameters for you, then maybe ContextCompat is for you. ContextCompat will provide a theme styled for the specified Context’s theme.

Instead of:

<activity>.getResources().getColor(R.color.levelColor)

you would:

ContextCompat.getColor(<context>,R.color.levelColor);

The same applies for most of the deprecated methods in Resources like getColorStateList. Check out ContextCompat.