Styling Colors & Drawables w/ Theme Attributes
You’ve probably noticed that when you write something like:
context.getResources().getColor(R.color.some_color_resource_id);
Android Studio will give you a lint message warning you that the
Resources#getColor(int)
method was deprecated in Marshmallow in favor of the
new, Theme
-aware Resources#getColor(int, Theme)
method. You also
probably know by now that the easy alternative to avoiding this lint warning
these days is to call:
ContextCompat.getColor(context, R.color.some_color_resource_id);
which under-the-hood is essentially just a shorthand way of writing:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return context.getResources().getColor(id, context.getTheme());
} else {
return context.getResources().getColor(id);
}
Easy enough. But what is actually going on here? Why were these methods
deprecated in the first place and what do the new Theme
-aware methods have to
offer that didn’t exist before?