csharp
csharp

Reputation: 23

I need the code to control the duration of my toast in android

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?

Upvotes: 2

Views: 66

Answers (2)

Vivek Khare
Vivek Khare

Reputation: 162

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

Upvotes: 3

Rick Sanchez
Rick Sanchez

Reputation: 4746

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

Upvotes: 1

Related Questions