0
0
Android Kotlinmobile~20 mins

Scaffold and TopAppBar in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Scaffold and TopAppBar Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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))
  }
}
AA screen with a top bar titled 'Welcome' and below it the text 'Hello Scaffold' with proper padding.
BA screen with only the text 'Hello Scaffold' centered and no top bar visible.
CA screen with a top bar but no title, and the text 'Hello Scaffold' overlapping the top bar.
DA blank screen with no visible UI elements.
Attempts:
2 left
💡 Hint
Remember Scaffold's topBar slot shows the TopAppBar and content respects padding.
lifecycle
intermediate
2: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")
    }
  }
}
AOnly once when the screen is first composed; clicking the button does not recompose TopAppBar.
BEvery time any UI element on the screen is clicked, regardless of state changes.
CWhen the 'Change Title' button is clicked and the title state changes, causing recomposition of TopAppBar.
DTopAppBar never recomposes because it is outside the content lambda.
Attempts:
2 left
💡 Hint
Think about what triggers recomposition in Compose with mutableStateOf.
navigation
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?
A
TopAppBar(
  title = { Text("Screen") },
  navigationIcon = {
    IconButton(onClick = { onBack() }) {
      Icon(Icons.Default.ArrowBack, contentDescription = "Back")
    }
  }
)
B
TopAppBar(
  title = { Text("Screen") },
  navIcon = {
    IconButton(onClick = { onBack() }) {
      Icon(Icons.Default.ArrowBack, contentDescription = "Back")
    }
  }
)
C
TopAppBar(
  title = { Text("Screen") },
  navigationIcon = IconButton(onClick = { onBack() }) {
    Icon(Icons.Default.ArrowBack, contentDescription = "Back")
  }
)
D
TopAppBar(
  title = { Text("Screen") },
  navigationIcon = {
    Icon(Icons.Default.ArrowBack, contentDescription = "Back")
  }
)
Attempts:
2 left
💡 Hint
The navigationIcon parameter expects a composable lambda with an IconButton wrapping the Icon.
📝 Syntax
advanced
2: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")
}
ASyntax error because of missing parentheses around Text composable.
BNo error; the code compiles and runs correctly showing the title and content.
CRuntime crash due to missing padding in content lambda.
DType mismatch error because title expects a composable lambda, not a Text composable directly.
Attempts:
2 left
💡 Hint
Check the expected type of the title parameter in TopAppBar.
🔧 Debug
expert
2: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")
}
AText composable must be wrapped in a Column to avoid overlap.
BThe content lambda does not apply the paddingValues parameter, so content overlaps the top bar.
CScaffold requires a bottomBar to prevent content overlap with topBar.
DTopAppBar is not visible because it is not inside the Scaffold content lambda.
Attempts:
2 left
💡 Hint
Scaffold provides paddingValues to content lambda to avoid overlapping system bars and topBar.