Search code examples
javaandroidprogress

How to start and stop progressbar in android?


I have an activity that calls a second java class. I want after the second class is called to show a progressbar and then return to normal activity execution. I found some other threads but i couldn't make the progressbar to stop.


Solution

  • There's a full example over here.

    Quote:

    Declare your progress dialog:

    ProgressDialog progress;
    

    When you're ready to start the progress dialog:

    progress = ProgressDialog.show(this, "dialog title",
        "dialog message", true);
    

    and to make it go away when you're done:

    progress.dismiss();
    

    Here's a little thread example for you:

    // Note: declare ProgressDialog progress as a field in your class.
    
    progress = ProgressDialog.show(this, "dialog title",
      "dialog message", true);
    
    new Thread(new Runnable() {
      @Override
      public void run()
      {
        // do the thing that takes a long time
    
        runOnUiThread(new Runnable() {
          @Override
          public void run()
          {
            progress.dismiss();
          }
        });
      }
    }).start();
    

    ProgressDialog is deprecated, so you might want to use a ProgressBar.

    I've found this post about deleting one of them.

    Well, I think this is rather ridiculous, but here is how I fixed it.

    In my xml for the ProgressBar, I added android:visibility="gone" to hide it by default. Then, in my code, I first told it to display (View.VISIBLE) before it tried getting the server list, then I told it to hide (View.GONE) after it was done. This worked (I could see the progress indicator while the data loaded, then it went away). So I suppose I couldn't get it to hide in the code because the code is not what forced it to be visible to begin with... That seems like a bug to me.