111

I need the exact duration of LENGTH_LONG and LENGTH_SHORT in milliseconds (ms). Also I need to know if the duration of Toast message with LENGTH_LONG will have the same duration in any phone and with any API version.

Does someone know where is the duration defined ?, I mean defined in ms . I know that LENGTH_LONG is some int const with value 1. But I could not find where is the actual duration defined.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Lukap
  • 31,523
  • 64
  • 157
  • 244

7 Answers7

207

Answered here. Like you mentioned Toast.LENGTH_SHORT and Toast.LENGTH_LONG are not in ms but 0 or 1.

The actual durations are:

private static final int LONG_DELAY = 3500; // 3.5 seconds
private static final int SHORT_DELAY = 2000; // 2 seconds
Lars
  • 4,082
  • 2
  • 20
  • 20
16

The Toast.LENGTH_SHORT and Toast.LENGTH_LONG are just flags.
You can find here the official android source where these flags are defined:

public class NotificationManagerService extends SystemService {

    static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT;
    /** Amount of time (in milliseconds) a toast window can be shown. */
    //public static final int TOAST_WINDOW_TIMEOUT = 3500; // 3.5 seconds
    static final int SHORT_DELAY = 2000; // 2 seconds

    private void scheduleDurationReachedLocked(ToastRecord r)
    {
       mHandler.removeCallbacksAndMessages(r);
       Message m = Message.obtain(mHandler, MESSAGE_DURATION_REACHED, r);
       int delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY;
       //....
       mHandler.sendMessageDelayed(m, delay);
     }
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 1
    **Now, it is changed to:** `static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT; static final int SHORT_DELAY = 2000; // 2 seconds still the same` – Mayank Sharma Dec 26 '19 at 07:24
9

its true that we are not allowed to change the duration of the Toast. But if you are searching for an alternative and you really need to do this with a Toast, then you can try this.

Keep another duplicate toast in the next line

Ex:

Toast.makeText(this, "Hello StackOverFlow", Toast.LENGTH_LONG).show();
Toast.makeText(this, "Hello StackOverFlow", Toast.LENGTH_LONG).show();

The user won't detect any change in transitions between 2 toasts.

Thanks.

PasinduJay
  • 487
  • 5
  • 17
  • 3
    Since it is clearly mentioned as 'impossible to do' in the previous replies I'm providing an alternative method and I clearly mentioned it as well. Hope you can understand. I just searched now and found out same kind of question was there and go through the answers some of them are alternatives. But I don't find any down votes for them. Better for find alternatives than being said impossible only right? Hope this make sense. [Can an Android Toast be longer than “Toast.LENGTH_LONG”?](http://stackoverflow.com/questions/2220560/can-an-android-toast-be-longer-than-toast-length-long) – PasinduJay Mar 09 '16 at 10:01
5

You need set the duration override, with setDuration in the action, example:

int s = 6000; // milisegundo    
Snackbar.make(coordinatorLayout, "This is my Snackbar", Snackbar.LENGTH_LONG).setDuration(s)
.show();
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
3

I am wondering that why do you not use method setDuration() supported in Toast.java class???

 /**
 * Set how long to show the view for.
 * @see #LENGTH_SHORT
 * @see #LENGTH_LONG
 */
public void setDuration(@Duration int duration) {
    mDuration = duration;
}
Dong Thang
  • 410
  • 6
  • 6
2

LENGTH_SHORT & LENGTH_LONG are mapped to time interval of 1 Second (1000mS) & 5 Seconds (5000mS) respectively,

To see this you need to dig into AOSP source code of Toast. in Toast class time interval is decided based on the FLAG

mParams.hideTimeoutMilliseconds = mDuration == Toast.LENGTH_LONG ? LONG_DURATION_TIMEOUT : SHORT_DURATION_TIMEOUT;

where

  static final long SHORT_DURATION_TIMEOUT = 5000;
  static final long LONG_DURATION_TIMEOUT = 1000;

https://android.googlesource.com/platform/frameworks/base/+/f4bed684c939b0f8809ef404b8609fe4ef849263/core/java/android/widget/Toast.java

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
0

Through trial and error I have found Toast.LENGTH_LONG lasts very close to 2500ms

Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
Mart
  • 27
  • 6