Search code examples
androidchronometer

Why isn't onChronometerTick being called?


My code never enters the onChronometerTick method. What am I doing incorrectly? Do I need to set the tick time somehow? I can't figure out a way to do that.

elapsedTimer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
    @Override
    public void onChronometerTick(Chronometer chronometer) {
        countUp[0] = (SystemClock.elapsedRealtime() - chronometer.getBase()) / 1000;
        elapsedTimeDynamic.setText("" + countUp);
    }
});
elapsedTimer.start();

Solution

  • For anyone in the future who is having trouble using chronometer for an elapsed timer/stopwatch and can't figure it out, I ended up scrapping chronometer and using CountDownTimer, but using it in such a way that it would also count up:

    final long startTime = System.currentTimeMillis();
    elapsedTimer = new CountDownTimer(activityTime + 1000, 100) {
        @Override
        public void onTick(long l) {
    
            long elapsedTime = System.currentTimeMillis() - startTime;
            elapsedTime = elapsedTime/1000;
            elapsedTimeTextView.setText(String.format("%02d", (elapsedTime)));
        }
    
        @Override
        public void onFinish() {
    
        }
    }.start();