Sliding Bottom Sheet in Android using Jetpack Compose
Bottom Sheet is a material design component that slides up from the bottom of the screen to display additional content for the application. It is like a message that is invoked or appears upon user actions. A Bottom Sheet is widely used in modern applications. For example, a Bottom Sheet appears inside a game where there are in-app purchases to be made.
In this article, we will show you how you could create a Simple Sliding Bottom Sheet in Android using Jetpack Compose. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.geeksforgeeks.jcbottomsheetimport android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.layout.*import androidx.compose.material.*import androidx.compose.runtime.Composableimport androidx.compose.runtime.rememberCoroutineScopeimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport kotlinx.coroutines.launchclass MainActivity : ComponentActivity() { @ExperimentalMaterialApi override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { // Calling the composable function // to display element and its contents MainContent() } }}// Creating a composable// function to display Top Bar@ExperimentalMaterialApi@Composablefun MainContent() { Scaffold( topBar = { TopAppBar(title = { Text("GFG | Bottom Sheet", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) }, content = { MyContent() } )}// Creating a composable function to// create a Button and a Bottom Sheet// Calling this function as content// in the above function@ExperimentalMaterialApi@Composablefun MyContent(){ // Declaring a Boolean value to // store bottom sheet collapsed state val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(bottomSheetState = BottomSheetState(BottomSheetValue.Collapsed)) // Declaring Coroutine scope val coroutineScope = rememberCoroutineScope() // Creating a Bottom Sheet BottomSheetScaffold( scaffoldState = bottomSheetScaffoldState, sheetContent = { Box(Modifier.fillMaxWidth().height(200.dp).background(Color(0XFF0F9D58))) { Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally) { Text(text = "Hello Geek!", fontSize = 20.sp, color = Color.White) } } }, sheetPeekHeight = 0.dp ){} Column( Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally) { // Creating a button that changes // bottomSheetScaffoldState value upon click // when bottomSheetScaffoldState is collpased, // it expands and vice-versa Button(onClick = { coroutineScope.launch { if (bottomSheetScaffoldState.bottomSheetState.isCollapsed){ bottomSheetScaffoldState.bottomSheetState.expand() }else{ bottomSheetScaffoldState.bottomSheetState.collapse() } } }, colors = ButtonDefaults.buttonColors(backgroundColor = Color(0XFF0F9D58))) { Text(text = "Click Me", color = Color.White) } }}// For displaying preview in// the Android Studio IDE emulator@ExperimentalMaterialApi@Preview(showBackground = true)@Composablefun DefaultPreview() { MainContent()} |
Output:
You can see in the below video that the Bottom Sheet slides up when the Button is clicked.

Please Login to comment...