Unresolved reference: animateColorAsState

Viewed 568

I'm trying to understand Android Compose Animation and I get this error

import androidx.compose.animation.animateColorAsState
...

val backgroundColor by animateColorAsState(if (isSelected) Color.Red else Color.Transparent)

Android Studio highlights animateColorAsState in red and writes " Unresolved reference: animateColorAsState"

2 Answers

Make sure you are using the correct "Color": below is an example

// notice the androidx.comose.ui.graphics.Color and MaterialTheme.colors (which returns androidx.comose.ui.graphics.Color)
val surfaceColor: androidx.compose.ui.graphics.Color by animateColorAsState(
   if (isExpanded) MaterialTheme.colors.primary else MaterialTheme.colors.surface
)

In your example, I think you are using incorrect "Color". You should check your import on if you are using:

import androidx.compose.ui.graphics.Color // <--- Correct one
// and NOT below
import android.graphics.Color // <--- Incorrect one

The name has changed to animateAsState

Related