Unresolved reference: ContextCompat in Kotlin Android

  private fun getDefaultCustomTabsIntentBuilder(activity: Activity): CustomTabsIntent.Builder {         val backArrow = getBitmapFromVectorDrawable(activity)         return CustomTabsIntent.Builder()                 .addDefaultShareMenuItem()                 .setToolbarColor(activity.resources.ContextCompat.getColor(R.color.colorPrimary))                 .setShowTitle(true)                 .setCloseButtonIcon(backArrow!!) 

When I am using ContextCompat it shows Unresolved reference: ContextCompat . I am new with this Tnx in Advance

Add Comment
2 Answer(s)

Do it like this:

private fun getDefaultCustomTabsIntentBuilder(activity: Activity): CustomTabsIntent.Builder {         val backArrow = getBitmapFromVectorDrawable(activity)         return CustomTabsIntent.Builder()                 .addDefaultShareMenuItem()                 .setToolbarColor(ContextCompat.getColor(activity,R.color.colorPrimary))                 .setShowTitle(true)                 .setCloseButtonIcon(backArrow!!)     } 
Add Comment

My ContextCompat is the member of androidx.

So I import androidx.core.content.ContextCompat on the top of file.

And then I can use ContextCompat like this ContextCompat.getColor(context, R.color.dark).

Answered on July 15, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.