Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use setDuration() method in SnackBar (Android Design Support Library)

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.

like image 567
tehnolog Avatar asked May 30 '15 20:05

tehnolog


People also ask

How do I set duration on snackbar?

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.

How do I use 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 layout is used for snack bar undo?

Creating A Snackbar With An Action Button In Android Studio: make(coordinatorLayout, "Message is deleted", snackbar. LENGTH_LONG) . setAction("UNDO", new View.

How do you make a kotlin snack bar?

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.


2 Answers

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

like image 179
CommonsWare Avatar answered Sep 23 '22 10:09

CommonsWare


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(); 
like image 29
Jimmy Kamau Avatar answered Sep 25 '22 10:09

Jimmy Kamau