Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getColor(int id) deprecated on Android 6.0 Marshmallow (API 23)

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?

like image 281
araks Avatar asked Jul 23 '15 14:07

araks


1 Answers

Starting from Android Support Library 23,
a new getColor() method has been added to ContextCompat.

Its description from the official JavaDoc:

Returns a color associated with a particular resource ID

Starting in M, the returned color will be styled for the specified Context's theme.


So, just call:

ContextCompat.getColor(context, R.color.your_color); 

You can check the ContextCompat.getColor() source code on GitHub.

like image 125
araks Avatar answered Sep 22 '22 04:09

araks