Scaffold in Android Jetpack is a composable function that provides a basic structure of the layout. It holds together different parts of the UI elements such as Application bars, floating action buttons, etc.
There are a lot of apps that contain TopAppBar, Drawer, Floating Action Button, BottomAppBar (in the form of bottom navigation), Snackbar. While you can individually set up all of these in an app it takes a lot of setups. Jetpack Compose provides Scaffold Composable which can save a lot of time. It's like a prebuilt template. In this article, we will see how to set up a Scaffold in Android with Jetpack Compose. We will be building a basic app that will demonstrate the Scaffold composable, here is a video showing the app.
Prerequisites
- Knowledge of Kotlin.
- Knowledge of Jetpack Compose.
Step by Step Implementation
Step 1 : Create a New Project
To create a new project in the Android Studio, please refer to How to Create a new Project in Android Studio with Jetpack Compose.
Note: Select Kotlin as the programming language.
Step 2 : Working with MainActivity.kt
We will be the following items in the Scaffold topBar, floatingActionButton, bottomBar and content. Let's explain how:
- Scaffold Layout: The Scaffold composable is used to organize the UI into standard sections like TopAppBar, BottomAppBar, FloatingActionButton, and main content (Box), ensuring consistent layout.
- TopAppBar: Displays a title ("Top Bar") at the top, with the background color set using MaterialTheme.colorScheme.secondary.
- FloatingActionButton: Shows a floating action button with an "Add" icon, which triggers an action when clicked (currently a placeholder).
- Main Content: Inside a Box composable, the main body contains centered text ("This is the Main Body of the Application") and respects the padding provided by the Scaffold.
- Preview: A @Preview composable is included to render the MyApp layout in Android Studio, applying the same theme as the main app.
MainActivity.kt:
package com.geeksforgeeks.demo
import android.os.Bundle
import android.widget.Toast
import androidx.activity.*
import androidx.activity.compose.*
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.*
import androidx.compose.ui.graphics.*
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import com.geeksforgeeks.demo.ui.theme.DemoTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
DemoTheme(dynamicColor = false, darkTheme = false) {
Surface(color = Color.White) {
MyApp()
}
}
}
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyApp() {
val context = LocalContext.current
Scaffold(
// top app bar
topBar = {
TopAppBar(
title = { Text("Top Bar") },
colors = TopAppBarDefaults.topAppBarColors(MaterialTheme.colorScheme.secondary)
)
},
// floating button
floatingActionButton = {
FloatingActionButton(
containerColor = MaterialTheme.colorScheme.primary,
onClick = {
Toast.makeText(context, "Button clicked", Toast.LENGTH_SHORT).show()
}
) {
Icon(Icons.Filled.Add, contentDescription = "Add")
}
},
// main surface
content = { paddingValues ->
Box(
modifier = Modifier
.padding(paddingValues)
.fillMaxSize()
) {
Text("Content", modifier = Modifier.align(Alignment.Center))
}
},
// bottom app bar
bottomBar = {
BottomAppBar {
Text(
text = "Bottom Bar",
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center
)
}
}
)
}