I am using Toast in my app. When I am pressing a button, it is showing a Toast. My problem is that the second time I'm pressing on the button the second toast is "waiting" for the first one to end and only than it shows.. I want the current one to show immediately and not wait. This is my simple code:
toast = Toast.makeText(getApplicationContext(), "Press Back to retorn to the main page", Toast.LENGTH_SHORT);
toast.show();
how can I do that?
How do you avoid a toast if there's one toast already being shown? It turned out by logging getDuration() that it carries a value of 0 (if makeText() 's parameter was Toast. LENGTH_SHORT ) or 1 (if makeText() 's parameter was Toast.
You can use toast. cancel() befor showing next toast. cancelling won't reduce the toast time.
Toast. makeText returns a Toast object. Call cancel() on this object to cancel it.
You can always cancel a Toast object.
final Toast tst = Toast.makeText(ctx, "This is a toast.", Toast.LENGTH_SHORT);
tst.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
tst.cancel();
tst.setText("Same toast with another message.");
tst.show();
}
}, 1000);
So instead of creating another Toast object you can use the first one, cancel it, set the new text and show it again.
In https://stackoverflow.com/a/4485531/517561, the writer didn't cancel the toast, they simply changed its text.
Cancel your original Toast
, set a new message and show again the Toast
message.
Toast mytoast;
mytoast = Toast.makeText(this, "Hi Ho Jorgesys! ", Toast.LENGTH_LONG);
mytoast.show();
....
....
....
if(CancelToast){
mytoast.cancel(); //cancelling old Toast!
mytoast = Toast.makeText(this, "Same toast with another message.", Toast.LENGTH_LONG); //Setting a new message.
mytoast.show(); //Show the new message!.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With