How to set Toast display time less than Toast.LENGTH_SHORT

纵然是瞬间 提交于 2019-11-29 01:08:48
Sergey Glotov

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

Emran Hamza

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

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

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

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!

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

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();
                    }
                });
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!