If you just want to update the ProgressBar and as we said in previous comments, you need to do it in another thread, look at this dummy example based in the official Android doc:
package com.example.progressbar;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private ProgressBar mProgress;
private static int mProgressStatus = 0;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgress = (ProgressBar) findViewById(R.id.rewards_progress);
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < 100) {
doWork();
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
}
}).start();
}
private void doWork(){
try {
Thread.sleep(100);
mProgressStatus+=2;
} catch (InterruptedException e) {
}
}
}
However, if you want to animate the ProgressBar update effect, there are several ways to achieve it. One particular way described in this post looks and works pretty good could be easily done by:
package com.example.progressbar;
import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private ProgressBar mProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgress = (ProgressBar) findViewById(R.id.rewards_progress);
ProgressBarAnimation anim = new ProgressBarAnimation(mProgress, 0, 100);
anim.setDuration(5000);
mProgress.startAnimation(anim);
}
public class ProgressBarAnimation extends Animation{
private ProgressBar progressBar;
private float from;
private float to;
public ProgressBarAnimation(ProgressBar progressBar, float from, float to) {
super();
this.progressBar = progressBar;
this.from = from;
this.to = to;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
float value = from + (to - from) * interpolatedTime;
progressBar.setProgress((int) value);
}
}
}
Note that you should customize ProgressBarAnimation
's parameters from
and to
in order to apply it in a realistic approach.