Scaffold helps you build a basic app screen layout easily. TopAppBar shows a bar at the top with a title and actions.
Scaffold and TopAppBar in Android Kotlin
Introduction
Syntax
Android Kotlin
Scaffold(
topBar = {
TopAppBar(
title = { Text("Title") },
navigationIcon = { /* optional icon */ },
actions = { /* optional buttons */ }
)
},
content = { paddingValues ->
// Your screen content here
}
)Scaffold arranges the screen with slots like topBar and content.
TopAppBar is placed inside topBar slot to show the app bar.
Examples
Android Kotlin
Scaffold(
topBar = {
TopAppBar(title = { Text("Home") })
},
content = { padding ->
Text("Welcome to the home screen", Modifier.padding(padding))
}
)Android Kotlin
Scaffold(
topBar = {
TopAppBar(
title = { Text("Profile") },
navigationIcon = {
IconButton(onClick = { /* go back */ }) {
Icon(Icons.Default.ArrowBack, contentDescription = "Back")
}
}
)
},
content = { padding ->
Text("User profile details", Modifier.padding(padding))
}
)Android Kotlin
Scaffold(
topBar = {
TopAppBar(
title = { Text("Settings") },
actions = {
IconButton(onClick = { /* open menu */ }) {
Icon(Icons.Default.MoreVert, contentDescription = "Menu")
}
}
)
},
content = { padding ->
Text("Settings options", Modifier.padding(padding))
}
)Sample App
This app shows a screen with a top bar titled "My App" and a greeting text below with proper padding.
Android Kotlin
import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.padding import androidx.compose.material3.* import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: android.os.Bundle?) { super.onCreate(savedInstanceState) setContent { MaterialTheme { MyApp() } } } } @Composable fun MyApp() { Scaffold( topBar = { TopAppBar( title = { Text("My App") } ) }, content = { paddingValues -> Text( text = "Hello, Scaffold and TopAppBar!", modifier = Modifier.padding(paddingValues).padding(16.dp) ) } ) } @Preview(showBackground = true) @Composable fun PreviewMyApp() { MaterialTheme { MyApp() } }
Important Notes
Always add padding from content parameter to avoid content under the app bar.
Use navigationIcon for back or menu buttons on the left side.
Use actions to add buttons on the right side of the app bar.
Summary
Scaffold helps create a basic screen layout with slots like topBar and content.
TopAppBar shows a bar at the top with a title and optional icons or buttons.
Use padding from content to keep content visible below the app bar.
Practice
1. What is the main purpose of the
Scaffold component in Android Jetpack Compose?easy
Solution
Step 1: Understand Scaffold's role
Scaffold is designed to create a basic app screen layout with predefined slots such as topBar, bottomBar, and content.Step 2: Compare with other options
Network requests, database operations, and animations are handled by other components, not Scaffold.Final Answer:
To provide a basic screen layout with slots like top bar and content area -> Option AQuick Check:
Scaffold = Basic layout structure [OK]
Hint: Remember Scaffold arranges screen parts like a frame [OK]
Common Mistakes:
- Confusing Scaffold with data or network handling
- Thinking Scaffold manages animations
- Assuming Scaffold is for database tasks
2. Which of the following is the correct way to add a
TopAppBar inside a Scaffold in Jetpack Compose?easy
Solution
Step 1: Check Scaffold's topBar slot usage
The topBar parameter expects a composable lambda, so it should be passed as topBar = { ... }.Step 2: Validate TopAppBar syntax
TopAppBar requires a title composable, so title = { Text("Title") } is correct.Final Answer:
Scaffold(topBar = { TopAppBar(title = { Text("Title") }) }) -> Option BQuick Check:
topBar = { TopAppBar(title = { Text() }) } [OK]
Hint: Use curly braces for composable lambdas in Scaffold slots [OK]
Common Mistakes:
- Passing title as a string instead of a composable
- Putting TopAppBar inside content or bottomBar slots
- Omitting curly braces for topBar parameter
3. What will be displayed when running this code snippet?
Scaffold(
topBar = { TopAppBar(title = { Text("Hello") }) },
content = { padding ->
Text("Content", Modifier.padding(padding))
}
)medium
Solution
Step 1: Analyze Scaffold usage
The Scaffold sets a TopAppBar with title 'Hello' and content that uses the padding provided by Scaffold.Step 2: Understand padding effect
The content Text uses Modifier.padding(padding) which ensures it is placed below the TopAppBar without overlap.Final Answer:
A screen with a top bar titled 'Hello' and text 'Content' below it with proper padding -> Option AQuick Check:
TopAppBar + padded content = visible, no overlap [OK]
Hint: Content padding avoids overlap with TopAppBar [OK]
Common Mistakes:
- Ignoring padding causes content overlap
- Assuming content replaces top bar
- Thinking padding parameter is optional and unused
4. Identify the error in this Scaffold usage:
Scaffold(
topBar = TopAppBar(title = { Text("App") }),
content = { padding ->
Text("Welcome", Modifier.padding(padding))
}
)medium
Solution
Step 1: Check topBar parameter type
topBar expects a composable lambda, so it must be passed as topBar = { TopAppBar(...) }.Step 2: Verify content usage
The content lambda correctly uses the padding parameter to prevent overlap with the top bar.Final Answer:
topBar should be a lambda: topBar = { TopAppBar(...) } -> Option CQuick Check:
topBar = { ... } required for composables [OK]
Hint: Always wrap topBar content in curly braces { } [OK]
Common Mistakes:
- Passing composable directly without lambda
- Ignoring padding parameter in content
- Misunderstanding title parameter type
5. You want to create a screen with a
TopAppBar that has a title and an action icon button on the right. Which of the following code snippets correctly implements this inside a Scaffold?hard
Solution
Step 1: Confirm topBar lambda syntax
topBar must be a lambda: topBar = { ... } wrapping TopAppBar composable.Step 2: Check TopAppBar parameters
Title must be a composable lambda: title = { Text("Home") } and actions is a lambda with IconButton inside.Step 3: Validate IconButton usage
IconButton requires onClick lambda and Icon with contentDescription for accessibility.Final Answer:
Scaffold(topBar = { TopAppBar(title = { Text("Home") }, actions = { IconButton(onClick = {}) { Icon(Icons.Default.Favorite, contentDescription = "Favorite") } }) }) -> Option DQuick Check:
topBar and actions use lambdas with composables [OK]
Hint: Use lambdas for title and actions in TopAppBar [OK]
Common Mistakes:
- Passing title or actions as direct values, not lambdas
- Omitting contentDescription in Icon
- Not wrapping topBar in a lambda
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
