Better Programming

Advice for programmers.

Follow publication

Implement Bottom Sheet in Jetpack Compose

A short guide for developers

Igor Stevanovic
Better Programming
Published in
3 min readAug 24, 2022

The Jetpack Compose logo used in this image is the official logo created by Google

Jetpack Compose brings us so many good things. Working on UI is just a pleasure compared to an XML. One of the things it brings us is the out-of-the-box way to implement bottom sheets. No need to include any dependency. Everything is already there.

“Modal bottom sheets present a set of choices while blocking interaction with the rest of the screen. They are an alternative to inline menus and simple dialogs, providing additional room for content, iconography, and actions.” According to the official documentation

We are gonna use ModalBottomSheetLayout which has its own state that we need to remember. So let’s get on with it.

First, we are gonna create sheetState:

There are three ModalBottomSheetValues:

  • Hidden
  • Expanded
  • HalfExpanded

The first two are describing enough. HalfExpanded will only show half of the bottom sheet, and this state is enabled only if the height of the bottom sheet is more than 50% of the screen height.

confirmStateChange is an optional callback invoked to confirm or veto a pending state change. With a value like that, we are blocking HalfExpanded bottom sheet value.

Don’t forget to add @OptIn(ExperimentalMaterialApi::class) as this is all still experimental.

Next, we can now implement ModalBottomSheetLayout and inside its content put the whole screen. Our screen will look like this:

As we can see, we put our screen composables inside the ModalBottomSheetLayout’s content. Also, we pass sheetState as an argument.
BackHandler is an effect for handling presses of the system back button. The first argument is when it is enabled, and the second is what it should do when the system back button is pressed.

In this case, we just want to hide the bottom sheet if it is shown and the system back button is pressed.

Button inside the Column is showing and hiding the bottom sheet. sheetState.show() will show the bottom sheet with animation and sheetState.hide() will hide the bottom sheet with animation.

And the last thing that we need to see here is BottomSheet composable:

Which is a really simple column filled with some text. You can put here whatever you want.

If you want to have some buttons that do something, you will just communicate via callbacks with the main composable.

This is pretty simple logic but very useful to know. One note here, the logic for showing and hiding the bottom sheet is put in the UI, but it should be handled by the ViewModel. That is a little bit out of the scope of this article, as I just wanted to show how you can create a bottom sheet.

That would be all for this article. I hope you liked it!

All of the source code you can find in my GitHub repo.

Want to Connect?GitHub
LinkedIn
Twitter
Portfolio website

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Igor Stevanovic
Igor Stevanovic

Written by Igor Stevanovic

Android Engineer, Freelancer and Writer

Responses (1)

Write a response