Click here to Skip to main content
15,867,835 members
Articles / Mobile Apps / Android

How to Show a Toast for a Specific Duration in Android

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
4 May 2015CPOL2 min read 45.8K   5   3
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 disappear by itself after a specified duration. Here is an example of what a toast looks like and how to display one:

CustomToastDuration

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

Java
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 canceled 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 canceled when the countdown is finished.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
Cindy Potvin is a software developer based in the Montreal area. At her day job, she creates web applications using the ASP.NET MVC framework and mobile applications using the Android SDK.

Comments and Discussions

 
SuggestionJUST REPEAT Pin
Member 1245276327-Jan-17 18:16
Member 1245276327-Jan-17 18:16 
GeneralMy vote of 5 Pin
Sachin Mahandule4-May-15 22:05
professionalSachin Mahandule4-May-15 22:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.