Bootstrap Progress bar not incrementing in android

The Only Smart Boy :

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?

Otema :

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=373882&siteId=1