title: Android ProgressBar: visibility not behaving
body: <p>I'm having trouble understanding how the visibility of a ProgressBar works. Basically my ProgressBar won't appear or disappear when I need it to.
  My code is too big to show all here so I have created a new simple demo project with a button to click:</p>
  <pre><code>    fab.setOnClickListener(new View.OnClickListener()
      {
          @Override
          public void onClick(View view)
          {
              Snackbar.make(view, &quot;Replace with your own action&quot;, Snackbar.LENGTH_LONG)
                      .setAction(&quot;Action&quot;, null).show();
              mProgressBar.setVisibility(View.VISIBLE);
              SystemClock.sleep(1000);
              mProgressBar.setVisibility(View.GONE);
              SystemClock.sleep(1000);
              mProgressBar.setVisibility(View.VISIBLE);
              SystemClock.sleep(1000);
              mProgressBar.setVisibility(View.GONE);
              SystemClock.sleep(1000);
              mProgressBar.setVisibility(View.VISIBLE);
           }
       }
   mProgressBar = findViewById(R.id.progressbar);
   mProgressBar.setVisibility(View.INVISIBLE);
  </code></pre>
  <p>I have also tried several versions of the Progressbar; currently it's this:</p>
  <pre><code>&lt;ProgressBar
      android:id=&quot;@+id/progressbar&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:layout_centerVertical=&quot;true&quot;
      android:backgroundTint=&quot;@android:color/white&quot;
      android:gravity=&quot;center&quot;
      android:indeterminate=&quot;true&quot;
      android:indeterminateTint=&quot;#1a09d6&quot;/&gt;
  </code></pre>
  <p>So, what happens is that the ProgressBar doesn't turn on and off; it just ends up with whatever is the last state. And the SnackBar doesn't appear either, until after all of the sleeps.
  If I could understand how to make this snippet work as I want, then I think would have a solution to the (slightly more complex) situation in my actual code.
  I have read other threads and understand that a ProgressBar can't be shown or updated while the main thread is too busy. I have tried, as others have suggested, using</p>
  <pre><code>runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      mProgressBar.setVisibility(View.GONE);
                  }
              })
  </code></pre>
  <p>but this makes no difference. I have also tried <em>View.Invalidate()</em> between the visibility changes, but to no effect. And I've tried 'INVISIBLE' instead of 'GONE'.
  Clearly I'm missing something fundamental about tasks and threading here! Help please.</p>