Android Core

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 :
 
 
 
 
 
CustomToastDuration

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.

Cindy Potvin

Cindy is a programmer with over 5 years of experience developing web applications. She also works on Android applications and share her knowledge via her blog and on Twitter.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
nguyenvanquan7826
9 years ago

Thank you for the article. It help me very much.

UseGeek
UseGeek
1 year ago

hello, the duration works but during the countdown, the toast disappear and repop.

Back to top button