10

I want to use a progressbar on my screen instead of progressDialog.

I have inserted a progressBar on my XML-view file, and I want to make it show when it loads and disable it when not loading.

So I was using visible, but it takes place, so rest of the data comes down.

How should I use progressbar in a asynctask? How can I show and hide it?

1

3 Answers 3

26

Here is a most exhaustive example:

public class ScreenSplash extends Activity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_splash);

        final ProgressBar progress = (ProgressBar)findViewById(R.id.progress);
        final TextView    textview = (TextView)findViewById(R.id.text);

        new MyWorker(this, progress, textview).execute();
    }
}


final class MyWorker extends AsyncTask<Void, Integer, Void> {
    private static final int titles[] = {R.string.splash_load_timezone,
                                         R.string.splash_load_memory,
                                         R.string.splash_load_genres,
                                         R.string.splash_load_channels,
                                         R.string.splash_load_content};
    private static final int progr[]  = {30, 15, 20, 25, 20};

    private int index;

    private final Activity parent;
    private final ProgressBar progress;
    private final TextView textview;

    public MyWorker(final Activity parent, final ProgressBar progress, final TextView textview) {
        this.parent = parent;
        this.progress = progress;
        this.textview = textview;
    }

    @Override
    protected void onPreExecute() {
        int max = 0;
        for (final int p : progr) {
            max += p;
        }
        progress.setMax(max);
        index = 0;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected Void doInBackground(final Void... params) {
        /* Load timezone. this is very slow - may take up to 3 seconds. */
          ...
        publishProgress();

        /* Get more free memory. */
          ...
        publishProgress();

        /* Load channels map. */
          ...
        publishProgress();

        /* Load genre names. */
          ...
        publishProgress();


        /* Preload the 1st screen's content. */
          ...
        publishProgress();

        return null;
    }

    @Override
    protected void onProgressUpdate(final Integer... values) {
        textview.setText(titles[index]);
        progress.incrementProgressBy(progr[index]);
        ++index;
    }

    @Override
    protected void onPostExecute(final Void result) {
        parent.finish();
    }
}

For shownig/hiding your progress bar use progress.setVisibility(View.VISIBLE) and progress.setVisibility(View.GONE). There is also View.INVISIBLE constant; its difference from GONE is that your progress bar is not drawn but still occupies its space (usefull for some layouts).

6
  • 3
    As with many examples in this area, it doesn't account for the possiblity of the Activity being destroyed (and maybe created anew) while the task is running... Nov 7, 2010 at 22:41
  • Yes, that's true. There is a good post regarding this problem: stackoverflow.com/questions/3357477/…
    – JBM
    Nov 8, 2010 at 8:57
  • 2
    In fact, one must kill their bg tasks in onDestroy. Activity guidelines say that after Activity is destroyed there should be nothing left running in the background (otherwise you drain phone's battery). Therefore you should compulsory finish your AsyncTasks even if it's just the user flipped the phone and start all over in onCreate - stupid, yeah, but what can we do if Google has put the Activity lifecycle in this way?
    – JBM
    Nov 8, 2010 at 9:15
  • private static final int progr[] = {30, 15, 20, 25, 20}; what is progr[] .. explain it please.. how.. May 16, 2014 at 6:38
  • 1
    @fcracker79, yes, rotation is not supported in this example. In fact, using AsyncTask is a bad idea in general. My experience so far has proved that decoupling the logic from the UI gives best results. In such approach, the logic may do the job without any concerns about Activity life cycle, and the UI may display results once it's ready.
    – JBM
    Oct 18, 2015 at 6:50
4

As for using A Progress bar from async task you can use the PostPrgressUpdate() from your doInBackground(int Progress) and in the OnProgressUpdate()method update the ProgressBar Accordingly. As for the Show and hide of the bar I was not able to understand your question ( or the problem Please Revise)

-1

in xml use

<ProgressBar
        android:id="@+id/progress_bar"
        style="@android:style/Widget.ProgressBar.Small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

then in Asyntask

 ProgressBar mProgress=(ProgressBar) findViewById(R.id.progress_bar);

 @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        mProgress.setVisibility(View.GONE);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.