TechnocraT
TechnocraT

Reputation: 313

update progressbar in android

I am starting a thread in my activity and immediately displaying progress dialog.The method in this thread after completing its task calls other thread which calls m_progressDialog.dismiss(); method.

This works fine but i want to implement progress bar now how does this bar fills according to completion of task in previous thread.It takes indefinite amount of time for completing its operations.

this could sound ambigious but iam ready to elaborate it further if required.

A little code snippet:

Method in first thread ends like this

...
runOnUiThread(m_returnRes);
}

private Runnable m_returnRes = new Runnable() {

    public void run() {
        m_progressDialog.dismiss();

Also i've tried to do it this way

m_progressDialog = new ProgressDialog(this);
        m_progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        m_progressDialog.setProgress(0);
        m_progressDialog.setMax(100);
        m_progressDialog.setMessage("Retrieving data..");
        m_progressDialog.show();

    Thread background = new Thread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub

            try {while(m_progressDialog.getProgress()<=100){
                Thread.sleep(500);}
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    });

    background.start();

but the bar does not gets filled and gets dismissed directly after .dismiss() method is called

Upvotes: 0

Views: 315

Answers (1)

Jakub Szczygieł
Jakub Szczygieł

Reputation: 1232

Try using AsyncTask. It handles threding for itself and has function publishprogress inside the onProgressUpdate to change view content, where you could update progress bar.

Upvotes: 3

Related Questions