The getColor()
method of the Resources
class in Android is deprecated as of API level 23 (Android 6.0 Marshmallow). This method was used to get a color value from a color resource, which is an integer value that represents a color.
The recommended way to get a color value from a color resource is to use the getColor()
method of the ContextCompat
class, which is part of the Android Support Library. This method is not deprecated and can be used on all versions of Android.
Here's an example of how you could use the getColor()
method of the ContextCompat
class to get a color value from a color resource:
import android.content.Context; import androidx.core.content.ContextCompat; int color = ContextCompat.getColor(context, R.color.my_color);
In this example, the color
variable will contain the color value of the my_color
color resource.
Note that the R.color.my_color
syntax is used to reference a color resource in the res/values
directory of your project. You will need to replace my_color
with the name of your color resource.
You can also use the getColorStateList()
method of the ContextCompat
class to get a ColorStateList
object from a color resource, which represents a color that changes based on the view's state.