controling the displaying time of a toast [duplicate]
Asked Answered
R

3

1

I used toasts in my android application. Can I show the toast as long as i wish instead of

Toast.LENGTH_LONG

and

Toast.LENGTH_SHORT

can someone help me with some helpful code segment. Thank you.

Rosalie answered 4/12, 2013 at 10:52 Comment(1)
#14504227.Terenceterencio
C
6

What you can do is create a method which with some kind of looping around to make your Toast display as long as duration you want it,

private void showToast(int duration) {
    final Toast toast = Toast.makeText(getBaseContext(), 
                                                      "This is a Toast Message!",
                Toast.LENGTH_SHORT);
    toast.show();
    new CountDownTimer(duration, 500) {
            public void onTick(long millisUntilFinished) {
                toast.show();
            }
            public void onFinish() {
                toast.cancel();
            }

        }.start();
    }

And then you can call this method as showToast(10000);. So, what it will do is it will keep showing the Toast in loop until the duration and will cancel the toast as the duration is completed.

Centiare answered 4/12, 2013 at 11:9 Comment(0)
R
2

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

Enjoy..

Rosenbaum answered 4/12, 2013 at 10:54 Comment(5)
are you sure it will appear for 10 seconds? I don't think so :-)Centiare
Yes..It will appear for 10 secsRosenbaum
@Lalit Please try edited answer.. Its workingRosenbaum
Perhaps you meant tag.cancel() inside onFinish()?Vagus
Yes your right.. apologies for mistake... Also if it is working u can accept the answerRosenbaum
L
0

no cannot directly handle that,you have to use handler to cancel the toast like this:

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

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
       @Override
       public void run() {
           toast.cancel(); 
       }
}, 500); // 500ms is the time to cancel the toast.
Langford answered 4/12, 2013 at 11:5 Comment(2)
Doesn't really answer OP's question...Vagus
While this code is working, it still probably won't help OP because he wants to have longer duration instead.Vagus

© 2022 - 2024 — McMap. All rights reserved.