user744881
user744881

Reputation:

How to Update Progress Bar using Thread

How to update progress bar using Background thread in android? It would also change the progress in progress bar. Please help.

AndroidVogue

Upvotes: 4

Views: 7271

Answers (6)

Hitesh Kushwah
Hitesh Kushwah

Reputation: 1361

Using Thread you can do it like this just call the function in oncreate method

public void loadProgressbar()
{
    progressBar.setMax(100);
    progressBar.setProgress(0);

    new Thread(new Runnable() {
        @Override
        public void run() {
            for (progress = 0; progress <= 100; progress++) {

                handler.post(new Runnable() {
                    public void run() {
                        progressBar.setProgress(progress);
                    }
                });


                try {
                    Thread.sleep(50);
                } catch (Exception ex) {
                }
            }
        }
    }).start();
}

Upvotes: 1

prashant00101
prashant00101

Reputation: 141

private ProgressBar pgb;
private TextView textview;
private int progressBarStatus = 0;
private Handler progressBarbHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);
    pgb = (ProgressBar) findViewById(R.id.progressBar2) ;
}

public void load_bar(View view)
{
    pgb.setMax(100);
    pgb.setProgress(0);

    new Thread(new Runnable() {
        @Override
        public void run() {
            for (progressBarStatus = 0; progressBarStatus <= 100; progressBarStatus++) {

                progressBarbHandler.post(new Runnable() {
                    public void run() {
                        pgb.setProgress(progressBarStatus);
                    }
                });


                try {
                    Thread.sleep(50);
                } catch (Exception ex) {
                }
            }
        }
    }).start();
}

Upvotes: 1

prashant00101
prashant00101

Reputation: 141

  // this is demonstrate how to update progress bar from a thread 
  private ProgressBar pgb;  //  is a progressBar named with pgb 
  private int progressBarStatus = 0;// is a variable named progressBarStatus and initialize by 0
  // in  onCreate  method find the ui component view horizontal prograssbar named progressBar2  


 public void load_bar(View view) // is a button OnClick
// and i am using a thread with for loop to demonstrate a task and variable progressBarStatus  as status 

  pgb = (ProgressBar) findViewById(R.id.progressBar2) ;// find view by id 

  pgb.setMax(100); // at this point we the maximum value of a progress bar to make it full like we make 100 pices of it 

  pgb.setProgress(0); // initialize the progress bar with 0 % progress

    // progress bar handler that update the status of progress bar . without handler we cant update progress bar from thread 

   progressBarbHandler.post(new Runnable() {
                public void run() {
                    pgb.setProgress(progressBarStatus);
                } 

Upvotes: 2

user456118
user456118

Reputation:

I have done similar task using AsyncTask. AsyncTask has method onProgressUpdate(Integer) that you can call each iteration for example or each time a progress is done during doInBackground() by calling publishProgress().

Refer to the docs for more details.

Upvotes: 3

Kakey
Kakey

Reputation: 4024

Thread t=new Thread()
{
public void run()
{
while(some condition)
{
sleep(1000);
Message myMessage = new Message();
myMessage.obj = "success";
handler.sendMessage(myMessage);
}
}
};


private Handler handler=new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            String result=(String)msg.obj;
                     if(result.equals("success")
                     {
                     //update progress bar here
                      }

                 }
        };

Upvotes: 2

Jaiprakash Soni
Jaiprakash Soni

Reputation: 4112

Use handler, this handler will update the progress bar you have to do is: 1)send message to handler from you thread 2)update progress bar in handler

Upvotes: 1

Related Questions