From Documentation: parameter duration - either be one of the predefined lengths: LENGTH_SHORT, LENGTH_LONG, or a custom duration in milliseconds. But I can't set custom duration.
For example
Snackbar .make(parentLayout, "Feed cat?", 8000) // try here .setAction("Yes", snackOnClickListener) .setActionTextColor(Color.MAGENTA) .setDuration(8000) // try here .show();
but instead of 8 seconds Snackbar gone quickly.
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. And you can set the duration of your snackbar by setDuration(int) method.
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.
Creating A Snackbar With An Action Button In Android Studio: make(coordinatorLayout, "Message is deleted", snackbar. LENGTH_LONG) . setAction("UNDO", new View.
This example demonstrates how to use Snackbar in Android Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Based on the implementation of Snackbar
and SnackbarManager
, I can confirm Eugene H's assessment: it's a bug. From SnackbarManager
:
private void scheduleTimeoutLocked(SnackbarRecord r) { mHandler.removeCallbacksAndMessages(r); mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_TIMEOUT, r), r.duration == Snackbar.LENGTH_LONG ? LONG_DURATION_MS : SHORT_DURATION_MS); }
So, any value that is not LENGTH_LONG
results in a short-duration snackbar.
I have filed an issue about it.
Edit: Has been fixed in revision 22.2.1. Check the release notes here
The android docs have NOT been updated yet, but if you jump into the source code you'll notice that the parameter to the method setDuration(int duration) can either be one of LENGTH_SHORT, LENGTH_LONG, LENGTH_INDEFINITE or a custom duration in milliseconds
Set the initial duration to LENGTH_INDEFINITE then set your custom duration afterwards:
Snackbar .make(parentLayout, "Feed cat?", Snackbar.LENGTH_INDEFINITE) .setAction("Yes", snackOnClickListener) .setActionTextColor(Color.MAGENTA) .setDuration(8000) .show();
EDIT
Setting a period directly in milliseconds now works;
Snackbar .make(parentLayout, "Feed cat?", 8000) .setAction("Yes", snackOnClickListener) .setActionTextColor(Color.MAGENTA) .show();
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