Sunday, January 10, 2010

Chess Clock, Step 1: Begin at the beginning

The first thing I wanted to figure out was how the chronometer worked. I expected to find a method that allowed it to either count up or count down. Such is not the case. Using Google, I was led to the awesome site StackOverflow.com. I've used this site before with programming questions I've had in the past. It's got a great user base and a very slick interface for sharing information.

Anyhow, I came across this post right here. It turns out that a little bit more Magic is involved to get things going. Not that it was overly complicated, but I wanted to lead up to getting the timer going so I opted to take a look at how to implement a simple paired set of buttons. The toggle button fits in quite nicely here. It takes care of its internal state (checked / not-checked). All I needed to do was extend the functionality a bit so that each button toggles the other button.

I fired up Eclipse and started a new project named ChessClock01 (expecting multiple iterations as I develop the app). I used the main.xml layout to establish a very simple interface - the exact layout is the least of my concerns at this point... I just wanted to get the basic element operation going. The layout consists of two Chronometer fields and two ToggleButtons. Further, I updated strings.xml to include textual definitions so I've got a one stop-shop for text definitions. That also allows me to re-use definitions that propagate throughout the whole project.

The ToggleButton is derived from CompoundButton which is derived from Button. The important methods I care about are: isChecked(), setChecked(boolean checked), and setOnClickListener(OnClickListener I).

isChecked allows you to determine the current state of the button. setChecked lets you set the state of the button. setOnClickListener allows you to do something when the button is clicked.

The chess clock has two buttons. When both buttons are off, the first button press will start the associated timer to countdown. When a button is on and is pressed, it will stop that timer and start the other timer. When a button is off and is pressed, nothing should happen. This is behavior is achieved via the listener that is configured with setOnClickListener.

Observations: on the emulator, it appears that the listener is executed before any of the UI graphics are updated. In the listener, when I detect that a button isChecked, I look to see what the state of the other button is. If it is off, I let it stay active and have a TODO: that will start the associated timer. If it is on, I force the clicked button to go back off. Doing this "just works". I half-expected to see the off button to "flash" On briefly before being turned back off. That didn't happen so I'm happy that no further action really needs to be taken.

SIDE NOTE: I discovered that if you go into the DDMS view in Eclipse with the emulator running, there is the option of taking a screen capture of the emulator:


Here is the current incarnation of my app. I've got button toggling operation defined and working. Chronometer isn't happening yet. Not much to look at but it's a start!

No comments:

Post a Comment