In general, a Toast can be displayed for either 2 seconds (Toast. LENGTH_SHORT) or 3.5 seconds (Toast. LENGTH_LONG). In this article, we will show you how you could display Toast for longer or shorter in Android.
You can use a android. os. CountDownTimer to count down the time for which to display a toast. The CountDownTimer class schedules a countdown for a time in milliseconds with notifications at specified intervals until the countdown is finished.
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.
If you dig deeper in android code, you can find the lines that clearly indicate, that we cannot change the duration of Toast message.
NotificationManagerService.scheduleTimeoutLocked() {
...
long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
}
and default values for duration are
private static final int LONG_DELAY = 3500; // 3.5 seconds
private static final int SHORT_DELAY = 2000; // 2 seconds
The values of LENGTH_SHORT
and LENGTH_LONG
are 0 and 1. This means they are treated as flags rather than actual durations so I don't think it will be possible to set the duration to anything other than these values.
If you want to display a message to the user for longer, consider a Status Bar Notification. Status Bar Notifications can be programmatically canceled when they are no longer relevant.
You may want to try:
for (int i=0; i < 2; i++)
{
Toast.makeText(this, "blah", Toast.LENGTH_LONG).show();
}
to double the time. If you specify 3 instead the 2 it will triple the time..etc.
If you want a Toast
to persist, I found you can hack your way around it by having a Timer
call toast.show()
repeatedly (every second or so should do). Calling show()
doesn't break anything if the Toast
is already showing, but it does refresh the amount of time it stays on the screen.
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