Bootstrap Progress bar not incrementing in android

128 Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

Based on the answer given by Kumar Harsh, I would suggest you do the following

 public void progressAnimator(){
    new CountDownTimer(total_duration, duration_of_one_step) {
        public void onTick(long millisUntilFinished) {
            // code to be executed on every iteration
            loaderLabel.setText(MessageFormat.format("{0} {1} {2}", getResources().getString(R.string.loading), ((total_duration - millisUntilFinished) / 1000) * 5, getResources().getString(R.string.percentSymbol)));
            mProgress.setProgress((int) ((total_duration - millisUntilFinished) / 1000)*5);
        }
        public void onFinish() {
            //code to be executed on completing the timer
            Toast.makeText(SplashActivity.this, R.string.welcome, Toast.LENGTH_LONG).show();
            Intent loadMain = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(loadMain);
            finish();
        }
    }.start();
}

That should solve your problem note that I multiply by 5 to get the progress to 100 otherwise it will stop at 20

2
On
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);
}

In your first code snippet, you have instantiated a Timer object but you have not used it to set a timer. Also, inside your Handler instance, there is no loop that can ensure that the counter is increased continually. Also, if there was a loop to increase the counter, as in your previous code

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);
}

The entire 100 iterations of your loop are being carried out in a flash as there is no SLEEP call to make the loop actually wait for 100 ms. You can solve this by adding Thread.sleep(100) before the end of the loop. However, this is not advised as it will cause the entire UI thread to be blocked for 100 ms. So I'll suggest that you use a CountDownTimer for this problem.

This is how you can use it:

new CountDownTimer(total_duration, duration_of_one_step) {
    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
        // Write code to be executed on every iteration here
    }

    public void onFinish() {
        mTextField.setText("done!");
        // Write code to be executed on completing the timer here
    }
}.start();

In your case, total_duration = 20000 and duration_of_one_step = 1000.