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.
A Toast in Android is a message that appears on the screen for a specific time whenever invoked. This message appears at the bottom of the application leaving some margin at the bottom. In general, a Toast can be displayed for either 2 seconds (Toast. LENGTH_SHORT) or 3.5 seconds (Toast.
Show the view or text notification for a long period of time. int. LENGTH_SHORT. Show the view or text notification for a short period of time.
There is no way to directly change the duration for which the toast is shown using the show() method without reimplementing the whole Toast class in your application, but there is a workaround. You can use a android. os. CountDownTimer to count down the time for which to display a toast.
The Toast class contains two predefined constants you can use: Toast. LENGTH_SHORT and Toast.
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
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With