Sunday, July 27, 2014

Day 4: Tackling the Chronometer and Spinner Event

Day 4: I previewed what still needed to be completed yesterday, and today, I decided to tackle the beast: having the chronometer stop at x minutes, where x is specified by the spinner option menu. I knew this would be more difficult, so I looked up some resources and compiled them here:

Sounds simple enough; simply set an onChronometerTickListener() and with an inner class, call chronometer.getText().toString() and compare it to your specified time. To figure out what kind of format it displayed it in, I decided to display it as a Toast notification. I find that doing Toast notifications allows for some feedback on the device itself when testing.  Toast.makeToast(getActivity().getApplicationContext(), text you want to display, Toast.LENGTH_SHORT).show() It's that simple. Through this, I saw that it gave me a text in form MM:SS, where M == minute and S == seconds. To compare it to something using equals(), I would need to format the spinner value.

But first, I must figure out how to get the value. By continuation of the Spinner documentation on Android's guide, it suggests to set a listener. It also conveniently tells you how to get the item selected. Due to many possible values of items selected, I made an if statement:
@Override
    public void onItemSelected(AdapterView<?> parent, View view,
      int position, long id) {

     String test = parent.getItemAtPosition(position).toString();

     if (test.length() == 1) {
      chronDisplayFormatted = "0" + test + ":00";
     } else {
      chronDisplayFormatted = test + ":00";
     }

    }
This makes the item, simply displayed as an integer on the spinner, into a format similar to what is displayed by chronometer.getText().toString() .

A simple if statement asking if the two are equal fulfills my requirements, and here we are, time for testing. Lo and behold, it works. 

Thoughts: This. Is. Awesome. Now to do Notifications tomorrow, we'll see what that's like as I have zero experience with it... well, I didn't have experience with this either.

No comments:

Post a Comment