How to update the progress bar using Thread

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

AndroidVogue

+3
source share
5 answers

I performed a similar task using AsyncTask. AsyncTaskhas a method onProgressUpdate(Integer)that you can call every iteration, for example, or every time that progress is made while doInBackground()calling publishProgress().

See docs for more details .

+3
source
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
                      }

                 }
        };
+2
source
  // 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);
                } 
+1

Use a handler, this handler will update the progress bar; you need to do the following: 1) send a message to the handler from you 2) update the progress bar in the handler

0
source
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();
}
0
source

All Articles