How can I show a toast for a specific duration?
Solution 1
This cannot be done. To show a toast for a length shorter than Toast.LENGTH_SHORT
, you must cancel it after the time you want. Something like:
final Toast toast = Toast.makeText(getApplicationContext(), "This message will disappear in half a second", Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 500);
Solution 2
Adding to @Senth's answer, if you don't wont to accumulate the time when you call the showToast method multiple times, with the same Message:
private Toast mToastToShow = null;
String messageBeingDisplayed = "";
/**
* Show Toast message for a specific duration, does not show again if the message is same
*
* @param message The Message to display in toast
* @param timeInMSecs Time in ms to show the toast
*/
public void showToast(String message, int timeInMSecs) {
if (mToastToShow != null && message.equals(messageBeingDisplayed)) {
Log.d("DEBUG", "Not Showing another Toast, Already Displaying");
return;
} else {
Log.d("DEBUG", "Displaying Toast");
}
messageBeingDisplayed = message;
// Set the toast and duration
int toastDurationInMilliSeconds = timeInMSecs;
mToastToShow = Toast.makeText(this, message, Toast.LENGTH_LONG);
// Set the countdown to display the toast
CountDownTimer toastCountDown;
toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, timeInMSecs /*Tick duration*/) {
public void onTick(long millisUntilFinished) {
if (mToastToShow != null) {
mToastToShow.show();
}
}
public void onFinish() {
if (mToastToShow != null) {
mToastToShow.cancel();
}
// Making the Toast null again
mToastToShow = null;
// Emptying the message to compare if its the same message being displayed or not
messageBeingDisplayed = "";
}
};
// Show the toast and starts the countdown
mToastToShow.show();
toastCountDown.start();
}
You can display toast now for 500 ms like this:
showToast("Not Allowed", 500);
Solution 3
I found this answer. Albeit a bit more complex it also allows you to create toasts longer than Toast.LENGTH_LONG. You might have to change the tick duration from 1000ms to 500ms.
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.
Solution 4
This one is working fine for me.
final Toast mToastToShow;
int toastDurationInMilliSeconds = 10000;
mToastToShow = Toast.makeText(getApplicationContext(), "Snapshot Saved Successfully.",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();
the countdown is used to display a toast message for a specific duration.
Solution 5
Can't do what you are asking with the standard Toasts. Perhaps you should think about integrating a 3rd party library that gives you better Toast options (named Crouton). I haven't used it myself, but people seem to like it.
You can't control the length of Toasts in the standard OS.
Crouton link: https://github.com/keyboardsurfer/Crouton
Related videos on Youtube
MuraliGanesan
Updated on September 18, 2021Comments
-
MuraliGanesan over 2 years
This is the way I have to show the
Toast
for 500 milliseconds. Though, it's showing more than a second.Toast.makeText(LiveChat.this, "Typing", 500).show();
How can I show
Toast
only for 500 milliseconds?-
njzk2 over 11 yearsread the doc. the last parameter can take predefined values only, it is not in millis.
-
Lalit Poptani over 10 years
-
Ciro Santilli OurBigBook.com about 8 yearsPossible duplicate of Can an Android Toast be longer than "Toast.LENGTH_LONG"?
-
-
Raghav Sood over 11 years@Muraliganesan Please accept the answer if it solved your problem
-
younes0 about 9 yearsanother android api design flaw
-
Almog Baku over 8 yearsadding
2000
toi
won't ensure that the time has past.. you need to use clock instead -
Almog Baku over 8 yearsyou can't count that adding
2000
toi
will add2 seconds
, it's depends on the device speed. If you want to loop until time you need to do something likewhile((System.currentTimeMillis()-startTime)<2000))
. That's basic in every programing language.. never different the time base on the loop interval.