Set Toast Appear Length

Is there anyway I can tell a Toast Notification to show up only for a specified amount of time. Generally shorter then a regular toast message.


Solution 1:

I found a solution to this by calling toast.cancel() after a certain delay that is shorter than the standard toast duration.

        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);

Solution 2:

No.

You can do something like:

Toast a = Toast.makeText(this, "a", Toast.LENGTH_LONG);
a.setDuration(300);

but it will not show itself.

The duration should be either LENGTH_SHORT or LENGTH_LONG.

Solution 3:

Try this

final Toast toast = Toast.makeText(getBaseContext(), "YOUR MESSAGE",Toast.LENGTH_SHORT);
            toast.show();
            new CountDownTimer(10000, 1000)
            {
                public void onTick(long millisUntilFinished) {toast.show();}
                public void onFinish() {toast.cancel();}
            }.start();

Hope this help.. Enjoy..!!!

Solution 4:

You can set a longer duration by using a hack, as described here