Home » Android » android – How to stop and start a ProgressBar

android – How to stop and start a ProgressBar

Posted by: admin April 23, 2020 Leave a comment

Questions:

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

How to&Answers:

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.

Answer:

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

Answer:

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.

Answer:

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

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

read on GONE VS. INVISIBLE here

Answer:

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.

Answer:

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

Answer:

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