Challenge - 5 Problems
Scaffold and TopAppBar Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this Scaffold with TopAppBar code?
Consider this Kotlin Compose code snippet. What will the user see on the screen?
Android Kotlin
import androidx.compose.material3.* import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier @Composable fun MyScreen() { Scaffold( topBar = { TopAppBar( title = { Text("Welcome") } ) } ) { paddingValues -> Text("Hello Scaffold", modifier = Modifier.padding(paddingValues)) } }
Attempts:
2 left
💡 Hint
Remember Scaffold's topBar slot shows the TopAppBar and content respects padding.
✗ Incorrect
The Scaffold composable places the TopAppBar at the top with the title 'Welcome'. The content inside Scaffold uses the paddingValues to avoid overlapping the top bar, so 'Hello Scaffold' text appears below the bar with correct spacing.
❓ lifecycle
intermediate2:00remaining
When is the TopAppBar recomposed in this Scaffold?
Given this Scaffold with a TopAppBar and a mutable state for the title, when does the TopAppBar recomposition happen?
Android Kotlin
import androidx.compose.material3.* import androidx.compose.runtime.* @Composable fun TitleScreen() { var title by remember { mutableStateOf("Home") } Scaffold( topBar = { TopAppBar(title = { Text(title) }) } ) { Button(onClick = { title = "Profile" }) { Text("Change Title") } } }
Attempts:
2 left
💡 Hint
Think about what triggers recomposition in Compose with mutableStateOf.
✗ Incorrect
The TopAppBar reads the 'title' state. When the button changes 'title' from 'Home' to 'Profile', Compose recomposes the TopAppBar to update the displayed title.
advanced
2:00remaining
How to add a navigation icon to the TopAppBar in Scaffold?
You want to add a back arrow icon on the left side of the TopAppBar that triggers a function when clicked. Which code snippet correctly adds this navigation icon?
Attempts:
2 left
💡 Hint
The navigationIcon parameter expects a composable lambda with an IconButton wrapping the Icon.
✗ Incorrect
Option A correctly uses the navigationIcon slot with an IconButton that calls onBack() when clicked, wrapping the Icon with proper contentDescription for accessibility.
📝 Syntax
advanced2:00remaining
What error does this Scaffold code produce?
Analyze this Kotlin Compose code snippet. What error will it cause?
Android Kotlin
Scaffold(
topBar = {
TopAppBar(
title = Text("Title")
)
}
) {
Text("Content")
}Attempts:
2 left
💡 Hint
Check the expected type of the title parameter in TopAppBar.
✗ Incorrect
The title parameter expects a composable lambda: title = { Text("Title") }. Passing Text("Title") directly is a type mismatch causing a compile error.
🔧 Debug
expert2:00remaining
Why does the content overlap the TopAppBar in this Scaffold?
This Scaffold code shows the TopAppBar but the content text is drawn behind it, overlapping. What is the cause?
Android Kotlin
Scaffold(
topBar = {
TopAppBar(title = { Text("Header") })
}
) {
Text("Overlapping content")
}Attempts:
2 left
💡 Hint
Scaffold provides paddingValues to content lambda to avoid overlapping system bars and topBar.
✗ Incorrect
The content lambda receives paddingValues which must be applied to the content's modifier to avoid drawing behind the TopAppBar. Without it, content overlaps the bar.