kabuto178
kabuto178

Reputation: 3167

Enabling and update progressbar from a thread

Progressbar will not show when I try to access it from a thread nor update the progress. I would prefer to just have a progress spinner be displayed until the network activity is complete, and this network activity is just to post some text data to the server.This is the code I have so far, any help is appreciated thanks,

 Thread th = new Thread(){
        public void run() {

                     try {
                        sleep(5000);

                         while(cnt < 5000){
                             activty.this.runOnUiThread(new Runnable(){
                                 public void run()
                                 {
                                     ProgressBar pd = (ProgressBar)findViewById(R.id.progressBar1);
                                    pd.setVisibility(0);
                                    pd.setMax(100);

                                     pd.setProgress(cnt);
                                    cnt += 100;
                                 }
                             });
                         }
                     } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
        }

        int cnt = 100;
    };
    th.start();
    `

Upvotes: 0

Views: 80

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 200110

This is exactly the case where you would want to use AsyncTask, which processes data in a separate thread and contains methods publishProgress and onProgressUpdate to update your UI on the UI thread.

The Processes and Threads Guide has an example of how to implement an AsyncTask as well.

Upvotes: 2

Related Questions