Getting started with jetpack compose – Scaffold layout

A Scaffold is a layout that implements the basic material design layout structure. You can add things like a TopBar, BottomBar, FAB, or a Drawer. Jetpack Compose provides Scaffold Composable which can save a lot of time. It’s like a prebuilt template.


If you’re new to Compose, I highly recommend going through the following articles:

Getting started with jetpack compose – Basic components

Getting started with jetpack compose – Layouts

Getting started with jetpack compose – Modifiers

Getting started with jetpack compose – ConstraintLayout

Getting started with jetpack compose – Theming


looks at my sample scaffold layout,

setContent { JetpackComposeScaffoldLayoutTheme { // A surface container using the 'background' color from the theme Surface(color = MaterialTheme.colors.background) { var toolbarTitle by remember { mutableStateOf("Home") } val scaffoldState = rememberScaffoldState(rememberDrawerState(initialValue = DrawerValue.Closed)) val scope = rememberCoroutineScope() Scaffold( modifier = Modifier.background(Color.White), scaffoldState = scaffoldState, topBar = { AppToolbar( scaffoldState = scaffoldState, scope = scope, toolbarTitle = toolbarTitle ) }, drawerContent = { DrawerView(scaffoldState = scaffoldState, scope = scope) }, content = {}, bottomBar = { AppBottomBar() }, snackbarHost = { state -> MySnackHost(state) }, isFloatingActionButtonDocked = true, floatingActionButtonPosition = FabPosition.Center, floatingActionButton = { MyFloatingButton(scaffoldState) } ) } } }
Code language: PHP (php)

and the output of this code,

scaffold layout demo

Looks very simple. let’s see all the composable in detail,

Modifier

A modifier is a way to modify certain aspects of the component that result in how it’s displayed on the screen.

I explained modifiers in another post. Please check it.

Getting started with jetpack compose – Modifiers

scaffoldState

It maintains the state of the scaffold composable. It contains drawerState and snackbarHostState to perform operations like closing and opening the drawer. Also, show and hide the snackbar.

class ScaffoldState( val drawerState: DrawerState, val snackbarHostState: SnackbarHostState )

set the scaffoldState in scaffold composable.

val scaffoldState = rememberScaffoldState(rememberDrawerState(initialValue = DrawerValue.Closed)) Scaffold(modifier = Modifier.background(Color.White), scaffoldState = scaffoldState, ... }

rememberCoroutineScope

As LaunchedEffect is a composable function, it can only be used inside other composable functions. In order to launch a coroutine outside of a composable, but scoped so that it will be automatically canceled once it leaves the composition, use rememberCoroutineScope.

rememberCoroutineScope is a composable function that returns a CoroutineScope bound to the point of the Composition where it’s called. The scope will be canceled when the call leaves the Composition.

For example, to display s snackbar we need to use remeberCoroutineScope,

val scope = rememberCoroutineScope() FloatingActionButton(onClick = { scope.launch { when (scaffoldState.snackbarHostState.showSnackbar( message = "Jetpack Compose Snackbar", actionLabel = "Ok" )) { SnackbarResult.Dismissed -> Log.d("TAB", "Dismissed") SnackbarResult.ActionPerformed -> Log.d("TAB", "Action!") } } }) { Text("X") }
Code language: JavaScript (javascript)

TopAppBar

It’s a simple composable function to design the top bar of the app.

Let’s see the function of the TopAppBar:

@Composable<br>fun TopAppBar(<br> title: @Composable () -> Unit,<br> modifier: Modifier = Modifier,<br> navigationIcon: @Composable (() -> Unit)? = null,<br> actions: @Composable RowScope.() -> Unit = <strong>{}</strong>,<br> backgroundColor: Color = MaterialTheme.colors.<em>primarySurface</em>,<br> contentColor: Color = <em>contentColorFor</em>(backgroundColor),<br> elevation: Dp = AppBarDefaults.TopAppBarElevation<br>)
Code language: HTML, XML (xml)

Parameters

title – provide the title text for the TopAppBar.

title = <strong>{ </strong><em>Text</em>(text = "Home") <strong>}</strong>
Code language: HTML, XML (xml)

Modifier – Used to modify the background of the TopAppBar.

navigationIcon – Used to set the Navigation Icon like the Drawer menu and handle the functions like opening the drawer by clicking the menu icon.

navigationIcon = <strong>{<br></strong><strong> </strong><em>Icon</em>(<br> painter = <em>painterResource</em>(id = R.drawable.<em>ic_baseline_menu_24</em>),<br> contentDescription = "Menu", modifier = Modifier.<em>clickable </em><strong>{<br></strong><strong> </strong>scope.<em>launch </em><strong>{<br></strong><strong> </strong>scaffoldState.drawerState.open()<br> <strong>}<br></strong><strong> }<br></strong><strong> </strong>)<br><strong>}</strong>
Code language: HTML, XML (xml)

actions – The actions displayed at the end of the TopAppBar. This should typically be IconButtons. The default layout here is a Row, so icons inside will be placed horizontally.

actions = <strong>{<br></strong><strong> </strong>val context = <em>LocalContext</em>.current<br> <em>Icon</em>(<br> painter = <em>painterResource</em>(id = R.drawable.<em>ic_baseline_settings_24</em>),<br> contentDescription = "Setting", modifier = Modifier.<em>clickable </em><strong>{<br></strong><strong> </strong>Toast.makeText(context, "Open Setting", Toast.<em>LENGTH_SHORT</em>).show()<br> <strong>}<br></strong><strong> </strong>)<br><strong>}</strong>
Code language: HTML, XML (xml)

backgroundColor – Set the background color for the TopApBar.

elevation – the elevation of this TopAppBar.

This is the code for the sample I have created for the TopAppBar.

@Composable fun AppToolbar(scaffoldState: ScaffoldState, scope: CoroutineScope, toolbarTitle: String) { TopAppBar( title = { Text(text = toolbarTitle) }, modifier = Modifier.background(Color(0xFF008800)), navigationIcon = { Icon( painter = painterResource(id = R.drawable.ic_baseline_menu_24), contentDescription = "Menu", modifier = Modifier.clickable { scope.launch { scaffoldState.drawerState.open() } } ) }, actions = { val context = LocalContext.current Icon( painter = painterResource(id = R.drawable.ic_baseline_settings_24), contentDescription = "Setting", modifier = Modifier.clickable { Toast.makeText(context, "Open Setting", Toast.LENGTH_SHORT).show() } ) } ) }
Code language: JavaScript (javascript)
TopAppBar

The Drawer Content

In the drawerContent, we need to provide the content for the Drawer. content of the Drawer sheet that can be pulled from the left side.

Let’s check out my sample drawer composable.

@Composable fun DrawerContent(scaffoldState: ScaffoldState, scope: CoroutineScope) { Column(modifier = Modifier.fillMaxWidth()) { Image( painter = painterResource(id = R.drawable.ic_baseline_face_24), modifier = Modifier .size(100.dp) .align(Alignment.CenterHorizontally), contentDescription = "Face" ) Spacer(modifier = Modifier.padding(4.dp)) Text( text = "Velmurugan", style = MaterialTheme.typography.subtitle1, modifier = Modifier.align( Alignment.CenterHorizontally ) ) Spacer(modifier = Modifier.padding(4.dp)) Text(text = "User1", Modifier .fillMaxWidth() .background(Color.Gray) .padding(8.dp, 4.dp) .clickable { scope.launch { scaffoldState.drawerState.close() } }) Spacer(modifier = Modifier.padding(4.dp)) } }
Code language: JavaScript (javascript)
Drawer layout

By using the scaffoldState we can show and close the drawer content.

scaffoldState.drawerState.open() – display the drawer

scaffoldState.drawerState.close() – close the drawer

BottomAppBar

The bottomAppBar is used to display the menu at bottom of the screen same as the bottom.

To create bottomAppBar, First, we need to create Menu items for the Bottom Navigation.

data class MenuItem(val title: String, val icon: ImageVector) fun getMenuList() : List<MenuItem> { val menuItems = mutableListOf<MenuItem>() menuItems.add(MenuItem(Navigations.HOME.name, Icons.Filled.Home)) menuItems.add(MenuItem(Navigations.SEARCH.name, Icons.Filled.Search)) menuItems.add(MenuItem(Navigations.FAVORITE.name, Icons.Filled.Favorite)) menuItems.add(MenuItem(Navigations.SETTINGS.name, Icons.Filled.Settings)) return menuItems } val bottomMenuList = getMenuList()
Code language: HTML, XML (xml)

Next, we need to create BottomNavigation composable to create a Menu item using the BottomNavigationItem.

@Composable<br>fun RowScope.BottomNavigationItem(<br> selected: Boolean,<br> onClick: () -> Unit,<br> icon: @Composable () -> Unit,<br> modifier: Modifier = Modifier,<br> enabled: Boolean = true,<br> label: @Composable (() -> Unit)? = null,<br> alwaysShowLabel: Boolean = true,<br> interactionSource: MutableInteractionSource = <em>remember </em><strong>{ </strong><em>MutableInteractionSource</em>() <strong>}</strong>,<br> selectedContentColor: Color = <em>LocalContentColor</em>.current,<br> unselectedContentColor: Color = selectedContentColor.copy(alpha = ContentAlpha.medium)<br>)
Code language: HTML, XML (xml)

Parameters

selected – set the select state for the bottom navigation menu item.

onClick() – set the click listener for the menu item click.

icon:@Composable() – set the icon for the bottom navigation menu item.

icon = <strong>{ </strong><em>Icon</em>( imageVector = bottomMenu.icon, contentDescription = bottomMenu.title ) <strong>}</strong>
Code language: HTML, XML (xml)

label – add the label for the menu item.

alwaysShowLabel: Boolean – show and hide the menu item label.

selectedContentColor: Color – content color for the selected menu item

unselectedContentColor: Color – content color for the unselected menu item.

Let’s see the example BottomAppBar,

@Composable fun BottomAppBar() { data class MenuItem(val title: String, val icon: ImageVector) fun getMenuList() : List<MenuItem> { val menuItems = mutableListOf<MenuItem>() menuItems.add(MenuItem(Navigations.HOME.name, Icons.Filled.Home)) menuItems.add(MenuItem(Navigations.SEARCH.name, Icons.Filled.Search)) menuItems.add(MenuItem(Navigations.FAVORITE.name, Icons.Filled.Favorite)) menuItems.add(MenuItem(Navigations.SETTINGS.name, Icons.Filled.Settings)) return menuItems } val bottomMenuList = getMenuList() BottomNavigation { bottomMenuList.forEach { bottomMenu -> BottomNavigationItem( selected = bottomMenu.title == "Home", onClick = { }, icon = { Icon( imageVector = bottomMenu.icon, contentDescription = bottomMenu.title ) }) } } }
Code language: HTML, XML (xml)
BottomAppBar design

FloatingActionButton

we can create FAB using the FloatingActionButton composable function.

@OptIn(ExperimentalMaterialApi::class)<br>@Composable<br>fun FloatingActionButton(<br> onClick: () -> Unit,<br> modifier: Modifier = Modifier,<br> interactionSource: MutableInteractionSource = <em>remember </em><strong>{ </strong><em>MutableInteractionSource</em>() <strong>}</strong>,<br> shape: Shape = MaterialTheme.shapes.small.copy(<em>CornerSize</em>(percent = 50)),<br> backgroundColor: Color = MaterialTheme.colors.secondary,<br> contentColor: Color = <em>contentColorFor</em>(backgroundColor),<br> elevation: FloatingActionButtonElevation = FloatingActionButtonDefaults.elevation(),<br> content: @Composable () -> Unit<br>)
Code language: HTML, XML (xml)

Parameters

onClick() – set click listener for the FAB.

modifier – add view modification for the FAB.

shape = MaterialTheme.shapes.small.copy(CornerSize(percent = 50)) – Provide the shape for the FAB.

backgroundColor – set the background color for the FAB.

contentColor – set the content color on the FAB.

content – provide the content composable for the FAB.

Let’s see the sample FAB,

@OptIn(ExperimentalMaterialApi::class) @Composable fun FloatingActionButtonSample() { FloatingActionButton(onClick = { }) { Text("+") } }
Code language: JavaScript (javascript)
FAB design

FloatingActionButtonPosition

When you have added a FAB, you can use FloatingActionButtonPosition to specify its position. The default position is at the end of your layout.

Now, I changed the position to the center using,

floatingActionButtonPosition = FabPosition.Center
BottomAppBar with FAB in center

Snackbar

By default, a Snackbar is provided, and we can just call it to show without setting it up. However, if we want to customize it, we can still set it up and customize it as below.

@OptIn(ExperimentalMaterialApi::class) @Composable fun MySnackHost(state: SnackbarHostState) { SnackbarHost( state, snackbar = { data -> Snackbar( modifier = Modifier .padding(8.dp) .background(Color.Black, RoundedCornerShape(8.dp)), action = { Text( text = "HIDE", modifier = Modifier .padding(8.dp) .clickable { state.currentSnackbarData?.dismiss() }, style = androidx.compose.ui.text.TextStyle( fontWeight = FontWeight.Bold, color = MaterialTheme.colors.primary, fontSize = 18.sp ) ) } ) { Text(text = data.message) } }) }
Code language: PHP (php)

Now we can set the customized snackbar into the scaffold.

Scaffold( modifier = Modifier.background(Color.White), scaffoldState = scaffoldState, topBar = { }, drawerContent = { }, content = {}, bottomBar = { BottomAppBar() }, snackbarHost = { state -> MySnackHost(state) }, isFloatingActionButtonDocked = true, floatingActionButtonPosition = FabPosition.Center, floatingActionButton = { FloatingActionButtonSample() } )
Code language: JavaScript (javascript)

Now, we set the custom snackbar into the scaffold layout.

The next step is to display the snackbar using the scaffoldState.

whenever we need to show the snackbar, we need to pass the message and the click action.

I have created a FAB composable, a click of the FAB, I am displaying the snackbar. please check the code below,

@OptIn(ExperimentalMaterialApi::class) @Composable fun FloatingActionButtonSample(scaffoldState: ScaffoldState) { val scope = rememberCoroutineScope() FloatingActionButton(onClick = { scope.launch { when (scaffoldState.snackbarHostState.showSnackbar( message = "Snackbar", actionLabel = "Ok" )) { SnackbarResult.Dismissed -> Log.d("TAB", "Dismissed") SnackbarResult.ActionPerformed -> Log.d("TAB", "Action!") } } }) { Text("X") } }
Code language: PHP (php)
Snackbar demo

That’s it. Thanks for reading.

You can download this example on GitHub.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *