Android Progressbar not updating

I am having a problem updating the progress bar. I am updating the progress bar in a separate thread and a variable in which the progressbar progress depends (which is a class variable), updating in another thread. Thus, the execution dialog shows, but always 0%, without updating it. Help me please.

public void setProgressbar() { progressBar = new ProgressDialog(context); progressBar.setCancelable(true); progressBar.setMessage("File downloading ..."); progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressBar.setProgress(0); progressBar.setMax(100); progressBar.show(); Thread thread = new Thread() { public void run() { while(progressBar.getProgress() < 100) { Log.v("progressbar", getProgress()+""); progressBar.setProgress(getProgress()); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; thread.start(); 

Refresh Value Code

  Thread thread = new Thread() { public void run() { . . . while((bytesRead = is.read(bytearray, 0, bytearray.length)) != -1) { bos.write(bytearray, 0, bytesRead); totaldownload = totaldownload + bytesRead; Log.v("downloadign ", totaldownload+""); // progressBar.setProgress((int) ((totaldownload/sizeoffile) * 100)); } bos.close(); . . . }; thread.start() 

And the getPrgoress method

  public int getProgress() { return (int) ((totaldownload/sizeoffile) * 100); } 
0
source share
5 answers

You update the inisde progress bar when the thread starts. you cannot update ui from the stream. You need to update ui in ui stream. Use runOnUiThread . runOnUiThread is an activity class method.

Or use Asynctask.

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

In asynctask doInBackground you can call publishProgress(Progress...) to publish one or more progress units. These values ​​are published in the user interface stream in step onProgressUpdate(Progress...) .

+3
source

This is a mistake in the ProgressBar!

It seems that setProgress(...) does not start the update in drawable if the same value is passed again. But this did not work during setMax . Therefore, there is no update.

To solve this problem, I just do bar.setProgress(0) before each update ... this is just a workaround, but it works for me, as expected:

 bar.setProgress(0); // call these two methods before setting progress. bar.setMax(20); bar.setProgress(20); 

The second option.

 mSeekBar.post(new Runnable() { @Override public void run() { mSeekBar.setProgress(percentOfFullVolume); } }); 

It may also work for someone.

+2
source

Use AsyncTask , there is also an example in this document to answer your needs.

This code from AsyncTask actually does what you want to execute.

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { protected Long doInBackground(URL... urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); // Escape early if cancel() is called if (isCancelled()) break; } return totalSize; } protected void onProgressUpdate(Integer... progress) { setProgressPercent(progress[0]); } protected void onPostExecute(Long result) { showDialog("Downloaded " + result + " bytes"); } } 

The only thing you need to change is setProgressPercent () for your method name to change the value of the execution line. You can remove the onPostExecute AFAIK method if you do not need it. Try using AsyncTask instead of java Thread, since AsyncTask provides methods already for your need, while you need to write them if you use Thread instead.

+1
source

There is something wrong with your logic. Change your stream to

 Thread thread = new Thread() { public void run() { int prog = 0; while(prog < 100) { progressBar.setProgress(prog); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } prog ++; } } }; thread.start(); 
+1
source

Try it.

declare this variable in Global.

 int delay = 100; int period = 2000; 

After calling the function where you need it.

 public void setProgressbar() { progressBar = new ProgressDialog(context); progressBar.setCancelable(true); progressBar.setMessage("File downloading ..."); progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressBar.setProgress(0); progressBar.setMax(100); progressBar.show(); final Handler mhandler = new Handler(); final Runnable mRunnable = new Runnable() { @Override public void run() { progressBar.setProgress(progress); progress++; } }; final Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { mhandler.post(mRunnable); } }, delay, period); } 
0
source

Source: https://habr.com/ru/post/952401/


All Articles