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

AndroidAndroid Design-LibrarySnackbar

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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();

Solution 3 - Android

Since 'com.android.support:design:22.2.1'

you can set the duration of your Snackbar to LENGTH_INDEFINITEit will make the Snackbar shown until it is dismissed or another snackbar is shown.

Solution 4 - Android

This code working perfectly for me try this

Snackbar.make(view, "Hello SnackBar", Snackbar.LENGTH_LONG)
        .setAction("Its Roy", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        })
        .setDuration(10000)
        .setActionTextColor(getResources().getColor(R.color.colorAccent))
        .show();

Solution 5 - Android

It seems to be fixed in

compile 'com.android.support:design:22.2.1'

Only Lint shows it red underlined, but it works.

Solution 6 - Android

I have created a work around for this, i made a Class that sets snackbars with a custom duration using handler and postDelayed:

public class SnackBarMaker {

public static void snack(View content, String message, String actionText,  int actionTextColor, View.OnClickListener onClick){
    Snackbar.make(content, message, Snackbar.LENGTH_LONG)
            .setAction(actionText, onClick)
            .setActionTextColor(actionTextColor)
            .show();
}

public static void snackWithCustomTiming(View content, String message, int duration){
    final Snackbar snackbar = Snackbar.make(content, message, Snackbar.LENGTH_INDEFINITE);
    snackbar.show();
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            snackbar.dismiss();
        }
    },duration);
}
}

to use like this:

  //your duration
   int duration = 4000 
SnackBarMaker.snackWithCustomTiming(getActivity().findViewById(android.R.id.content)
                                               , getString(R.string.your_message), duration);

Solution 7 - Android

Hello there give this external library a try https://github.com/nispok/snackbar. It is deprecated but it will easily solve your problem. It is moreover easy to implement. Before Support library i was using this library only for snackbars. Due to the duration problem of support library, i am happy to use this library only.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontehnologView Question on Stackoverflow
Solution 1 - AndroidCommonsWareView Answer on Stackoverflow
Solution 2 - AndroidJimmy KamauView Answer on Stackoverflow
Solution 3 - AndroidMuhammad AlfaifiView Answer on Stackoverflow
Solution 4 - Androidkundan royView Answer on Stackoverflow
Solution 5 - AndroidJacksonView Answer on Stackoverflow
Solution 6 - AndroidZiv KestenView Answer on Stackoverflow
Solution 7 - AndroidAkshatView Answer on Stackoverflow