Can an Android Toast Be Longer Than Toast.Length_Long

Can an Android Toast be longer than Toast.LENGTH_LONG?

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.

What is the duration of a Toast LENGTH_LONG and LENGTH_SHORT

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

What is the value of Toast.LENGTH_LONG and Toast.LENGTH_SHORT?

There is another question that answers what you are looking for. The answers are:

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

This was courtesy of FeelGood. You can find the whole thread below.

Can an Android Toast be longer than Toast.LENGTH_LONG?

Hope this helps.

how to increase time of toast msg

If you want to display all toasts one by one, then you need to create a new class and write your own logic. I can give you a solution.

First create a new class as below.

ToastManager.java

class ToastManager {
private final WeakReference<Context> mContext;
private final Handler uiHandler = new Handler(Looper.getMainLooper());

private final List<Item> items = new ArrayList<>();
private int durationInMillis;
private boolean isShowing;
private int delayedBetweenToastInMillis;

public ToastManager(Context context) {
mContext = new WeakReference<>(context);
}

public void addToast(String message, @NonNull Duration duration) {
Item item = new Item(message, duration);
items.add(item);
}

public void show() {
// Prevent client from calling this method many time.
if (isShowing) {
return;
}

// Show all toast on screen.
showToast();

// After calling show(), if client add new toasts by calling addToast()
// Then we must show them on screen. Otherwise reset all data of this class.
uiHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (!items.isEmpty()) {
showToast();
} else {
reset();
}
}
}, durationInMillis);
}

public void setDelayedBetweenToast(int delayInMillis) {
delayedBetweenToastInMillis = delayInMillis;
}

public void cancel() {
reset();
uiHandler.removeCallbacksAndMessages(null);
}

private void showToast() {
List<Item> list = new ArrayList<>(items);
items.clear();

durationInMillis = 0;
for (Item item : list) {
uiHandler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext.get(), item.text, item.getDurationForToast()).show();
}
}, durationInMillis);
durationInMillis += item.getDurationInMillis() + delayedBetweenToastInMillis;
}
}

private void reset() {
items.clear();
durationInMillis = 0;
isShowing = false;
}

private static class Item {
String text;
Duration duration;

Item(String text, Duration duration) {
this.text = text;
this.duration = duration;
}

int getDurationForToast() {
return duration == Duration.LENGTH_SHORT ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG;
}

int getDurationInMillis() {
return duration == Duration.LENGTH_SHORT ? 2000 : 3500;
}
}

enum Duration {
LENGTH_SHORT,
LENGTH_LONG
}
}

Then use it from your class.

NewUserActivity.java

// Create a new instance of ToastManager
ToastManager toastManager = new ToastManager(NewUserActivity.this);

// NAME VALIDATION
if (NAME.isEmpty()) {
toastManager.addToast("Plz Enter Name", ToastManager.Duration.LENGTH_SHORT);
} else if (!((NAME.length() > 3) && (NAME.length() < 15))) {
toastManager.addToast("Name > 3 and < 15", ToastManager.Duration.LENGTH_SHORT);
} else if (!NAME.matches("[a-zA-Z ]+")) {
toastManager.addToast("Only enter alphabets", ToastManager.Duration.LENGTH_SHORT);
}

//EMAIL VALIDATION
if (EMAIL.isEmpty()) {
toastManager.addToast("Plz Enter Email", ToastManager.Duration.LENGTH_SHORT);
} else if (!(EMAIL.matches(emailPattern))) {
toastManager.addToast("Invalid Email", ToastManager.Duration.LENGTH_SHORT);
}

//PHONE NUMBER VALIDATION
if (PHONENO.isEmpty()) {
toastManager.addToast("Plz Enter Phone no.", ToastManager.Duration.LENGTH_SHORT);
} else if (!(PHONENO.length() == 10)) {
toastManager.addToast("Invalid Phone no.", ToastManager.Duration.LENGTH_SHORT);
} else if (!(PHONENO.matches(phonePattern))) {
toastManager.addToast("Invalid Phone Number", ToastManager.Duration.LENGTH_SHORT);
}

//USERNAME VALIDATION
if (username.isEmpty()) {
toastManager.addToast("Plz Enter Username", ToastManager.Duration.LENGTH_SHORT);
} else if (!((username.length() > 6) && (username.length() < 15))) {
toastManager.addToast("Plz Enter Username", ToastManager.Duration.LENGTH_SHORT);
}

//PASSWORD VALIDATION
if (password.isEmpty()) {
toastManager.addToast("Plz Enter Password", ToastManager.Duration.LENGTH_SHORT);
} else if (!((password.length() > 6) && (password.length() < 15))) {
toastManager.addToast("Password > 6 and < 15", ToastManager.Duration.LENGTH_SHORT);
}

// When one or all Edit Text are blank
toastManager.addToast("All fields are compulsory", ToastManager.Duration.LENGTH_SHORT);

// Finally show all toast all screen
toastManager.show();

If you want to set extra time between toast:

toastManager.setDelayedBetweenToast(1000); // 1 second 

If you don't want the toast still show when the activity is no longer visible (usually put this line onStop() method).

@Override
protected void onStop() {
toastManager.cancel();
super.onStop();
}

Set Android Toast duration to be really long (e.g., 1 minute)

Since LENGTH_SHORT is 2 seconds (and LENGTH_LONG is 3.5 seconds), try this:

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

How can I show a toast for a specific duration?

This cannot be done. To show a toast for a length shorter than Toast.LENGTH_SHORT, you must cancel it after the time you want. Something like:

final Toast toast = Toast.makeText(getApplicationContext(), "This message will disappear in half a second", Toast.LENGTH_SHORT);
toast.show();

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 500);

How to set Toast display time less than Toast.LENGTH_SHORT

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

Set Toast Appear Length

I found a solution to this by calling toast.cancel() after a certain delay that is shorter than the standard toast duration.

        final Toast toast = Toast.makeText(ctx, "This message will disappear in 1 second", Toast.LENGTH_SHORT);
toast.show();

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 1000);


Related Topics



Leave a reply



Submit