Home » Android » Can an Android Toast be longer than Toast.LENGTH_LONG?

Can an Android Toast be longer than Toast.LENGTH_LONG?

Posted by: admin March 10, 2020 Leave a comment

Questions:

When using setDuration() for a Toast, is it possible to set a custom length or at least something longer than Toast.LENGTH_LONG?

How to&Answers:

The values of LENGTH_SHORT and LENGTH_LONG are 0 and 1. This means they are treated as flags rather than actual durations so I don’t think it will be possible to set the duration to anything other than these values.

If you want to display a message to the user for longer, consider a Status Bar Notification. Status Bar Notifications can be programmatically canceled when they are no longer relevant.

Answer:

If you dig deeper in android code, you can find the lines that clearly indicate, that we cannot change the duration of Toast message.

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

and default values for duration are

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

Answer:

You may want to try:

for (int i=0; i < 2; i++)
{
      Toast.makeText(this, "blah", Toast.LENGTH_LONG).show();
}

to double the time. If you specify 3 instead the 2 it will triple the time..etc.

Answer:

The best solution to avoid fading effects between the toasts which are launched in sequence:

final Toast tag = Toast.makeText(getBaseContext(), "YOUR MESSAGE",Toast.LENGTH_SHORT);

tag.show();

new CountDownTimer(9000, 1000)
{

    public void onTick(long millisUntilFinished) {tag.show();}
    public void onFinish() {tag.show();}

}.start();

Here the toast is displayed approximately 10 s.

Hope this helps.

Answer:

If you want a Toast to persist, I found you can hack your way around it by having a Timer call toast.show() repeatedly (every second or so should do). Calling show() doesn’t break anything if the Toast is already showing, but it does refresh the amount of time it stays on the screen.

Answer:

I have developed a Custom Toast class with which you can show Toast for a desired amount of duration (in Milli seconds)

Answer:

I’ve coded up a helper class for doing this. You can see the code at github: https://github.com/quiqueqs/Toast-Expander/blob/master/src/com/thirtymatches/toasted/ToastedActivity.java

This is how you’d display a toast for 5 seconds (or 5000 milliseconds):

Answer:

I know I am a bit late, but I took Regis_AG’s answer and wrapped it in a helper class and it works great.

In your application code, just do something like this:

Answer:

I know the answer is quite late .. I had the very same issue and decided to implement my own version of bare bones Toast , after looking into android’s source code for toast .

Basically you need to create a new Window manager , and show and hide the window for the desired duration duration using a handler

After initialization of the layout you can use your own hide and show methods

Now all you need is to add two runnable threads which calls the handleShow() and the handleHide() which you could post to the Handler.

and the final part

This was a quick and dirty implementation .. Have not taken any performance into consideration .

Answer:

LONG_DELAY toast display for 3.5 sec and SHORT_DELAY toast display for 2 sec.

Toast internally use INotificationManager and calls it’s enqueueToast method every time a Toast.show() is called.

Call the show() with SHORT_DELAY twice will enqueue same toast again. it will display for 4 sec ( 2 sec + 2 sec).

similarly, call the show() with LONG_DELAY twice will enqueue same toast again. it will display for 7 sec ( 3.5 sec + 3.5 sec)

Answer:

Here is a custom Toast class I made using the above code:

Answer:

If you need a long Toast, there’s a practical alternative, but it requires your user to click on an OK button to make it go away. You can use an AlertDialog like this:

If you have a long message, chances are, you don’t know how long it will take for your user to read the message, so sometimes it is a good idea to require your user to click on an OK button to continue. In my case, I use this technique when a user clicks on a help icon.

Answer:

As mentioned by others Android Toasts can either be LENGTH_LONG or LENGTH_SHORT. There is no way around this, nor should you follow any of the ‘hacks’ posted.

The purpose of Toasts are to display “non-essential” information and due to their lingering effect, messages may be put far out of context if their duration exceeds a certain threshold. If stock Toasts were modified so that they can display longer than LENGTH_LONG the message would linger on the screen until the application’s process is terminated as toast views are added to the WindowManager and not a ViewGroup in your app. I would assume this is why it is hard coded.

If you absolutely need to show a toast style message longer than three and a half seconds I recommend building a view that gets attached to the Activity’s content, that way it will disappear when the user exits the application. My SuperToasts library deals with this issue and many others, feel free to use it! You would most likely be interested in using SuperActivityToasts

Answer:

Simply use SuperToast to make an elegant toast on any situation. Make your toast colourful. Edit your font color and also it’s size. Hope it will be all in one for you.

Answer:

Why eat Toast, when you can have the entire Snackbar: https://developer.android.com/reference/android/support/design/widget/Snackbar.html

Snackbar > Toast, Custom Toast, Crouton

Answer:

Here is a very simple method which worked for me:

for (int i=0; i < 3; i++)
{
Toast.makeText(this, "MESSAGE", Toast.LENGTH_SHORT).show();
}

The duration of LENGTH_SHORT is 2 seconds and LENGTH_LONG is 3.5 seconds, Here the toast message will be shown for 6 Seconds since it is enclosed in a for loop. But a drawback of this method is after each 2 sec a small fading effect may arise. but it is not much noticeable.
Hope it is helpful

Answer:

The user cannot custome defined the Toast’s duration. because NotificationManagerService’s scheduleTimeoutLocked() function not use the field duration. the source code is the following.

Answer:

Use Crouton, it is a very flexible Toast library.

Crouton

You can use it just like toasts:

or you can even go a little deeper and customise it more, like setting the time to infinite! for example here I want to show a toast message until the user acknowledges it by clicking on it.

Custome Layout that will be inflated for the toast.

Answer:

Toast duration can be hacked using a thread that runs the toast exclusively. This works (runs the toast for 10 secs, modify sleep and ctr to your liking):

Answer:

A toast with custom background and view did the trick for me. I tested it in nexus 7 tablet and I noticed no fadein fadeout animation during looping. Heres the implementation:

Heres the custom textview used in above code:

@drawable/fragment_background is making my toast have rounded corner as in kitkat version. You can add other views too in the file. Any modifications for improvement and comments are encouraged as I am planning to implement this in my live app.

Answer:

Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field:

Answer:

This text will disappear in 5 seconds.

Edit:
As Itai Spector in comment said it will be shown about 3.5 seconds, So use this code:

Answer:

A very simple approach to creating a slightly longer message is as follows:

Note that the above example eliminates the LENGTH_SHORT option to keep the example simple.

You will generally not want to use a Toast message to display messages for very long intervals, as that is not the Toast class’ intended purpose. But there are times when the amount of text you need to display could take the user longer than 3.5 seconds to read, and in that case a slight extension of time (e.g., to 6.5 seconds, as shown above) can, IMO, be useful and consistent with the intended usage.

Answer:

Sets toast to a specific period in milli-seconds:

Answer:

Answer:

After failing with every available solution, I finally had workaround using recursion.

Code:

Answer:

No, and most/all of the hacks listed here no longer work in android 9. But there’s a much better solution: if you message needs to hang around, use a dialog box.

Answer:

A very simple solution to the question. Twice or triple of them will make Toast last longer. It is the only way around.

Answer:

You can set the desired time in milliseconds in the Toast.makeText(); method like this: