I have several SeekBar
and onSeekBarProgressStop()
, I want to show a Toast
message.
But if on SeekBar
I perform the action rapidly then UI thread somehow blocks and Toast
message waits till UI thread is free.
Now my concern is to avoid the new Toast
message if the Toast
message is already displaying. Or is their any condition by which we check that UI thread is currently free then I'll show the Toast
message.
I tried it in both way, by using runOnUIThread()
and also creating new Handler
.
Check for showing toast message on screen either it is displayed or not. For Showing a toast message Make a separate class. And use the method of this class which display the toast message after checking the visibility of the toast message. Use This Snippet of code:
I hope this solution will help you.
Thanks
The following is an alternative solution to the most popular answer, without the try/catch.
keep track of the last time you showed the toast, and make re-showing it a no-op if it falls within some interval.
and then,
this isn't perfect, since the length of
LENGTH_SHORT
andLENGTH_LONG
are undefined, but it works well in practice. it has the advantage over other solutions that you don't need to hold on to the Toast object and the call syntax remains terse.I've tried a variety of things to do this. At first I tried using the
cancel()
, which had no effect for me (see also this answer).With
setDuration(n)
I wasn't coming to anywhere either. It turned out by logginggetDuration()
that it carries a value of 0 (ifmakeText()
's parameter wasToast.LENGTH_SHORT
) or 1 (ifmakeText()
's parameter wasToast.LENGTH_LONG
).Finally I tried to check if the toast's view
isShown()
. Of course it isn't if no toast is shown, but even more, it returns a fatal error in this case. So I needed to try and catch the error. Now,isShown()
returns true if a toast is displayed. UtilizingisShown()
I came up with the method:The enhanced function from above thread, which will show toast only if not visible with same text message:
added timer to remove the toast after 2 seconds.