Alona
Alona

Reputation: 27

timer doesn't show minutes

I'm trying to set a timer. the timer is working but it counts only seconds ... the minutes remain 00.

Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {

    @Override
    public void run() {
        long millis = ((System.currentTimeMillis() + resumeTime) - startTime);
        int hours   = (int) ((millis / (1000*60*60)) % 24);
        int seconds = (int) ((millis / 1000) % 60); 
        int minutes = seconds / 60;

        TextView timer = (TextView) findViewById(R.id.textView6);
        timer.setText(String.format("%02d:%02d:%02d",hours, minutes, seconds));

        timerHandler.postDelayed(this, 500);
    }
};

Upvotes: 0

Views: 42

Answers (2)

Giru Bhai
Giru Bhai

Reputation: 14408

As WarrenFaith suggested seconds is always between 0 and 59, dividing it by 60 just means that it is always 0.

So to get minutes,Change

int minutes = seconds / 60;

to

int minutes = (int) (millis / (1000 * 60));

Upvotes: 0

WarrenFaith
WarrenFaith

Reputation: 57682

seconds is always between 0 and 59, dividing it by 60 just means that it is always 0. You should modify your math to get the minutes based on hours and not seconds. Doing the math is up to you :)

PS: I just read that your question title contradicts the first sentence of your question. Yu mean your minutes is always 00 and not the seconds, right?!

Upvotes: 1

Related Questions