getResources().getColor() is deprecated

It looks like the best approach is to use ContextCompat.getColor() method:
This method will choose the Marshmallow two parameter method or the pre-Marshmallow method appropriately.

How to use ContextCompat.getColor() method:

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

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



Code For all the Kotlin users out there:

context?.let {
    val color = ContextCompat.getColor(it, R.color.colorPrimary)
    // ...
}

No comments:

Post a Comment

Popular Posts