Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Support Library Snackbar with indefinite length

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?

like image 646
Simon Avatar asked Jun 27 '15 16:06

Simon


People also ask

How can I increase my snackbar duration?

You can change the duration which a SnackBar is displayed by assigning a required duration to the backgroundColor property of SnackBar, as a Duration object.

How long is snackbar long?

Snackbars appear without warning, and don't require user interaction. They automatically disappear from the screen after a minimum of four seconds, and a maximum of ten seconds.

What is an android snackbar?

Snackbar in android is a new widget introduced with the Material Design library as a replacement of a Toast. Android Snackbar is light-weight widget and they are used to show messages in the bottom of the application with swiping enabled. Snackbar android widget may contain an optional action button.

What snackbar methods must be called to create a snackbar with a button?

In the onClickListener a Snackbar is created and is called. So whenever the button is clicked, the onClickListener creates a snackbar and calls it and the user sees the message. This snackbar contains an action and if clicked will show a toast.


2 Answers

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(); 
like image 177
Dan Avatar answered Sep 20 '22 16:09

Dan


UPDATE: As mentioned this is now possible with the release of Android support library 22.2.1, use the LENGTH_INDEFINITE flag

It is not possible to set an indefinite display of a Snackbar when using the official implementation from the Android Design Support library.

While doing this may violate the Material Design philosophy of a Snackbar, there are 3rd party Snackbar implementations that do allow this. Here is an example:

https://github.com/nispok/snackbar

This project allows the following values for duration of display:

LENGTH_SHORT: 2s LENGTH_LONG: 3.5s (default) LENGTH_INDEFINTE: Indefinite; ideal for persistent errors 

Beware that this project is no longer being developed due to the release of the official Snackbar implementation.

like image 43
BrentM Avatar answered Sep 21 '22 16:09

BrentM