Android - Hide all shown Toast Messages
How do I remove all toast messages currently displayed?
In my App, there is a list, when a user clicks on an item, a toast message is displayed, 10 items - 10 toast messages.
So if the user clicks 10 times, then presses the menu button, they have to wait for some seconds until they're able to read the menu option text.
It shouldn't be like that :)
My solution was to initialize a single Toast in the activity. Then changing its text on each click.
Toast mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
if (a) {
mToast.setText("This is a");
mToast.show();
} else if (b) {
mToast.setText("This is b");
mToast.show();
}
how do I disable all toast messages being process currently?
You can cancel individual Toasts
by calling cancel()
on the Toast
object. AFAIK, there is no way for you to cancel all outstanding Toasts
, though.
What about checking if a toast is already being displayed?
private Toast toast;
...
void showToast() {
if (toast == null || toast.getView().getWindowVisibility() != View.VISIBLE) {
toast = Toast.makeText(getActivity(), "Toast!", Toast.LENGTH_LONG);
toast.show();
}
}
Mudar's solution worked beautifully for me on a similar problem - I had various toasts stacking up in a backlog after multiple button
clicks.
One instance of Toast with different setText()s
and show()
s was the exactly the answer I was looking for - previous message cancelled as soon as a new button is clicked. Spot on
Just for reference, here's what I did...
In OnCreate
:
final Toast myToast = Toast.makeText(getBaseContext(), "", Toast.LENGTH_SHORT);
Within in each OnClick
:
myToast.setText(R.string.toast1);
myToast.show();