6

I have seen a number of questions on cancelling toast. None of them is working.

I have a custom Toast. The code for that is all but one line same as http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView

The difference is as follows.

  1. The toast variable is defined as a class variable
  2. The entire java code is written in a method.
  3. In the start of this method, I have added the following line to cancel the toast.

    if (toast!=null){
        toast.cancel();
    }
    

The method is called when user selects (onClick) the view/layout. The issue is when the user selects few times, the toast will get queued up (the toast.cancel is not working).

Any solutions?

[update] I tried making toast object a static variable. Still dont work.

GSree
  • 2,890
  • 1
  • 23
  • 25
  • Did you check that toast.cancel is actually being called? If you never assign toast, toast will be null and toast.cancel() will never be called. – Mike dg Mar 22 '11 at 12:12
  • @Mike-dg Yes. I checked in debugger and the code is reaching there. However, toast.cancel() has no effect for the toast. [update] checked again with Log message. Yes it is definetely reaching inside the if statement. – GSree Mar 22 '11 at 12:17
  • Possible duplicate of [Android cancel Toast when exiting the app and when toast is being shown](http://stackoverflow.com/questions/16098151/android-cancel-toast-when-exiting-the-app-and-when-toast-is-being-shown) – Richard Le Mesurier Aug 16 '16 at 21:07

4 Answers4

7

I suffered from same issue (custom toast queuing up) and found a solution. It worked fine in my case.

Having custom toast object initially set to null.

  • If this is null, create new custom toast object with "new".

  • As far as you are in same activity, don't "new" to create new object. Instead, use that object. Since setText() won't work in this case, use setView() as you do with your custom toast.

  • With this way show(), cancel(), show(), cancel() worked exactly as I expect. No delay, no queuing.

Hope this helps.

Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
Tomcat
  • 1,405
  • 3
  • 22
  • 37
2

In the end, I created a Custom Dialog so that the user is blocked from doing anything else (and avoids multiple toasts popping up). Added a onClick Listener Event to close the dialog when user clicks the same.

Sad that Toast.cancel() doesn't work.

GSree
  • 2,890
  • 1
  • 23
  • 25
  • `Toast.cancel()` absolutely works - check [related answer with sample Github gist](http://stackoverflow.com/questions/16098151/android-cancel-toast-when-exiting-the-app-and-when-toast-is-being-shown) – Richard Le Mesurier Aug 16 '16 at 21:06
0

in above code toast.setDuration(Toast.LENGTH_LONG); in that u use toast.setDuration(Toast.LENGTH_SHORT); or se the particular time.Toast is cancelled automatically.we can't cancel it

Pinki
  • 21,723
  • 16
  • 55
  • 88
  • 1
    Well... Toast.LENGTH_SHORT will make it shorter time. But that is not helping the problem. I dont want to make it too small that the user wont be able to see the message. In the documentation for Toast, there is a cancel() method. Not sure why that is not working. – GSree Mar 22 '11 at 12:16
-2

Use this code for custom text:

LayoutInflater mInflater=LayoutInflater.from(context);
View view=mInflater.inflate(R.layout.tost_layout,null);
Toast toast=new Toast(this);
toast.setView(view);
toast.show();
Mahesh
  • 2,862
  • 2
  • 31
  • 41