8

I am using Toast in my app. When I am pressing a button, it is showing a Toast. My problem is that the second time I'm pressing on the button the second toast is "waiting" for the first one to end and only than it shows.. I want the current one to show immediately and not wait. This is my simple code:

toast = Toast.makeText(getApplicationContext(), "Press Back to retorn to the main page", Toast.LENGTH_SHORT);
toast.show();

how can I do that?

Dhasneem
  • 4,037
  • 4
  • 33
  • 47
roiberg
  • 13,629
  • 12
  • 60
  • 91

4 Answers4

6

You can always cancel a Toast object.

final Toast tst = Toast.makeText(ctx, "This is a toast.", Toast.LENGTH_SHORT);
tst.show();

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
       @Override
       public void run() {
           tst.cancel(); 
           tst.setText("Same toast with another message.");
           tst.show();
       }
}, 1000);

So instead of creating another Toast object you can use the first one, cancel it, set the new text and show it again.

papaiatis
  • 4,231
  • 4
  • 26
  • 38
3

In https://stackoverflow.com/a/4485531/517561, the writer didn't cancel the toast, they simply changed its text.

Community
  • 1
  • 1
Sparky
  • 8,437
  • 1
  • 29
  • 41
2

Cancel your original Toast, set a new message and show again the Toast message.

Toast mytoast;
mytoast = Toast.makeText(this, "Hi Ho Jorgesys! ", Toast.LENGTH_LONG);
mytoast.show();
....
....
....
if(CancelToast){
  mytoast.cancel();  //cancelling old Toast!
  mytoast = Toast.makeText(this, "Same toast with another message.", Toast.LENGTH_LONG); //Setting a new message.
  mytoast.show(); //Show the new message!.
}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
1

You can use toast.cancel() befor showing next toast.

Rookie
  • 8,660
  • 17
  • 58
  • 91
  • cancelling won't reduce the toast time. – Kazekage Gaara Jun 04 '12 at 08:52
  • Thanks Kazekagebut, but as per defination Close the view if it's showing, or don't show it if it isn't showing yet. You do not normally have to call this. Normally view will disappear on its own after the appropriate duration. – Dheeresh Singh Jun 04 '12 at 08:55
  • 1
    public void cancel () Since: API Level 1 Close the view if it's showing, or don't show it if it isn't showing yet. You do not normally have to call this. Normally view will disappear on its own after the appropriate duration. – Rookie Jun 04 '12 at 08:55
  • The other toast will come after the fixed amount of time that it is supposed to wait,whether you cancel the first toast or not! – Kazekage Gaara Jun 04 '12 at 08:58
  • Cancelling the `Toast` works perfectly, and absolutely reduces the toast time if done correctly. Check sample code in [this related post](http://stackoverflow.com/questions/16098151/android-cancel-toast-when-exiting-the-app-and-when-toast-is-being-shown), or link to github and just copy the gist. Code has been production tested for a few years so far. – Richard Le Mesurier Aug 16 '16 at 21:04