9

I want to create an incrementing second timer like a stopwatch.

So I want to be able to display the seconds and minutes incrementing in the format 00:01...

Google only brings up 24 hour clock examples, I was wondering could anyone get me started with an example or tutorial of what I want to do?

Edit:

Here is what I have using the Chronometer in Android so far

In onCreate()

    secondsT = 0;
    elapsedTimeBeforePause = 0;

    stopWatch.start();
    startTime = SystemClock.elapsedRealtime();
    stopWatch.setBase(elapsedTimeBeforePause);

     stopWatch.setOnChronometerTickListener(new OnChronometerTickListener(){
            @Override
            public void onChronometerTick(Chronometer arg0) {
                //countUp is a long declared earlier
                secondsT = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
                String asText = (secondsT / 60) + ":" + (secondsT % 60); 
                //textGoesHere is a TextView
                ((TextView)findViewById(R.id.time)).setText(asText);
            }
     });

In onDestroy()

@Override
public void onDestroy() {

    inCall = false;
    elapsedTimeBeforePause = SystemClock.elapsedRealtime() - stopWatch.getBase();

    super.onDestroy();
}

The above compiles and runs but the TextView never increments, it always stays at 0, can anyone see why?

Donal Rafferty
  • 19,707
  • 39
  • 114
  • 191
  • 2
    You'd want to be counting in Unix time and displaying in 00:00 format. – Rimian Mar 29 '10 at 09:31
  • 1
    Is the timer the problem, or displaying a timer? And how do you want to display it, Swing, CLI, web page? – extraneon Mar 29 '10 at 09:31
  • Creating the timer at the minute is what i want to learn how to do properly, target is actually Android and the timer will need to be able to be displayed in a TextView - http://developer.android.com/intl/de/reference/android/widget/TextView.html – Donal Rafferty Mar 29 '10 at 09:38

2 Answers2

29

I'm assuming you aren't aware of the Android Chronometer - it already has a basic stopwatch function. You need to work with its peculiarities a bit, but it's not hard to get it to do what you want once you understand how it works.

There are a few ways that time is calculated on the phone, but the two main ones are:

  1. The "real time", such as right now according to my computer clock, it is 11:23am in England. However, this can change if my computer contacts a time server and is told it has the wrong time, or if I were travelling with a laptop and crossed a timezone boundary. Using this would wreak havoc with your stopwatch as the measured time could change at any time.

  2. The "elapsed time since boot", which is the number of milliseconds since the phone was switched on. This number doesn't bear any relation to the real time it is, but it will behave in a perfectly predictable manner. This is what the Android Chronometer uses.

The Chronometer is essentially a 'count up' timer, comparing the current SystemClock.elapsedRealtime() against the elapsedRealtime() that was set fot its base time. The difference between the two, divided by 1000, is the number of seconds since the timer was started. However, if you stop the timer and then start it again, you will get a counter-intuitive result - the timer will show the elapsed time as if it had never stopped. This is because you need to adjust its base time to take into consideration the time it was stopped. This is simple to do:

// When you're stopping the stopwatch, use this
// This is the number of milliseconds the timer was running for
elapsedTimeBeforePause = SystemClock.elapsedRealtime() - timer.getBase();

// When you're starting it again:
timer.setBase(SystemClock.elapsedRealtime() - elapsedTimeBeforePause);

Edit: Here is the full code for a basic stopwatch, which displays your time in a TextView rather than the Chronometer widget declared in your XML file.

public class TestProject extends Activity {
    TextView textGoesHere;
    long startTime;
    long countUp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Chronometer stopWatch = (Chronometer) findViewById(R.id.chrono);
        startTime = SystemClock.elapsedRealtime();

        textGoesHere = (TextView) findViewById(R.id.textGoesHere);
        stopWatch.setOnChronometerTickListener(new OnChronometerTickListener(){
            @Override
            public void onChronometerTick(Chronometer arg0) {
                countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
                String asText = (countUp / 60) + ":" + (countUp % 60); 
                textGoesHere.setText(asText);
            }
        });
        stopWatch.start();
    }
}

In your main.xml you need to have this

