Android Support Library Snackbar with indefinite length

Simon picture Simon · Jun 27, 2015 · Viewed 20.4k times · Source

I see that the Snackbar will only take either LENGTH_LONG or LENGTH_SHORT when determining the length of its display on screen.

I would like to have it displayed until someone swipes it off the screen. This is for some cases when you have persistent errors, like when you have no internet and you want to notify the user without having it disappearing off the screen after 2750ms when selecting LENGTH_LONG.

Of course I can use setDuration to a ridiculously long milliseconds values, but is there no way to just set it up so that it doesn't disappears until the user dismisses it?

Answer

Dan picture Dan · Jul 18, 2015

The latest version of the Android Support Library (22.2.1), now includes LENGTH_INDEFINITE.

The following will show the Snackbar until it is dismissed or another Snackbar is shown.

Snackbar.make(view, "Your Snackbar", Snackbar.LENGTH_INDEFINITE)
        .setAction("Your Action", null).show();