0

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
Devrath
  • 42,072
  • 54
  • 195
  • 297

2 Answers2

4

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.

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
  • [+1] for descriptive answer .... apart from the two values `short` and `long` its not possible to give any other values (http://stackoverflow.com/a/2221285/1083093) – Devrath Jun 07 '15 at 17:32
  • 1
    Great answer but actually, you have to create the toast object like this, Toast toast = Toast.makeText(context, "", Toast.LENGTH_SHORT); or your app crashes. – Ziad H. Dec 25 '19 at 01:36
3

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491