Monday 27 July 2015

Checkboxes and scrollviews

The Udacity course took a swing into the Java side with explanations of variables and accessing the Views so as to manipulate them more programmatically. This to me was a welcome change of pace because I already have a fair understanding of Java. While XML has been a fun way to approach design, I'm happier when I'm making more unique code. The introduction to logic in this series has been with the use of a checkbox. Checking the state of the box also led to some details on inheritance. For the most part this is in explaining how to get a specific type of View by its ID rather than just the default View. What I found interesting was my ideas of how to handle the quantity picker and 0 or overly high values being selected. While at first I thought my  ode did a simple but effective job at preventing the variable from decreasing, I realized later that an if/else block actually gave me a much more elegant solution.

My original code simply had the following:

public void increment() {
     quantity++;

     if(quantity > 100){
          quantity = 100;
     }

     updateDisplay();
}

Not too pretty but it does the job needed. The tutorial suggests adding a Toast if the user attempts to increase the quantity above the limit (here set at 100). This idea seemed fun so I worked that into my code and came to the realization that if the incrementer is only ever going to respond to single digit changes, we could instead just never increment past the upper limit. My code instead now looks like this!

public void increment() {
     if(quantity == 100){
          Toast.makeText(this, "You cannot order more coffees",
          Toast.LENGTH_SHORT).show();
     } else {
          quantity++;
     }

     updateDisplay();
}

It is these little changes to the code that really give me a sense of satisfaction in my own ability. The code that the tutorial uses is a little more heavy but also does the task, but seeing my own implementation and its efficiency is one of those little wins.

The other aspect of the tutorials I have been introduced to is the use of a scrollview as the root view in the xml file. This is a welcome lesson as I have been attempting to move the Java utility I made for my work into an android setting (more as a test of my ability to port an existing app than anything else) and realized I quickly ran out of space on the screen if I had text at a decent size. While I am happy to read a book or website in tiny text, its important that if my app is to be used for quick sessions to check data, the display be a size that utilizes as much of the screen as possible.

Next steps are going to be looking at Intents basics and localization. Since I don't speak any foreign languages, I will be playing around with specific locations of English, such as En-GB and EN-US so that I can get accustomed to adding localization as I go, but also so I can dick around with stereotypical dialects :P