getResources().getColor() is deprecated [duplicate]
Asked Answered
N

4

568

Using: 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?

Nettle answered 5/8, 2015 at 21:16 Comment(5)
Where do you see that? developer.android.com/reference/android/content/res/…Cissy
This has been discussed here: #31591214Intensive
Use ContextCompat.getColor(context, R.color.color_name)Gina
in Adapter context.getResources().Phenacaine
ContextCompat.getColor(context, R.color.colorname) For Fragments it would be "activity as Context" and for activities it will be "this"Throstle
P
1267

It looks like the best approach is to use:

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

eg:

yourView.setBackgroundColor(ContextCompat.getColor(applicationContext,
                            R.color.colorAccent))

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

Patio answered 25/8, 2015 at 11:0 Comment(3)
Kotlin: replace 'applicationContext' with 'this'Lollar
to use in Kotlin as Ben Said ContextCompat.getColor(this, R.color.color_name)Antiserum
What if you have an instance of "Resources" and not of "Context"?Cupping
C
74

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

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    yourTitle.setTextColor(getActivity().getResources().getColor(android.R.color.white, getActivity().getTheme()));
}else {
    yourTitle.setTextColor(getActivity().getResources().getColor(android.R.color.white));
}

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

Coward answered 28/12, 2015 at 3:26 Comment(2)
Instead of getActivity().getTheme(), I'd use getContext().getTheme()Hairline
Its very usefull.Its working for meMartsen
M
49

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'
Martingale answered 11/9, 2015 at 7:53 Comment(1)
What if you have an instance of "Resources" and not of "Context"?Cupping
K
26

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.

Ker answered 5/8, 2015 at 21:21 Comment(1)
Why did Android's developers not just keep the method which can internally select from a default option, and provide the second overloaded method for those that need it. Google need to stop foisting their own developer concerns on the masses. It's needless change and over complication.Mcmasters

© 2022 - 2024 — McMap. All rights reserved.