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
- LENGTH_SHORT
- 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!
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!