<Chronometer android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/chrono"
    android:visibility="gone"/>

There's undoubtedly a way to get the Chronometer to work without declaring it in the XML file, but the constructor Chronometer stopwatch = new Chronometer(this); didn't work properly.

The above code displays the elapsed time in a very basic way. For example, if only 5 seconds have gone by, it will show 0:5 rather than the 0:05 you probably want. Fixing that is not hard to do, but I'll leave that for you to work out! :)

Steve Haley
  • 55,374
  • 17
  • 77
  • 85
  • Excellent post! Can I just ask where you get the startTime from? – Donal Rafferty Mar 29 '10 at 11:06
  • 1
    Oh, startTime is just another long, set to the SystemClock.elapsedRealtime() at the same time as the chronometer is started. That actually gives it the same value as the chronometer's base time, so instead you could have used arg0.getBase(). – Steve Haley Mar 29 '10 at 11:19
  • I have implemented as above (see edited post) but the TextView stays at 0 and never increments at all, can you see the reason for this by any chance? – Donal Rafferty Mar 29 '10 at 11:27
  • 1
    Yes, I cut a corner when posting the answer because it was getting too long. I'll edit my answer to provide the full issue. – Steve Haley Mar 29 '10 at 11:35
  • Thanks, So now I have added it to the xml I presume I have to reference it in my code, something like stopwatch = findResourceById(R.id.Chrono) ? – Donal Rafferty Mar 29 '10 at 11:44
  • 1
    No, that's the whole code now. By some magic in `setContentView` or `findViewById(R.id.chrono)`, it initialises the chronometer correctly. Running that code for me correctly displays a textbox with x:x, incrementing properly every second. – Steve Haley Mar 29 '10 at 11:47
  • Cheers, thanks that has sorted it, your help is much appreciated – Donal Rafferty Mar 29 '10 at 11:53
  • Thank you I was looking for this info – jramirez Aug 09 '10 at 04:47
  • thank u this is very useful for me, how to set the format like this 00:09 . – RajaReddy PolamReddy Sep 23 '11 at 09:46
  • Raj: spend some time to understand what the code above is doing. Once you understand it, you'll see that changing that format of the text can be done very easily. Just have an if-statement checking the value of `countUp / 60` and `countUp % 60`, adding a 0 before the string if needed. – Steve Haley Sep 23 '11 at 14:28
  • Wonderful post, suggestion how to approach the scenario, where the Activity is exited (onDestroy()) and you bring it back (let's say from Notifications- onResume()). Chronometer will be destroyed. I want to still count the time, even after the Activity is destroyed. I did 3 solutions - but can't decide which is best. Chronometer in a Service class (that lives on) and adding that Chronometer programmatically in the layout of the Activity. Handler with tick message, that increments a counter. Last-the above code, but I keep timeOfDestroy and timeOfResume and add the diff to the Chronometer base. – hovanessyan Dec 12 '11 at 22:07
  • Thanks! In place of "String asText ...;", I used this: "asText= chrono.getText().toString();" This eliminates the need for countUp too. – Jon Jun 21 '12 at 18:33
  • Hi can any one help me I want to make this stop watch global for every activity of my application?? – Ankit HTech Oct 23 '12 at 04:44
  • @rajohns I haven't looked at this in over 3 years :P I couldn't even say whether `Chronometer` is the best way to do this, if it ever was. You can achieve the same result with a Runnable posting itself, if you want to get the 'tick' every second. – Steve Haley Jul 12 '13 at 16:27
  • @SteveHaley I ended up using the answer i posted here: http://stackoverflow.com/questions/17598440/why-isnt-onchronometertick-being-called Thank you for coming back and letting me know though. – Adam Johns Jul 12 '13 at 17:38
  • How do you display this in the notification bar when the app goes to background? – John Ernest Guadalupe May 25 '15 at 05:18
2

How about this one Digital Clock.

But basically you should be able to find many java implementations by googling for keywords like java, stopwatch, digital clock, ...

You could take a look at Stopwatch Applet and modify as needed (Oops no source with this one)

jitter
  • 53,475
  • 11
  • 111
  • 124