Challenge - 5 Problems
Animation Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this animation code?
Given this Kotlin Compose code snippet, what will the UI show after the button is clicked once?
Android Kotlin
var expanded by remember { mutableStateOf(false) }
val size by animateDpAsState(targetValue = if (expanded) 100.dp else 50.dp)
Column {
Box(Modifier.size(size).background(Color.Red))
Button(onClick = { expanded = !expanded }) { Text("Toggle") }
}Attempts:
2 left
💡 Hint
animateDpAsState smoothly changes the size value over time.
✗ Incorrect
animateDpAsState animates the size value between 50.dp and 100.dp when the target changes, so the box size changes smoothly on button click.
❓ lifecycle
intermediate2:00remaining
What happens if you remove remember from the state?
Consider this code snippet without remember:
var expanded by mutableStateOf(false)
val size by animateDpAsState(targetValue = if (expanded) 100.dp else 50.dp)
What will happen when the button toggles expanded?
Attempts:
2 left
💡 Hint
remember keeps state across recompositions in Compose.
✗ Incorrect
Without remember, the state resets on every recomposition, so the animation target changes unexpectedly and the UI does not behave as intended.
📝 Syntax
advanced2:00remaining
Which option correctly animates a color change using animateColorAsState?
Select the correct Kotlin Compose code snippet that animates a Box background color between Color.Red and Color.Green based on a Boolean state.
Attempts:
2 left
💡 Hint
animateColorAsState returns a State, use 'by' to get the Color value.
✗ Incorrect
Option A correctly uses 'by' to get the Color value and applies it directly to background modifier.
🔧 Debug
advanced2:00remaining
Why does this animation not run smoothly?
This code animates a Float value but the animation jumps instantly instead of animating smoothly:
var progress by remember { mutableStateOf(0f) }
val animatedProgress by animateFloatAsState(targetValue = progress)
Button(onClick = { progress += 0.5f }) { Text("Increase") }
What is the likely cause?
Attempts:
2 left
💡 Hint
animateFloatAsState animates between values but may behave unexpectedly if values go beyond expected range.
✗ Incorrect
If progress exceeds 1.0f (common max for progress), the animation may jump or behave unexpectedly because the UI expects progress in 0..1 range.
🧠 Conceptual
expert2:00remaining
What is the main benefit of using animate*AsState in Jetpack Compose?
Choose the best explanation for why animate*AsState functions are useful in Compose animations.
Attempts:
2 left
💡 Hint
animate*AsState links animation to state changes automatically.
✗ Incorrect
animate*AsState functions animate smoothly between old and new target values when state changes, reducing boilerplate and integrating well with Compose's reactive model.