The Resources.getColor(int id)
method has been deprecated.
@ColorInt
@Deprecated
public int getColor(@ColorRes int id) throws NotFoundException {
return getColor(id, null);
}
What should I do?
The Resources.getColor(int id)
method has been deprecated.
@ColorInt
@Deprecated
public int getColor(@ColorRes int id) throws NotFoundException {
return getColor(id, null);
}
What should I do?
I don't want to include the Support library just for getColor, so I'm using something like
I guess the code should work just fine, and the deprecated
getColor
cannot disappear from API < 23.For Drawable
For Color
You can use:
Tested on Android Marshmallow. It worked.
tl;dr:
Explanation:
You will need to use ContextCompat.getColor(), which is part of the Support V4 Library (it will work for all the previous APIs).
If you don't already use the Support Library, you will need to add the following line to the
dependencies
array inside your appbuild.gradle
(note: it's optional if you already use the appcompat (V7) library):If you care about themes, the documentation specifies that:
This way:
I got frustrated too. My need was very straightforward. All I wanted was the ARGB color from the resources, so I wrote a simple static method.