28

I want to display toast less than Toast.LENGTH_SHORT, as i feel its taking around 2 seconds. i want to display toast only for half second.

And what is time interval for Toast.LENGTH_SHORT and Toast.LENGTH_LONG ?

2

8 Answers 8

33

This has worked for me

final Toast toast = Toast.makeText(getApplicationContext(), "The following 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);
24

There are only two possible values:

private static final int LONG_DELAY = 3500; // 3.5 seconds
private static final int SHORT_DELAY = 2000; // 2 seconds

Setting other values doesn't work. If duration not equals 1 (Toast.LENGTH_LONG), then duration will be SHORT_DELAY (2 seconds):

long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);

In sources of Toast written that

This time could be user-definable.

but I can't find way to do this.

Update: There is solution here: Set Toast Appear Length

3
  • 1
    I think that implies user-definable by the user of the device. Not the developer.
    – Aaron
    Apr 25, 2013 at 13:09
  • I am afraid, bt it might be incorrect @Sergey Glotov to say that one cant do it. The code mentioned by Emran Hamza very well works. Nov 13, 2013 at 9:33
  • @SergeyGlotov Not an issue :) Nov 13, 2013 at 10:12
10

See my suggested solution here. You basically call toast.cancel() after a specified delay that is shorter than the standard toast duration.

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

1
public void showMyToast(final Toast toast, final int delay) {
    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            toast.show();
        }
    }, 0, 1000);
    new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            toast.cancel();
            timer.cancel();
        }
    }, delay);
}

//how to use(MainActivity.this is example ,it could be changed by yourself) and (In showMyToast(first param ,second param) method, second parameter is time what u really defined like this)

Toast toast=Toast.makeText(MainActivity.this, "MyToast Test", Toast.LENGTH_LONG);
            showMyToast(toast, 1000);
2
  • 1
    Please edit your answer to explain why this piece of code answers the question. Mar 5, 2018 at 14:00
  • This is a method , I will add how to use it. Mar 5, 2018 at 14:28
0

it will work.


   public void toastMessage(final String message) {
    this.runOnUiThread(new Runnable() {
        public void run() {
            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(100);
            myMessage.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL
                    | Gravity.CENTER_VERTICAL);
            toast.show();
        }
    });
}
0
0

For noobs I've made the simplest solution - a method.

public void showToastMessage(String text, int duration){
        final Toast toast = Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT);
        toast.show();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
            }
        }, duration);
}

You should also import:

import android.os.Handler;
import android.widget.Toast;

You can call this method for example:

showToastMessage("your noob", 1000);

method above should work only in Fragment! If you want it to work in Activity, replace getActivity() with getApplicationContext() in toast message. Good luck developers!

-4

This seems to work for me (set the duration to whatever you want):

mActivity.runOnUiThread(new Runnable() {
                    public void run() {
                        Toast toast = new Toast(mActivity
                                .getApplicationContext());
                        toast.setView(layout);
                        toast.setDuration(400);
                        toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
                        toast.show();
                    }
                });
2
  • Did you try it? It does not wokt at all!
    – Christian
    Feb 16, 2012 at 16:21
  • Yes, when I tried it it worked, or I though it did. The accepted answer seems to be that its impossible. Its been a long time since I wrote this. I was using Android 2.2 at the time.
    – tjb
    Feb 18, 2012 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.