2

I want the code that allows freedom of duration of the toast.

also the location of a toast. i tried using handlers but im not completely sure how it works so that was a failure from my side.

so any better ideas?

csharp
  • 23
  • 2

2 Answers2

3
public void CustomToast(Context context, String msg, long timeinmillisec) {
    // TODO Auto-generated constructor stub

    final Toast toast = Toast.makeText(context, ""+msg, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
            toast.show();
    CountDownTimer countDownTimer = new CountDownTimer(timeinmillisec, 1000)     {

        private boolean isShowing = false;

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            if (isShowing) {
                toast.cancel();
            } else {
                toast.show();
            }
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub

        }
    };
    countDownTimer.start();
}
Vivek Khare
  • 162
  • 4
  • Just tried and this does not work, besides the positioning of the toast. Where did you get this code? – App Dev Guy Jun 27 '15 at 09:45
  • That's mine and works well as i am using it. Although i have used custom layout to customize toast. – Vivek Khare Jun 27 '15 at 09:51
  • The only thing you have change in duration is the from LENGTH_SHORT to LENGTH_LONG, which is 1.5 seconds to 2.5 seconds. The onTick has no effect on toast.show() for me at all, and I am implementing it exactly as you are. Perhaps you could show how you're calling this method to display the parsed msg. – App Dev Guy Jun 27 '15 at 09:53
  • I retract my statement, you're code works providing it is implemented correctly. – App Dev Guy Jun 27 '15 at 09:58
  • oh i got it....fixed it now ,forgot to set the text message on it,glad that helped. – Vivek Khare Jun 27 '15 at 10:11
  • I revised it a little as well in my own answer plus displayed how to call and utilise. Giving yours a +1 – App Dev Guy Jun 27 '15 at 10:14
1

As for the location, you can call the following method of Toast class.
setGravity (int gravity, int xOffset, int yOffset)

You can only set the duration to one of the following constants LENGTH_SHORT and LENGTH_LONG

Rick Sanchez
  • 4,528
  • 2
  • 27
  • 53
  • Doesn't work @Kushtrim, those duration are are default set. i want to customize them. – csharp Jun 26 '15 at 21:50
  • You cannot. If you want shorter durations, you might have to use some custom dialog. For longer durations, a *workaround* is to call Toast multiple times with the same text ( though it may blink transitioning from Toast to Toast ) – Rick Sanchez Jun 26 '15 at 21:57