34

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.

Mitchell
  • 929
  • 2
  • 11
  • 34
  • 1
    I have developed a custom Toast class with which you can show Toast for a specified amount of time... have a look at my answer http://stackoverflow.com/questions/2220560/can-an-android-toast-be-longer-than-toast-length-long/21203554#21203554 – Gopal Gopi Jan 18 '14 at 12:43

7 Answers7

103

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);
noypiscripter
  • 1,451
  • 1
  • 13
  • 13
6

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.

Macarse
  • 91,829
  • 44
  • 175
  • 230
4

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..!!!

Virag Brahme
  • 2,062
  • 1
  • 20
  • 32
1

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

Skip
  • 6,240
  • 11
  • 67
  • 117
0

//try it

    public void myToast(String message) {
    LayoutInflater myInflator = getLayoutInflater();
    View myLayout = myInflator.inflate(R.layout.custom_layout,
            (ViewGroup) findViewById(R.id.toastlayout));
    TextView myMessage = (TextView) myLayout.findViewById(R.id.label);
    myMessage.setText(message);
    Toast toast = new Toast(getApplicationContext());
    toast.setView(myLayout);
    toast.setDuration(400);
    myMessage.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL
            | Gravity.CENTER_VERTICAL);
    toast.show();
}
Emran Hamza
  • 3,829
  • 1
  • 24
  • 20
  • 1
    I don't think this does anything for extending the duration. If the duration is not either LENGTH_SHORT or LENGTH_LONG, it defaults to LENGTH_LONG. This method accepts arbitrary values in nature, but in reality, only changes the behavior (toast length) if the value is not zero. – Lo-Tan Feb 13 '13 at 19:34
0

The stock Android Toast class is coded to accept only a Toast.LENGTH_SHORT or Toast.LENGTH_LONG parameter when calling a Toast. The values of these parameters are 0 and 1 respectively and do not accept any millisecond value when calling setDuration(); If you must show a Toast for a different duration than you may consider using a class from my SuperToasts library. The SuperToast class in the library is a mimic of the stock Android Toast class and can have any millisecond value used as a duration parameter. I do not recommend using this class to show a Toast longer than the maximum stock Android Toast length due to the lingering effect of these Toasts. I recommend that you use the SuperActivityToast class to show Toast messages in an Activity/Fragment because the Toast will be destroyed along with your Activity eliminating any chance of a lingering message. To use this class you may create a new object:

SuperActivityToast superActivityToast = new SuperActivityToast(this);  
superActivityToast.setDuration(SuperToast.DURATION_SHORT); 
// setDuration(); can also accept millisecond values
// superActivityToast.setDuration(1000);  
superActivityToast.setText("Hello world!");  
superActivityToast.show();  

Or use the static method:

SuperActivityToast.createDarkSuperActivityToast(this, "Hello world!", SuperToast.DURATION_SHORT).show();  

There are tons of customization options you can use with the library as well, check out the Wiki page!

John P.
  • 4,358
  • 4
  • 35
  • 47
-1

Here is another way to configure the time per your choice:

public void showMsg(String msg, final long duration) {
    final Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);
    toast.show();
    Thread t = new Thread() {
        public void run(){
            try {
                sleep(duration);
                toast.cancel(); 
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            finally { }
        }
    };
    t.start();
}

NOTE: The duration is specified in milliseconds.