Home » Android » android – getResources().getColor() is deprecated

android – getResources().getColor() is deprecated

Posted by: admin March 10, 2020 Leave a comment


buildToolsVersion "22.0.1" ,
targetSdkVersion 22 in my gradle file.

I found that the useful getResources().getColor(R.color.color_name) is deprecated.

What should I use instead?

How to&Answers:

It looks like the best approach is to use:

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



This will choose the Marshmallow two parameter method or the pre-Marshmallow method appropriately.


well it’s deprecated in android M so you must make exception for android M and lower. Just add current theme on getColor function. You can get current theme with getTheme().

This will do the trick in fragment, you can replace getActivity() with getBaseContext(), yourContext, etc which hold your current context

    yourTitle.setTextColor(getActivity().getResources().getColor(android.R.color.white, getActivity().getTheme()));
}else {

*p.s : color is deprecated in M, but drawable is deprecated in L


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

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

As specified in the documentation, “Starting in M, the returned color will be styled for the specified Context’s theme”. SO no need to worry about it.

You can add the Support V4 library by adding the following to the dependencies array inside your app build.gradle:

compile 'com.android.support:support-v4:23.0.1'


I found that the useful getResources().getColor(R.color.color_name) is deprecated.

It is not deprecated in API Level 21, according to the documentation.

It is deprecated in the M Developer Preview. However, the replacement method (a two-parameter getColor() that takes the color resource ID and a Resources.Theme object) is only available in the M Developer Preview.

Hence, right now, continue using the single-parameter getColor() method. Later this year, consider using the two-parameter getColor() method on Android M devices, falling back to the deprecated single-parameter getColor() method on older devices.