What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations


Toasts are used in android to display Notifications. When .show() method is called they fade-in and stay for a while and fade-out depending upon what time in milliseconds we set for a Toast using setDuration() method.

We can only specify two values for the duration of Toast those are

  1. LENGTH_SHORT
  2. LENGTH_LONG

Do you know that even if we can set any integer value to setDuration(), the toast message will only be displayed for 1.5secs (ie Shot period) or 3secs (long period).

Both of these variables are Flags with value 0 and 1 respectively.

The reason if you see: NotificationManagerService.java code which is called to display Toast,

private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
    {
        Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
        long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
        mHandler.removeCallbacksAndMessages(r);
        mHandler.sendMessageDelayed(m, delay);
    }

Where,

private static final int LONG_DELAY = 3500; // 3.5 seconds
 private static final int SHORT_DELAY = 2000; // 2 seconds

Hence there is no way you can change the value of LENGTH_LONG or LENGTH_SHORT. The only way to display Toast for a longer period is through some java code tweaking!