Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to Add a Snackbar to Jetpack Compose
Aniket Kadam
Aniket Kadam

Posted on • Edited on

     

How to Add a Snackbar to Jetpack Compose

Snackbars are now the default way to show transitory information in Android. They replaced Toasts a while ago and for good reason!

If you're using Jetpack Compose, it's a bit of a procedure to use them though. Here's how you do it.

If you need a quick refresher, here's the answer. Don't worry if it doesn't make sense, I'll explain all the components by the end of the article.

snackbarCoroutineScope.launch{scaffoldState.snackbarHostState.showSnackbar("Snacks in Compose")}
Enter fullscreen modeExit fullscreen mode

Note: This is valid as of Jetpack Compose Beta 3 (1.0.0-beta03)

Starting With a Button

We'll start off with a Compose UI that has a button which prints a log and then instead of a log, we'll get to a point where it can show a Snackbar.

Here's that button printing a log.

@ComposablefunMainScreenUI(){Button(onClick={Log.d("tag","Button Clicked")}){Text("Click Me")}}
Enter fullscreen modeExit fullscreen mode

For a conventional Snackbar, you need a Scaffold. A scaffold is a container that makes it easy to add all kinds of standard UI like top bars, bottom bars and even places the Snackbar conveniently for you. The 'state' of the Snackbar is held within the state of the Scaffold.

I'm guessing you already have some UI in a Compose function so just add a Scaffold to the top.

Halfway There, Adding the Scaffold

@ComposablefunMainScreenUI(){Scaffold{Button(onClick={Log.d("tag","Button Clicked")}){Text("Click Me")}}}
Enter fullscreen modeExit fullscreen mode

Now we need two things:

  1. AScaffoldState so we can get at the SnackbarState within it.
  2. Snackbars are generally animated and do some background work to appear so we also need a CoroutineScope to run their background work in.

Personally I think it's a bit strange that we've got to handle the background animation of the Snackbar ourselves but that's the api.

Getting To the SnackbarState

First we'll add the ScaffoldState

@ComposablefunMainScreenUI(){valscaffoldState=rememberScaffoldState()Scaffold(scaffoldState=scaffoldState){Button(onClick={Log.d("tag","Button Clicked")}){Text("Click Me")}}}
Enter fullscreen modeExit fullscreen mode

Note: We'll be using 'remember' which wraps a state object for the ScaffoldState into a value that persists after recompositions. If you want more details on that, ask me onTwitter I'll be happy to answer.

Now we'll create a CoroutineScope for the SnackBar and launch it, in place of the log!

The Complete Code

@ComposablefunMainScreenUI(){valscaffoldState=rememberScaffoldState()valsnackbarCoroutineScope=rememberCoroutineScope()Scaffold(scaffoldState=scaffoldState){Button(onClick={snackbarCoroutineScope.launch{scaffoldState.snackbarHostState.showSnackbar("Button Clicked")}}){Text("Click Me")}}}
Enter fullscreen modeExit fullscreen mode

As you can see, finally it all comes down the line:

snackbarCoroutineScope.launch{scaffoldState.snackbarHostState.showSnackbar("Button Clicked")}
Enter fullscreen modeExit fullscreen mode

The SnackbarState is within the ScaffoldState we retrieved. The new CoroutineScope we created is used to launch the snackbar.

Easy as pie! Once you know what's going on that is.


I'm on the lookout for a Senior Android position with great customer impact, a great team and great compensation. Let me know if you're hiring! Also open to consulting contracts.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

An android dev with ~8 years of experience. I consult and may join fulltime for the right company.
  • Location
    India
  • Work
    Android Developer
  • Joined

More fromAniket Kadam

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp