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

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

Posted by: admin November 1, 2017 Leave a comment

Questions:

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

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

What can I do?

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.

Questions:
Answers:

You will need to use ContextCompat.getColor(), which is part of the Support V4 Library (so it will work for all the previous APIs).

ContextCompat.getColor(context, R.color.my_color)

If you don’t already use the Support Library, you will need add the following line to the dependencies array inside your app build.gradle (note: it’s optional if you already use the appcompat (V7) library):

compile 'com.android.support:support-v4:23.0.0' # or any version above

If you care about themes, the documentation specifies that:

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

Questions:
Answers:

I don’t want to include the Support library just for getColor, so I’m using something like

public static int getColorWrapper(Context context, int id) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return context.getColor(id);
    } else {
        //noinspection deprecation
        return context.getResources().getColor(id);
    }
}

I guess the code should work just fine, and the deprecated getColor cannot disappear from API < 23.

Questions:
Answers:

In Android Marshmallow many methods are deprecated.

For example, to get color use

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

Also to get drawable use

ContextCompat.getDrawable(context, R.drawable.drawble_name);

Questions:
Answers:

For Drawable

ContextCompat.getDrawable(getApplicationContext(),R.drawable.back_arrow)

For Color

 ContextCompat.getColor(getApplicationContext(),R.color.red)

Questions:
Answers:

The getColor() method is deprecated in API 23, and it was introduced in ContextCompat. Before using the getColor() method using ContextCompat, add the dependencies in the build.gradle script, which is:

dependencies{

    // Other stuff

    compile 'com.android.support:support-v4:23.0.0'
}

Then the getColor() method can be used like:

int colorcode = ContextCompat.getColor(getApplicationContext(), R.color.your_color);

Questions:
Answers:

The shortest answer is:

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

Questions:
Answers:

You can use:

if (Build.VERSION.SDK_INT >= 23) {
    return ctx.getColor(R.color.YOURCOLOR);
}
else {
    return ctx.getRecources().getColor(R.color.YOURCOLOR);
}

I hope it helps. I tested it on Android Marshmallow, and it worked.

Questions:
Answers:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  return context.getResources().getColor(id, context.getTheme());
} else {
  return context.getResources().getColor(id);
}

make use of this .It helps

Questions:
Answers:

Use the getColor(Resources, int, Theme) method of the ResourcesCompat from the Android Support Library.

int white = new ResourcesCompat().getColor(getResources(), R.color.white, null);

I think it reflect better your question than the getColor(Context, int) of the ContextCompat since you ask about Resources. Prior to API level 23, the theme will not be applied and the method calls through to getColor(int) but you’ll not have the deprecated warning. The theme also may be null.

Questions:
Answers:

In Mono / xamarin.android I had the same issue and the following code worked perfectly:

ContextCompat.GetColor (context, Resource.Color.myColor)

In my case the context is my activity (Android.Support.V7.App.AppCompatActivity) so a my real-usage example is:

var color = new Android.Graphics.Color(ContextCompat.GetColor(this, Resource.Color.myColor))    

Questions:
Answers:

Even though getColor() is deprecated in marshmallow, the replacement method
getColor(int, Theme) is available. Which can be used as an alternative to ContextCompat.getColor(context, R.color.your_color);

refer this for more information.

Questions:
Answers:

This way:

fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.badge_participants_counter_color)));

Questions:
Answers:

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.

protected static int getARGBColor(Context c, int resId)
        throws Resources.NotFoundException {

    TypedValue color = new TypedValue();
    try {
        c.getResources().getValue(resId, color, true);
    }
    catch (Resources.NotFoundException e) {
        throw(new Resources.NotFoundException(
                  String.format("Failed to find color for resourse id 0x%08x",
                                resId)));
    }
    if (color.type != TYPE_INT_COLOR_ARGB8) {
        throw(new Resources.NotFoundException(
                  String.format(
                      "Resourse id 0x%08x is of type 0x%02d. Expected TYPE_INT_COLOR_ARGB8",
                      resId, color.type))
        );
    }
    return color.data;
}