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?
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.
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.
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.
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.
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With