Friday, July 25, 2014

Day 2: The Beginning

Day 2: After having the architecture of the button presses implemented, I dove straight into implementing them. I knew that there were three scenarios to the button presses:

  1. Starting from time 00:00
  2. Stopping and pausing the time
  3. Resuming the time from the paused time

My first idea of tackling case one,starting from time 00:00, is to simply call chronometer.start().
Testing this on my device, this clearly does not work as on start, a random number is displayed rather than beginning 00:01. So I decide to look at the documentation, and a method caught my eye,  chronometer.setBase(long base). This method sets the time that the count-up timer is in reference to. What I want it to be a reference to is how much time has elapsed. Fortunately, there is a method for that already, SystemClock.elapsedRealTime()

Time for case two: stopping the timer. Like my first idea to case one, I thought of trying chronometer.stop(). This worked perfectly. It stopped it momentarily.

Case three; the last case. I knew for a fact that chronometer.start() must be invoked. The difficulty I encountered with this was how to preserve the time seen on the Chronometer after pausing the time and starting it again. For example, I pause it at 00:30 and start it again, and it should go to 00:31 rather than 00:00. There must be a way to get the time when stopped, then start it again at that precise time. After playing around with getText in the stop then setBase in resume, I was slightly stuck, so onto Google it was. I found this StackOverflow answer helpful.

 As I was working on the app, I saw the necessity to add a restart button to begin at 00:00. This implementation was quite straight forward compared to case three. When one clicks restart, the chronometer must be stopped, the toggle button must be flipped and the chronometer must be reset to 00:00. All this came be completed with chronometer.stop(), chronometer.setText(00:00) and toggle.setChecked(false);

Thoughts so far: Wow, what an experience. There's definitely still plenty to learn, but I feel like I have learnt a lot already. From not even knowing what a chronometer is, to creating buttons that can control the chronometer, the future is bright.

Useful Links:
http://android-pro.blogspot.ca/2010/06/android-chronometer-timer.html

No comments:

Post a Comment