23

Since I've migrated my Android Project to AndroidX i am receiving the following lint error:

Error: Must be one of: Snackbar.LENGTH_INDEFINITE, Snackbar.LENGTH_SHORT, Snackbar.LENGTH_LONG [WrongConstant]
        Snackbar snackbar = Snackbar.make(rootView, message, sticky ? Snackbar.LENGTH_INDEFINITE : 4500

So instead of defining a custom duration (4500ms), I am now using Snackbar.LENGTH_LONG. Since i am implementing some time-based operation that depends on how long the snackbar is visible, I need to know the actual duration of Snackbar.LENGTH_LONG in milliseconds. How do I find out that value?

The docs are not really helpful here:

Show the Snackbar for a short period of time.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ice_chrysler
  • 2,633
  • 1
  • 21
  • 27
  • 1
    I think you can also use setDuration with a custom value in milliseconds. – Payam Asefi Jun 14 '19 at 12:59
  • You are right, its still possible to use custom milliseconds with Snackbar.setDuration(). They've only restricted using a custom value in Snackbar.make(...). – ice_chrysler Jun 16 '19 at 09:47

2 Answers2

49

After some fishing in the source code you will find these constants in the SnackbarManager:

private static final int SHORT_DURATION_MS = 1500;
private static final int LONG_DURATION_MS = 2750;

So to answer your question the actual duration for the long length is 2750 milliseconds.


Class com.google.android.material.snackbar.SnackbarManager:

enter image description here

Boken
  • 4,825
  • 10
  • 32
  • 42
jbarat
  • 2,382
  • 21
  • 23
  • 1
    Thank you so much! I actually tried to look it up in the source code but couldn't find the value somehow. – ice_chrysler Jun 14 '19 at 12:58
  • This is actually very useful since I kept getting a lint error for using the Snackbar length constants in Gradle 3.6.4. – harveyhans Aug 25 '21 at 05:26
1

As stated by jbarat, but here's it in the source code as asked by a user. Could not reply in the thread, and so posting it as an answer. 2.75 Seconds