Android ProgressBar: timer loading

Update: This post was from when I studied android with Android 2.2, be sure to check if it is compatible with the api level you are working with.

I’m ready to get around a lot about how to load the progress bar using a timer, I tried to use methods, but I always got a pointer exception and when I tried to start the application when loading, it crashed.

My question is: lol did anyone come across any tutorials or examples on how to do this? I think that all I need is a while loop, but I have not yet seen that this is done on a timer. I'm a complete noob, but slowly but surely studying

My timer

final Timer t = new Timer();
    t.schedule(new TimerTask() {
        public void run() {
            d.dismiss();  //MY DIALOG WINDOW
            t.cancel();
        }
    }, 7000);   

This is the one I was trying to find, but I think I did more damage than good with it.

isRunning = false;
    // handler for the background updating
    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            total = total - 1;
            String perc = String.valueOf(total).toString();
            a.setText(perc + " SECONDS  until close");
            bar.incrementProgressBy(25);
        }
    };

    super.onStart();
    // reset the bar to the default value of 0
    bar.setProgress(0);
    // create a thread for updating the progress bar
    Thread background = new Thread(new Runnable() {
        public void run() {
            try {
                for (int i = 4; i < 20 && isRunning; i++) {
                    // wait 1000ms between each update
                    Thread.sleep(1000);
                    handler.sendMessage(handler.obtainMessage());
                }
            } catch (Throwable t) {
            }
        }
    });
    isRunning = true;
    // start the background thread
    background.start();

, - , , , , ,

tahnks

+5
2

android.os.CountDownTimer

 countDownTimer = new CountDownTimer(length_in_milliseconds,period_in_milliseconds) {
        private boolean warned = false;
        @Override
        public void onTick(long millisUntilFinished_) {
           progressBar.progress = (length_in_milliseconds-millisUntilFinished_)/lenght_in_milliseconds*100.0;
        }

        @Override
        public void onFinish() {
            // do whatever when the bar is full
        }
    }.start();
+11

:

ObjectAnimator ProgressBar:

ObjectAnimator animation = ObjectAnimator.ofInt(pb, "progress", 0, 100);
animation.setDuration(5000);
animation.setInterpolator(new DecelerateInterpolator());
animation.addListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animator) { }

    @Override
    public void onAnimationEnd(Animator animator) {
        //do something when the countdown is complete
    }

    @Override
    public void onAnimationCancel(Animator animator) { }

    @Override
    public void onAnimationRepeat(Animator animator) { }
});
animation.start();
+1

All Articles