Working with Snackbar in Android

Snackbars are just like Toast messages except they provide action to interact with. Snackbar will be displayed at the bottom of the screen and can be swiped
off in order to dismiss them.

Simple Snackbar

Below is the syntax of a simple snackbar. The make function accepts three parameters. View, display message and duration of the message to be displayed.

Normally passing CoordinatorLayout as view param is the best option as it allows Snackbar to enable some features like swipe-to-dismiss and automatically moving of widgets
like FloatingActionButton.

And the duration should be LENGTH_SHORT, LENGTH_LONG or LENGTH_INDEFINITE. When LENGTH_INDEFINITE is used, the snackbar will be displayed indefinite time and can be dismissed with swipe off.

Snackbar snackbar = Snackbar.make(coordinatorLayout, "Welcome to Scorpion_fun_World", Snackbar.LENGTH_LONG);;

Snackbar with Action Callback

You can also mention a callback interaction method using setAction() method. This allows us to take certain action when user interacts with the snackbar.

Snackbar snackbar = Snackbar
        .make(coordinatorLayout, "Message is deleted", Snackbar.LENGTH_LONG)
        .setAction("UNDO", new View.OnClickListener() {
            public void onClick(View view) {
                Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message is restored!", Snackbar.LENGTH_SHORT);

Customizing the Snackbar View

Snackbar comes with default white color text and #323232 background color. You can override these colors as mentioned below.

Snackbar snackbar = Snackbar
        .make(coordinatorLayout, "No internet connection!", Snackbar.LENGTH_LONG)
        .setAction("RETRY", new View.OnClickListener() {
            public void onClick(View view) {

// Changing message text color

// Changing action button text color
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(;

Here’s how the end result looks.


Software Engineer(Android) at Bennett Coleman and Co. Ltd. (Times Group)

This content has been helpful to you?

Thanks for contributing!

Yes No