61
votes

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.

7
What does "I can't set custom duration" mean? What are your specific symptoms?CommonsWare
@CommonsWare What he means is, he is not able to set a custom duration. It is only taking Length.Long and Length.Short. Output "Must be one of: Snackbar.LENGTH_SHORT, Snackbar.LENGTH_LONG" The documentations states that it is possible to set a custom duration in milliseconds. I believe it is a mistake in the library and will probably be fixed. I tried many different methods but was not able to find the solution. If you can have a hack at it and find and answer please post it. developer.android.com/reference/android/support/design/widget/…Eugene H
@EugeneH: You're right; it's a bug. See my answer below.CommonsWare
I have this problem too. I use LENGTH_LONG but the problem do not solved and snackbar dismiss at third second...Hamidreza Hosseinkhani
Use this link.. technotalkative.com/part-2-welcome-snackbar-goodbye-toast It's complete demo by @pareshMayani GDG founderZala Janaksinh

7 Answers

64
votes

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

32
votes

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

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.

7
votes

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

It seems to be fixed in

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

Only Lint shows it red underlined, but it works.

5
votes

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);
1
votes

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.