I have the following code I would like to use to increment my progress bar slowly for 20 seconds
public void progressAnimator(){
final long period = 1000;
timer=new Timer();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//this repeats every 100 ms
if (counter<100){
runOnUiThread(new Runnable() {
@Override
public void run() {
loaderLabel.setText(String.valueOf(counter)+"%");
}
});
mProgress.setProgress(counter);
counter++;
}
else{
//closing the timer
timer.cancel();
Intent intent =new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
// close this activity
finish();
}
}
}, period);
}
My problem is that this only ends up indicating 0% on the loaderLabel and then freezes without doing anything. I had earlier tried this code but it only blinks 100% on the loaderLabel and fills the progress bar and then progresses on to the next window.
public void progressAnimator(){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
for( counter =1; counter<=100; counter ++) {
System.out.println(counter);
mProgress.setProgress(counter);
loaderLabel.setText(getResources().getString(R.string.loading) + " " + counter + " " + getResources().getString(R.string.percentSymbol));
if (counter == 100) {
Toast.makeText(SplashActivity.this, R.string.welcome, Toast.LENGTH_LONG).show();
Intent loadMain = new Intent(SplashActivity.this, MainActivity.class);
startActivity(loadMain);
finish();
}
}
}
}, 100);
}
If I increase the delay to 20,000 then it freezes at zero, What could I be doing wrong?
Based on the answer given by Kumar Harsh, I would suggest you do the following
That should solve your problem note that I multiply by 5 to get the progress to 100 otherwise it will stop at 20