araks araks -5 years ago 462
Android Question

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


Resources.getColor(int id)
method has been deprecated.

public int getColor(@ColorRes int id) throws NotFoundException {
return getColor(id, null);

What can I do?

Answer Source

UPDATE 2015-08-22

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

So, just call:

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

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.

This is the ContextCompat.getColor() source code from the Support Library:

public static final int getColor(Context context, int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 23) {
        return ContextCompatApi23.getColor(context, id);
    } else {
        return context.getResources().getColor(id);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download