saqibabbasi
saqibabbasi

Reputation: 423

How to stop and start a ProgressBar

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

Upvotes: 34

Views: 67676

Answers (7)

RexSplode
RexSplode

Reputation: 1505

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

Upvotes: 2

MvanDoorn
MvanDoorn

Reputation: 49

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.

Upvotes: 4

Riten
Riten

Reputation: 2959

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.

Upvotes: 20

Hojat Modaresi
Hojat Modaresi

Reputation: 771

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

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

read on GONE VS. INVISIBLE here

Upvotes: 4

chings228
chings228

Reputation: 1889

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

Upvotes: -3

Charlie-Blake
Charlie-Blake

Reputation: 11050

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()

Upvotes: 6

AggelosK
AggelosK

Reputation: 4351

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.

Upvotes: 4

Related Questions