What is the duration of a Toast LENGTH_LONG and LENGTH_SHORT

后端 未结 6 1384
梦毁少年i
梦毁少年i 2020-12-01 10:13

I need the exact duration of LENGTH_LONG and LENGTH_SHORT in milliseconds (ms). Also I need to know if the duration of Toast message with LENGTH_LONG will have the same dura

相关标签:
6条回答
  • 2020-12-01 10:32

    Through trial and error I have found Toast.LENGTH_LONG lasts very close to 2500ms

    0 讨论(0)
  • 2020-12-01 10:45

    its true that we are not allowed to change the duration of the Toast. But if you are searching for an alternative and you really need to do this with a Toast, then you can try this.

    Keep another duplicate toast in the next line

    Ex:

    Toast.makeText(this, "Hello StackOverFlow", Toast.LENGTH_LONG).show();
    Toast.makeText(this, "Hello StackOverFlow", Toast.LENGTH_LONG).show();
    

    The user won't detect any change in transitions between 2 toasts.

    Thanks.

    0 讨论(0)
  • 2020-12-01 10:48

    The Toast.LENGTH_SHORT and Toast.LENGTH_LONG are just flags.
    You can find here the official android source where these flags are defined:

    public class NotificationManagerService extends SystemService {
    
        static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT;
        /** Amount of time (in milliseconds) a toast window can be shown. */
        //public static final int TOAST_WINDOW_TIMEOUT = 3500; // 3.5 seconds
        static final int SHORT_DELAY = 2000; // 2 seconds
    
        private void scheduleDurationReachedLocked(ToastRecord r)
        {
           mHandler.removeCallbacksAndMessages(r);
           Message m = Message.obtain(mHandler, MESSAGE_DURATION_REACHED, r);
           int delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY;
           //....
           mHandler.sendMessageDelayed(m, delay);
         }
    }
    
    0 讨论(0)
  • 2020-12-01 10:49

    I am wondering that why do you not use method setDuration() supported in Toast.java class???

     /**
     * Set how long to show the view for.
     * @see #LENGTH_SHORT
     * @see #LENGTH_LONG
     */
    public void setDuration(@Duration int duration) {
        mDuration = duration;
    }
    
    0 讨论(0)
  • 2020-12-01 10:51

    You need set the duration override, with setDuration in the action, example:

    int s = 6000; // milisegundo    
    Snackbar.make(coordinatorLayout, "This is my Snackbar", Snackbar.LENGTH_LONG).setDuration(s)
    .show();
    
    0 讨论(0)
  • 2020-12-01 10:53

    Answered here. Like you mentioned Toast.LENGTH_SHORT and Toast.LENGTH_LONG are not in ms but 0 or 1.

    The actual durations are:

    private static final int LONG_DELAY = 3500; // 3.5 seconds
    private static final int SHORT_DELAY = 2000; // 2 seconds
    
    0 讨论(0)
提交回复
热议问题