Reputation: 911
I want to create a stopwatch in my Android application that starts running when the user presses start and continues to run and enables the user to time specific intervals.
Use case: The user presses "start timing" and the stopwatch starts. The user presses "stop timing", the stopwatch continues to run but counts the length of the interval. After sometime the user presses "start timing" again and then after some time "stop timing" and so on.
In the end I want to be able to build the following table:
start time end time
00:00 00:10
00:15 00:20
...
I am using a chronometer and have tried this, and this, but they both require me to stop the chronometer which I don't want to do.
I can solve this by creating two chronometers, one visible that runs and gets start times and another invisible one for timing the intervals, but am wondering if this could be done in a more elegant way.
Thank you!
Upvotes: 1
Views: 530
Reputation: 1596
Check out android-ultimatestopwatch, an open source app by Rich Hyndman, an Android Developer Advocate at Google.
It refers to intervals as "LapTimes" and implements the following classes for managing them:
LapTimeBlock
LapTimeRecorder
LapTimesBaseAdapter
LapTimesFragment
This might be overkill for your requirements, but it could probably give you the ideas you need for the elegant solution you seek. The app is also available on Google Play if you want to give it a quick run: Ultimate Stopwatch & Timer
Upvotes: 1