I have a problem with updating progressbar. Here I make a custom progressbar function like below
ProgressDialog progressBar1;
private int progressBarStatus = 0;
private void customProgressBar(){
progressBar1 = new ProgressDialog(DictionaryActivity.this);
progressBar1.setCancelable(true);
progressBar1.setMessage("Downloading File...");
progressBar1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar1.setMax(100);
progressBar1.setProgress(0);
progressBar1.show();
//reset progress bar status
progressBarStatus = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
Log.d("TAG", "run: pStatus" + " " + pStatus);
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
// call progressBar status data update
progressBarStatus = setProgressBarStatus();
new Handler(Looper.getMainLooper()).post(new Runnable(){
@Override
public void run() {
progressBar1.setProgress(progressBarStatus);
Log.d("TAG", "run: handler: " +progressBarStatus);
}
});
}
// when, file is downloaded 100%,
if (progressBarStatus >= 100) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar1.dismiss();
}
}
}).start();
}
public int setProgressBarStatus(){
return progressBarStatus += 2;
}
Then call it from data downloaded function. When data download complete I insert data to SQLite DB (local dB).
Here before data insert strat progress bar update. But when data stated to insert progress bar not update. How can solve this problem? Thanks, everyone.