How to show a Toast for a specific duration in Android
In the Android SDK, an android.widget.Toast
is a small message that pops up at the bottom of the screen to display an information. The toast will disappears by itself after a specified duration. Here is an example of what a toast looks like and how to display one :
Context context = getApplicationContext(); Toast.makeText(context, "Hello world, I am a toast.", Toast.LENGTH_SHORT).show();
The duration for which a toast is displayed on screen is unfortunately defined by a flag: you can either show it for a SHORT duration, which is 2 seconds or a LONG duration which is 3,5 seconds. But what if you have a long error message that needs to be shown for longer than that? Or if you need to show a countdown that updates every second?
There are 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 CountDownTimer
class schedules a countdown for a time in milliseconds with notifications at specified intervals until the countdown is finished.
In this example, the countdown is used to display a toast message for a specific duration when a button is pressed:
private Toast mToastToShow; public void showToast(View view) { // Set the toast and duration int toastDurationInMilliSeconds = 10000; mToastToShow = Toast.makeText(this, "Hello world, I am a toast.", Toast.LENGTH_LONG); // Set the countdown to display the toast CountDownTimer toastCountDown; toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, 1000 /*Tick duration*/) { public void onTick(long millisUntilFinished) { mToastToShow.show(); } public void onFinish() { mToastToShow.cancel(); } }; // Show the toast and starts the countdown mToastToShow.show(); toastCountDown.start(); }
Here is how it works: the countdown has a notification time shorter than the duration for which the toast is displayed according to the flag, so the toast can be shown again if the countdown is not finished. If the toast is shown again while it is still on screen, it will stay there for the whole duration without blinking. When the countdown is finished, the toast is cancelled to hide it even if its display duration is not over.
This works even if the toast must be shown for a duration shorter than the default duration: the first toast displayed will simply be cancelled when the countdown is finished.
drcrab
August 3, 2016 @ 02:25
Working like charm
Thanks {Kewl}
Parth Prajapati
April 13, 2021 @ 02:33
Hi, I tried this but for me it’s not working. Apparently, I have used thread and inside that thread I have implemented countdowntimer and it disappears after 3.4 Sec.
Toast! Is it accessible? - Maxability
February 4, 2018 @ 12:02
[…] immediately after it disappears even without user notice. Here is a sample implementation on cindypotvin’s blog that shows the toast message until the count down set by the author […]
Terry
February 4, 2018 @ 13:36
Thanks for this nifty trick. 🙂
Svenne
April 30, 2018 @ 09:23
I would suggest; if you need a message to be shown for more than the toast duration, use a dialog instead.
Cindy Potvin
September 9, 2018 @ 10:13
I agree that the dialog may be the best choice UI-wise; at the time somebody requested this and you don’t always have a choice.
Barry Fruitman
May 24, 2018 @ 20:43
Neato!
Ahmet
September 5, 2018 @ 04:24
Not 2- 3,5 seconds
Right click on Toast > Go To > Declaration
– static final long SHORT_DURATION_TIMEOUT = 4000;
static final long LONG_DURATION_TIMEOUT = 7000;
Dina
August 22, 2019 @ 08:52
Thanks!
Vinothkumar
November 20, 2019 @ 00:51
int toastDurationInMilliSeconds = 10000;
Toast objToas=Toast.makeText(getApplicationContext(),”HI, THIS IS LONG TOAST”,Toast.LENGTH_LONG);
// Set the countdown to display the toast
CountDownTimer toastCountDown;
toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, 1000 /*Tick duration*/) {
public void onTick(long millisUntilFinished) {
objToas.show();
}
public void onFinish() {
objToas.cancel();
}
};
// Show the toast and starts the countdown
objToas.show();
toastCountDown.start();
I am using this code but i didn’t get the toast message. Getting error?
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gcr.foretee, PID: 8275
java.lang.IllegalStateException: View android.widget.LinearLayout{4ec626 V.E…… ……ID 0,0-699,172} has already been added to the window manager.
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:328)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.widget.Toast$TN.handleShow(Toast.java:499)
at android.widget.Toast$TN$1.handleMessage(Toast.java:403)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Hyeon
July 11, 2020 @ 08:32
Thanks !!