Search code examples
javaandroidtimercountdown

Countdown Timer not counting correctly


When I pause the timer and then start it again it seems like the timer counts the current second again. For example it is 00:10, after half a second I stop the timer and when I start it again instead of going from 00:10 to 00:09 in half a second it counts a full second. I can't find what is wrong. Thanks for any help.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer_2);   
    //...
     Timer buttonTimer = new Timer();
     buttonTimer.schedule(new TimerTask() {
          @Override
           public void run() {
            runOnUiThread(new Runnable() {                            
            if (timer_2_up_running) {
                pausetimer_2_up();
                starttimer_2_down();

            } else {
                starttimer_2_up();
                if (timer_2_down_running) {
                    pausetimer_2_down(); 


private void starttimer_2_up() {
    timer_2_up_countdowntimer = new CountDownTimer(starttimeup, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            starttimeup = millisUntilFinished;
            update_up_text();          

        @Override
        public void onFinish() {
            timer_2_up_running = false;

        }
    }.start();

    timer_2_up_running = true;
}

private void starttimer_2_down() {
    timer_2_down_countdowntimer = new CountDownTimer(starttimedown, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            starttimedown = millisUntilFinished;
            update_down_text();

        }

        @Override
        public void onFinish() {
            timer_2_down_running = false;

        }
    }.start();

    timer_2_down_running = true;
}

private void pausetimer_2_up() {
    timer_2_up_countdowntimer.cancel();
    timer_2_up_running = false;
}

private void pausetimer_2_down() {
    timer_2_down_countdowntimer.cancel();
    timer_2_down_running = false;
}

private void update_up_text() {
    int minutes_up = (int) (starttimeup / 1000) / 60;
    int seconds_up = (int) (starttimeup / 1000) % 60;
    String time_2_up_left_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up, seconds_up);
    timer_2_up.setText(time_2_up_left_formatted);
}

private void update_down_text() {
    int minutes_down = (int) (starttimedown / 1000) / 60;
    int seconds_down = (int) (starttimedown / 1000) % 60;
    String time_2_down_left_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_down, seconds_down);
    timer_2_down.setText(time_2_down_left_formatted);
}

Solution

  • You can use Chronometer to display recording time. start the Chronometer when recording start and stop it when recording stop

    <Chronometer
            android:id="@+id/chronometerRecordTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/relativeLayout_bottom_video_controls"
            android:layout_centerInParent="true"
            android:layout_marginBottom="20dp"
            android:visibility="invisible"/>
    
    
    private Chronometer chronometerRecordTime;
    
    chronometerRecordTime = findViewById(R.id.chronometerRecordTime);
    
    chronometerRecordTime.setBase(SystemClock.elapsedRealtime()); // Reset
    chronometerRecordTime.start(); // start
    
    chronometerRecordTime.stop(); // stop