Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've tried a variety of things to do this. At first I tried using the <code>cancel()</code>, which had no effect for me (see also <a href="https://stackoverflow.com/a/7235755/1276636">this answer</a>).</p> <p>With <code>setDuration(n)</code> I wasn't coming to anywhere either. It turned out by logging <code>getDuration()</code> that it carries a value of 0 (if <code>makeText()</code>'s parameter was <code>Toast.LENGTH_SHORT</code>) or 1 (if <code>makeText()</code>'s parameter was <code>Toast.LENGTH_LONG</code>).</p> <p>Finally I tried to check if the toast's view <code>isShown()</code>. 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, <code>isShown()</code> returns true if a toast is displayed. Utilizing <code>isShown()</code> I came up with the method:</p> <pre><code> /** * &lt;strong&gt;public void showAToast (String st)&lt;/strong&gt;&lt;/br&gt; * this little method displays a toast on the screen.&lt;/br&gt; * it checks if a toast is currently visible&lt;/br&gt; * if so &lt;/br&gt; * ... it "sets" the new text&lt;/br&gt; * else&lt;/br&gt; * ... it "makes" the new text&lt;/br&gt; * and "shows" either or * @param st the string to be toasted */ public void showAToast (String st){ //"Toast toast" is declared in the class try{ toast.getView().isShown(); // true if visible toast.setText(st); } catch (Exception e) { // invisible if exception toast = Toast.makeText(theContext, st, toastDuration); } toast.show(); //finally display it } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload