views:

87

answers:

1

Can I use progress bar in android, without the thread?

HERE IS CODE OF MY CURRENT WAY OF IMPLEMENTING PROGRESS DIALOG

            //              Adding Progress bar

            String[][] data;    //Global variable

//called on onCreate() or onItemSelected

            final ProgressDialog myProgressDialog;

            myProgressDialog = ProgressDialog.show(ListingPage.this,"Please Wait", "Loading Date", true);
            new Thread() {
                public void run() {
                    try{

                        setSelected();

                        sleep(5000);
                    } catch (Exception e) { }

                    myProgressDialog.dismiss();
                }
            }.start();


            populateList(Split.splitToTwoDimArray(data));   // populates the list view

HOPE ABOVE HELPS, IF USING THREAD THE LIST IS NOT BEING POPULATED.

A: 

Sure, you can always set the progress manually via

progressBar.setProgress(int progress);

Above question/added code is a bit confusing cause you asked how to use the progress bar without a thread but now in your code you're using a thread. I thought that you initially wanted to avoid.

Anyway, maybe you should use an AsyncTask instead of the Thread, which allows you to modify anything in the main UI thread.

https://sites.google.com/site/androidhowto/how-to-1/create-a-custom-progress-bar-using-asynctask

http://developer.android.com/reference/android/os/AsyncTask.html

Mathias Lin
Thank Mathiasbut can u help me elaborate more, perhaps with an example.as I want to use it in a way, when certain method has finished its work, then the progress bar completes.
kaibuki
@Mathias:I tried below code :int counter =0; while(counter<100) { mybar.setProgress(counter); counter++; } But it didn't show the progress bar :(
kaibuki
In your certain method you need to calculate the percentage of your progress and then set the progress via above mentioned method. If your method counts from 1 to 1000, it would be: for(int i=1;i<=1000;i++) { doStep(1); progressBar.setProgress(i);}. Or do you want to actually set the progress according to the time the system would need to execute your certain method? btw: why you don't want to use a thread?
Mathias Lin
And your try is executed in the UI thread of your activity? Maybe add some code to your questions above.
Mathias Lin
Here is the code :final ProgressDialog myProgressDialog;myProgressDialog = ProgressDialog.show(ListingPage.this,"Please Wait", "Loading Date", true);new Thread() { public void run() { try{ setSelected(); sleep(5000); } catch (Exception e) { } myProgressDialog.dismiss(); }}.start();now my method is putting some data in variable, so after the thread when I want to populate the list with that variable, nothing happens, if I take out thread, then the list successfully populates.
kaibuki
Ok,a bit confusing cause you asked how to use the progress bar without a thread but now in your code you're actually using a thread. I thought that you initially wanted to avoid that. Therefore, see my extended reply above, especially first link should be a helpful example.
Mathias Lin