How to cancel Toast

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


The shortest duration you can specify for the toast is Toast.LENGTH_SHORT which has a value of 0 but is actually 2000 milliseconds long. If you want it shorter than that, then try this:

    final Toast toast = Toast.makeText(ctx, "This message will disappear in 1 second", Toast.LENGTH_SHORT);
    toast.show();

    Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
           @Override
           public void run() {
               toast.cancel(); 
           }
    }, 1000); //specify delay here that is shorter than Toast.LENGTH_SHORT

Tags:

Android

Toast