34

I am unable to stop a ProgressBar. Its style is ProgressBarStylesmall. How can I start and stop a circular, small ProgressBar?

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
saqibabbasi
  • 423
  • 1
  • 5
  • 9

7 Answers7

20

By default you can make progressBar visible and hide it when not needed like this:

progressBar.setVisibility(View.GONE);

If you want to control it programatically then you can achieve that with:

progressBar.setVisibility(View.VISIBLE); //to show
progressBar.setVisibility(View.GONE); // to hide

You can even go with ProgressDialog:

private ProgressDialog progressDialog;
...

public void showProgressDialog() {
    if (progressDialog == null) {
        progressDialog = new ProgressDialog(PhotographerDetailActivity.this);
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
    }
    progressDialog.setMessage("your message");
    progressDialog.show();
}

public void dismissProgressDialog() {
    if (progressDialog != null ) {
        progressDialog.dismiss();
    }

Hope it will help you.

Riten
  • 2,879
  • 3
  • 24
  • 28
6

You'll need to handle AsyncTasks for this.

/*
 * We set the visibility to true. If it was already visible 
 * there's no need of doing this but we do it for the sake of readability.
 */
final ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar1);
progressBar.setVisibility(View.VISIBLE);
new AsyncTask<Void,Void,Void>(){

//The variables you need to set go here

    @Override
    protected Void doInBackground(final Void... params){
         // Do your loading here. Don't touch any views from here, and then return null
         return null;
    }


    @Override
    protected void onPostExecute(final Void result){
         // Update your views here
         progressBar.setVisibility(View.GONE);
    }
}.execute()
Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90
  • If you're loading a list or something, just create an empty list as a field inside the AsyncTask, fill it during doInBackground, then display it during onPostExecute. – Charlie-Blake Sep 27 '12 at 09:52
  • Why do you make live that complicated and use an async task? This can be done on the main thread... – GabrielWeis Dec 29 '14 at 07:24
  • To set visibility you can use also progressBar.setVisibility(ProgressBar.VISIBLE) to show, progressBar.setVisibility(ProgressBar.INVISIBLE) to hide but place still appear or progressBar.setVisibility(ProgressBar.GONE) to hide minimized. – Stefan Babos Feb 21 '16 at 09:38
4

I don't know if that helps, but you can use a ProgressDialog. Let's say you have a running ProgressDialog named progressDialog you can use progressDialog.dismiss();. Just make sure the instance of the ProgressDialog you are dismissing is the ProgressDialog that is actually showing. You can check it with progressDialog.isShowing();. To start a ProgressDialog:

progressDialog = ProgressDialog
                    .show(myActivityContext,
                            "ProgressDialog Title",
                                "ProgressDialog Body");

By default the ProgressDialog UI is circular just like you need it.

AggelosK
  • 4,313
  • 2
  • 32
  • 37
  • 1
    it shows the dialog and circular inside it,i just want to show circular animate .My question is how to stop circular – saqibabbasi Jul 04 '12 at 16:32
  • I dont understand. You want to stop the progressDialog or change its UI not to be circular? – AggelosK Jul 04 '12 at 18:08
  • In android sdk,their is only circular control,i just want to start and stop it through code.i do not need progressbar dialog – saqibabbasi Jul 04 '12 at 18:26
  • 1
    You can set your progressbar's visibility to invisible or gone through progressbar.setVisibility(View.INVISIBLE); or progressBar.setVisibility(View.GONE); as suggested form a previous comment in your question. – AggelosK Jul 04 '12 at 18:38
4

hide progressbar : progressBar.setVisibility(View.INVISIBLE);

show progressbar : progressBar.setVisibility(View.VISIBLE);

read on GONE VS. INVISIBLE here

Community
  • 1
  • 1
Hojat Modaresi
  • 641
  • 7
  • 10
4

Came across this older topic and noticed how everyone is talking about the ProgressDialog. It should be noted that the ProgressDialog is deprecated and the ProgressBar class should be used instead.

From the android docs:

Avoid ProgressDialog

Android includes another dialog class called ProgressDialog that shows a dialog with a progress bar. This widget is deprecated because it prevents users from interacting with the app while progress is being displayed. If you need to indicate loading or indeterminate progress, you should follow the design guidelines for Progress & Activity and use a ProgressBar in your layout, instead of using ProgressDialog.

MvanDoorn
  • 49
  • 2
2

There are "indetermined" progress bar, which spins and "determined", whih doesn't. Stick to the latter and you're going to be just fine. To make it "determined", add the progress attribute to it's xml element, set

 style="?android:attr/progressBarStyleHorizontal"
        android:indeterminate="false"

Android Circular Determinate ProgressBar

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
RexSplode
  • 1,475
  • 1
  • 16
  • 24
-3

setAlpha value to 1 when task begin and change back to 0 when task finish but setAlpha only for API Level 11 or above

chings228
  • 1,859
  • 24
  • 24