How to use Snackbar in Android

Snackbar in Android
Snackbar in Android

The Snackbar is a view that provides brief feedback about app processes at the bottom of the mobile screen. It appears above all elements on the screen, indicating the user to perform that operation first. It supports content labeling for approachability and is understandable by most screen readers.

Snackbar is just like the Toast message but in case of snackbar user can interact with it. It can offer actions like “OKAY”, “DISMISS”, “RETRY”, “UNDO”, “REDO”, etc. It automatically disappears after a timeout or after user interaction.

Note: Snackbars work properly if they are held inside of a CoordinatorLayout. CoordinatorLayout allows the snackbar to enable performance like swipe-to-dismiss, as well as automatically moving widgets like FloatingActionButton

Snackbar vs. Toast

  • Snackbar appears above all elements on the screen, unlike toast that may block any significant part of the screen.
  • Snackbar performs actions like undo, redo, retry, etc. But toast not.
  • Users can easily dismiss the snackbar, but in the case of toast, users need to wait until the end of the time scheduled for it.

Showing the snackbar

To create a snackbar, calling make creates a snackbar. Creating and showing are two different things. To show a snackbar, use the show method on the return Snackbar instance.

To show snackbar with a message and no action:

  • The view is used to make a snackbar.
  • Generally, it viewed that interacted with a trigger.
  • Let’s say the trigger is a button.
make and show methods

Adding an action

To add action, use the setAction method on that object return from make.

Sanckbar is automatically closed when the action is performed.

Snackbar with action:

snackbar with setAction method
snackbar with setAction method
snackbar with setAction method

Anchoring the snackbar

By default, the snackbar is anchored at the bottom of their parent’s view. But we can use setAnchorView method to appear the snackbar above a specific widget within that layout. For example, a FloatingActionButton.

setAnchorView in snackbar
setAnchorView in snackbar
setAnchorView in snackbar

Using a snackbar in Android Studio

To create a snackbar in the android studio, you just need a library. For that, we used a Material Design Library.

Add the library dependency into our build.gradle (app) file:

material library

As we are using material design, so we need to change some styling of the layout. For that go to res/values/style.xml file and change the theme to Theme.MaterialComponents.Light.NoActionBar

style.xml file

Creating the snackbar in the android studio (XML file)

first, create the view of the snackbar. And also add a Floating ActionButton to show how the Anchoring things worked.


Creating the snackbar in the android studio (Java file)

Open the main activity and add the below code.

Here we make the snackbar, set the timeout, set the anchor view, set the action, set the animation, and then display the snackbar on the screen


Download code

Thank you. Hope you like it.

Hi everyone, myself Golap an Android app developer with UI/UX designer.