Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop a Toast and start another in android [duplicate]

Tags:

android

toast

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?

like image 511
roiberg Avatar asked Jun 04 '12 08:06

roiberg


People also ask

How do you avoid a toast if there's one toast already being shown?

How do you avoid a toast if there's one toast already being shown? It turned out by logging getDuration() that it carries a value of 0 (if makeText() 's parameter was Toast. LENGTH_SHORT ) or 1 (if makeText() 's parameter was Toast.

How do I stop toast messages on android?

You can use toast. cancel() befor showing next toast. cancelling won't reduce the toast time.

How do I close toast on Android?

Toast. makeText returns a Toast object. Call cancel() on this object to cancel it.


3 Answers

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.

like image 163
papaiatis Avatar answered Sep 23 '22 18:09

papaiatis


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

like image 37
Sparky Avatar answered Sep 25 '22 18:09

Sparky


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!.
}
like image 40
Jorgesys Avatar answered Sep 24 '22 18:09

Jorgesys