Monday, July 28, 2014

Day 5: Notifications (du da da!)

Day 5: Notifications, this is exciting. Again, the Developer Android Guide is unbeatable, it's amazing and incredibly condense. I followed this with ease and simply called for the notification in the method I created yesterday, where I checked if the chronometer's time was the same as the specified time to stop. 

Having finished so quickly, I decided to take a stab at the overflow menu. The easiest option was to create an About page which details why taking breaks are important, and who it was created by. Glancing through the code in MainActivity, the method onOptionsItemSelected() took care of this. I created a new class that would open when the option is pressed. I simply created two textViews, one larger one for the explanation, and one smaller one for Author. Here's a screenshot:
Looking pretty good eh?

Thoughts: Well, all the major features for the basic app are complete. It only five days but they were long, and tiring. What's next? Time to think of more features to add!

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.

Saturday, July 26, 2014

Day 3: TextViews and Spinners

Day 3: It's slowly coming together, the chronometer is done, and now it's onto the textViews and spinners. When I first heard the word "spinner" in an Android context, I thought, "Oh, the spinny wheel when things load?" Nope, not even close. A spinner is actually a drop down menu of options to choose from. 

First things first, let's tackle the easy things: textViews. TextViews are easily generated by loading up the XML file  associated with the Activity and in the graphical view, dragging the TextView onto the screen. To edit this, simply head to the XML layout and you can change name, textSize, textStyle, id, and much more! After configuring my textViews, I went on to add the spinner. Like the TextView, simply drag it onto the screen.

Configuring the Spinner is not as simple as a TextView, however. First, create an instance of the spinner similar to Day 1's buttons, instead however, with type Spinner. To add values into a Spinner, one must use an adapter. I found this resource particularly helpful: Android Guide: Spinners. The developer guide goes quite in depth with example code. I simply followed that and was on my way. 

With two things crossed off my list, I decided to have a little fun and put some quotes in the empty space of my application. I knew it would be impossible to display multiple quotes; it simply will not fit unless the font is miniscule. It would be super cool if it could display a different, random quote out of an array of quotes I have, each time the application is opened. Thus began my quest on Google to learn how this would be possible as I had no ideas. I followed this solution on stackOverflow and simply ignored the Button events. This worked perfectly and I now had quotes on the bottom.

Looking good right? 

Thoughts: It's getting closer and closer! I even made a quick icon on Paint today!
What I still need to complete: Chronometer stopping in x minutes as specified by spinner, and notifications when those x minutes are up!

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

Wednesday, July 23, 2014

The First Day...

First day: To be fair, this isn't exactly my first day at this, simply just the first day writing this blog. So I did do a little bit of playing around in Eclipse with sample applications before this. While trying to create an application idea I had around Car Statistics, I realized my need for a reminder type system for myself to take a break. Sometimes I would be googling and browsing through Stack Overflow for the longest time without giving my eyes a break and even missing some of my favourite shows on television because I have lost track of time. So, here goes, my first app, albeit a simple one, one that I intend to finish and publish. 

First, a little background..

I'm entering my second year of studies in Computer Science at the University of British Columbia, having experience in three CPSC courses; CPSC 110 (Programming with Scheme), CPSC 121 (Models of Computation) and CPSC 210 (Software Construction with Java). CPSC 210 allowed myself and other students to get their first glimpse of Android programming through an end of the year project culminating to a NextBus transit application. From this experience, I'm diving into the world of Android Application Development. Follow along daily if you wish...
Hop along for the ride!