2

I have a search function (using a web service) which takes a while to load data to my inflated layout. I want to show a ProgressBar untill the data is fetched from the web service. This is a small time interval and hence I don't to use a ProgressDialog for this. I've gone through many posts and dev guide but I'm not able to figure out a way to use that in my app. I've a Spinner in my layout. Once I choose an item from the spinner drop down I pass the string to the search function which fetches as well as loads data into the inflated layout.

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        Selection = spinner.getSelectedItem().toString();           

        search(Selection);
    }

I created a ProgressBar, handler as explained in the dev guide.

private ProgressBar mProgress =  new ProgressBar(SearchActivity.this, null, android.R.attr.progressBarStyleSmall);
private int mProgressStatus = 0;

private Handler mHandler = new Handler();

And say this is a new thread used to display the ProgressBar

new Thread(new Runnable() {
         public void run() {
             while (mProgressStatus < 100) {
                 mProgressStatus = doWork();

                 // Update the progress bar
                 mHandler.post(new Runnable() {
                     public void run() {
                         mProgress.setProgress(mProgressStatus);
                     }
                 });
             }
         }
     }).start();

How do I track progress of my search function to show the ProgressBar?

Thank you for reading

Harsh
  • 487
  • 3
  • 9
  • 29

2 Answers2

2

You should use a AsyncTask instead of a thread. A asyncTask let you to have a preexecute, postexecute and publishprogress which run on UI thread, and a doInBackground which run in background. There is a lot of tuts on google.

Try this one.

AMerle
  • 4,354
  • 1
  • 28
  • 43
  • I don't mind using `AsyncTask` but I don't want to a `ProgresDialog`. Can we show a `ProgressBar` using `AsyncTask`? – Harsh Jul 24 '12 at 20:49
  • @Harsh Of course and it's easier with asynctasks. just look at the tut I linked in my answer – AMerle Jul 24 '12 at 20:50
  • I didn't see any tutorials on how to show `ProgressBars` using `AsyncTask`. most of the headings say `ProgressBar` but they end up showing a `ProgressDialog` – Harsh Jul 24 '12 at 20:51
  • @Harsh but it's the same! just use mProgressBar.setProgress(progress); – AMerle Jul 24 '12 at 20:54
  • Okay. What's the value of progress that I am setting here? I'm sorry but I'm quite new to this. – Harsh Jul 24 '12 at 20:57
  • @Harsh Follow the tut, and set the progress of your progressBar instead of the progress of the dialog – AMerle Jul 24 '12 at 21:05
1

Use an AsyncTask instead of a thread. It's way easier to use and provides a built-in mechanism to be notified of progress updates.

Look at this question for a complete sample :

Download a file with Android, and showing the progress in a ProgressDialog

Community
  • 1
  • 1
Dalmas
  • 26,409
  • 9
  • 67
  • 80
  • I don't mind using AsyncTask but I don't want to a ProgresDialog. Can we show a ProgressBar using AsyncTask? – Harsh Jul 24 '12 at 20:49
  • This code give the general idea, it's not hard to change it to update a `ProgressBar` instead. For example in `onPreExecute()` you can set the ProgressBar to 0%, then on each call to `onProgressUpdate()` you change its value to the specified amount. – Dalmas Jul 24 '12 at 20:53
  • All the tutorials have used `ProgressDialogs` I don't see any with `ProgressBars`. As far as I've learned `ProgressBar` requires progress to be tracked. You can't show a `ProgressBar` simply by using `.show()` like we use in `ProgressDialog` right? – Harsh Jul 24 '12 at 20:55
  • No, just define it in your layout and you're done. You don't have to show/hide it like a `ProgressDialog`. – Dalmas Jul 24 '12 at 21:09