Search code examples
androidtimestampprogress-barcountdowntimer

ProgressBar with timestamp in millis


I need progress bar with interval from two timestamp values in millis.

For example, I have timestamp of 08:46:11 30.06.2019 and 10:46:11 30.06.2019. But current time is 09:46:11 30.06.2019, so now progressbar should be filled on 50% and going up to 100 until 10:46:11 30.06.2019.

So I have tried next code:

private void progressBar(){
            prBar.setProgress(i);
            mCountDownTimer=new CountDownTimer(ltimestampStop * 1000,ltimestampStart * 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    Log.v("Log_tag", "Tick of Progress"+ i+ millisUntilFinished);
                    i++;
                    int Start = ltimestampStart.intValue();
                    int Stop = ltimestampStop.intValue();
                    prBar.setProgress((int)i*100/(Stop/Start));

                }

                @Override
                public void onFinish() {
                    //Do what you want
                    i++;
                    prBar.setProgress(100);
                }
            };
            mCountDownTimer.start();
        }

But problem is that int cannot handle my timestamp in millis and setProgress() didn't work with long type.

What I can try for achieving my task?

UPD: depending on my task I think countdowntimer is not what I need.


Solution

  • Initialize your progressbar and CountDownTimer like this -

     MyCountDownTimer myCountDownTimer;
    `ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressbar);`
     progressBar.setProgress(100);
    

    Make your countdowntimer like this -

    public class MyCountDownTimer extends CountDownTimer {
    
       public MyCountDownTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
      }
    
        @Override
        public void onTick(long millisUntilFinished) {
        Log.d("Time in millis", String.valueOf(millisUntilFinished));
        int progress = (int) (millisUntilFinished/100);
        progressBar.setProgress(progress);
       }
    
        @Override
        public void onFinish() {
        Log.d("Time in millis", "Timer finished");
        progressBar.setProgress(0);
       }
    }
    

    Call your CountDownTimer like this-

    myCountDownTimer = new MyCountDownTimer(50000, 1000);
    myCountDownTimer.start();