61

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.

einverne
  • 6,454
  • 6
  • 45
  • 91
tehnolog
  • 1,204
  • 1
  • 11
  • 23
  • What does "I can't set custom duration" mean? What are your specific symptoms? – CommonsWare May 30 '15 at 20:08
  • 6
    @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. http://developer.android.com/reference/android/support/design/widget/Snackbar.html#setDuration(int) – Eugene H May 31 '15 at 00:18
  • 1
    @EugeneH: You're right; it's a bug. See my answer below. – CommonsWare May 31 '15 at 00:27
  • I have this problem too. I use LENGTH_LONG but the problem do not solved and snackbar dismiss at third second... – Hamidreza Hosseinkhani Jun 06 '15 at 11:25
  • Use this link.. http://www.technotalkative.com/part-2-welcome-snackbar-goodbye-toast/ It's complete demo by @pareshMayani GDG founder – Zala Janaksinh Jul 02 '15 at 07:23

8 Answers8

66

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

GunnerFan
  • 3,576
  • 3
  • 25
  • 38
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • There are also issues with the FAB button on the support library. https://code.google.com/p/android/issues/detail?id=175185#makechanges – Eugene H May 31 '15 at 00:32
  • @CommonsWare where did you find that source code? I'd love to know the values of `LONG_DURATION_MS` and `SHORT_DURATION_MS` for a temporary solution while we wait on the fix to be published, but the places that I have looked (AOSP, source in download manager) do not seem to have `Snackbar` nor `SnackbarManager` classes. – Scott W Jun 25 '15 at 14:39
  • 5
    @ScottW: "where did you find that source code?" -- ...and I pulled the sword from the stone, and the Lady of the Lake then intoned "sorry, but the whole King of the Britons thing was a one-time deal; can I interest you in some MNC source code instead?". Or, possibly, I downloaded the source through the SDK Manager. Believe what you want. :-) "I'd love to know the values of LONG_DURATION_MS and SHORT_DURATION_MS" -- 1500 and 2750, respectively. – CommonsWare Jun 25 '15 at 14:49
  • 3
    @ScottW: "the places that I have looked (AOSP, source in download manager) do not seem to have Snackbar nor SnackbarManager classes." -- on my machine, it is in `$ANDROID_SDK/sources/android-MNC/android/support/design/widget/SnackbarManager.java`. – CommonsWare Jun 25 '15 at 14:50
  • @CommonsWare ah ha! Thank you! I was looking under `$ANDROID_SDK/extras/android/support/design/...`. I didn't even think to check in the M preview source. – Scott W Jun 25 '15 at 15:18
  • Also, I choose to believe the Lady of the Lake story ;) – Scott W Jun 25 '15 at 15:21
  • 2
    @ScottW: Glad you found it, and I am equally glad that you believe in Arthurian mythos! A quick note to anyone who stumbles upon this in the future, though -- I had the two numbers reversed in my comment. The long duration is 2750, and the short duration is 1500. – CommonsWare Jun 25 '15 at 15:32
  • 1
    It looks like this issue has been fixed in M with Preview 2 but the fix has not yet been released in the Design Support Library. – John Cummings Jul 14 '15 at 20:03
  • This issue has been fixed in latest Design Support Library. `dependencies { compile 'com.android.support:design:22.2.1' }` – Gaurav Vachhani Aug 03 '15 at 08:11
  • 7
    In 22.2.1 it's possible to set a custom duration, but I had to disable the lint inspection for the setDuration method because it was giving an error (the inspection hasn't been updated correctly yet). – jmart Aug 11 '15 at 10:06
  • 1
    I like how Google ship the SDK. "Go figure out yourself in our source code and report to us , you will do our QA. This way we will save money and time." – Raymond Chenon Dec 04 '15 at 10:56
32

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();
Jimmy Kamau
  • 325
  • 4
  • 8
  • 1
    If we use 8000, Android compiles. But it doesn't compile, if we set a constant (DURATION = 8000). Write `@SuppressLint("WrongConstant")` for this situation. – CoolMind Nov 13 '19 at 17:24
9

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.

Muhammad Alfaifi
  • 5,662
  • 2
  • 19
  • 23
8

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();
Ziem
  • 6,579
  • 8
  • 53
  • 86
kundan roy
  • 1,936
  • 1
  • 18
  • 21
6

It seems to be fixed in

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

Only Lint shows it red underlined, but it works.

Mo.
  • 26,306
  • 36
  • 159
  • 225
Jackson
  • 87
  • 1
  • 3
5

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);
Ziv Kesten
  • 1,206
  • 25
  • 41
1

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.

Akshat
  • 76
  • 1
  • 9
0

In 2022 with AndroidX this solution is still actual: https://stackoverflow.com/a/44009407/7699617

Just set your duration in ms directly to make function:

Snackbar.make(context, messageId, DISMISS_TIMEOUT)
        .setAction(actionId, actionListener)
        .addCallback(dismissCallback)
        .show()

const val DISMISS_TIMEOUT = 6000

In addition, I made some calculations and find out default duration values:

  • Snackbar.LENGTH_LONG ~ 3000ms
  • Snackbar.LENGTH_SHORT ~ 2000ms
SerjantArbuz
  • 982
  • 1
  • 12
  • 16