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 ?

Zoombie
  • 3,590
  • 5
  • 33
  • 40
  • 2
    It looks like you can't. http://stackoverflow.com/questions/2220560/can-an-android-toast-be-longer-than-toast-length-long – ccheneson May 23 '11 at 08:43
  • possible duplicate of [Set Toast Appear Length](http://stackoverflow.com/questions/3775074/set-toast-appear-length) – Sergey Glotov Nov 13 '13 at 09:50

8 Answers8

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);
Gal Bracha
  • 19,004
  • 11
  • 72
  • 86
Emran Hamza
  • 3,829
  • 1
  • 24
  • 20
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

Community
  • 1
  • 1
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
10

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

Community
  • 1
  • 1
noypiscripter
  • 1,451
  • 1
  • 13
  • 13
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..!!!

Virag Brahme
  • 2,062
  • 1
  • 20
  • 32
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);
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();
        }
    });
}
Emran Hamza
  • 3,829
  • 1
  • 24
  • 20
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!

Erikas
  • 1,006
  • 11
  • 22
-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();
                    }
                });
tjb
  • 11,480
  • 9
  • 70
  • 91
  • Did you try it? It does not wokt at all! – Christian Feb 16 '12 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 '12 at 13:45