How to dismiss a toast message before the provided time to display it

2k views Asked by At

What i have is a simple toast message that stays dispalayed until 90seconds are completed:

Toast.makeText(ActAtomicGodImages.this, "Please Wait \n Song is buffering ...", 90000);

What i am trying to do:

  1. Say i have a button click listener what does some action(Ex: opens another activity).
  2. On click of that button i want to dismiss the toast even if the 90seconds are not completed
  3. Is this possible, if so how
2

There are 2 answers

0
CommonsWare On BEST ANSWER

Call cancel() on the Toast to get rid of it.

However, 90000 will not work. Your choices are Toast.LENGTH_SHORT or Toast.LENGTH_LONG, neither of which are anywhere near 90 seconds in duration.

2
Jonas Czech On

Do it like this:

  1. Define your toast, possibly like this, maybe as a global variable in your class, so you can access it from anywhere in your class:

    Toast toast = new Toast(context);
    
  2. To show it

    toast.setText("Text");
    toast.show(); //(call show()  to display Toast)
    
  3. When you need to hide it:

    toast.cancel();//(call cancel() to  hide Toast).
    

Also note that a duration of 90000 may not work correctly, as CommonsWare says.