Wednesday 9 February 2011

Part 3: TipCalc Sample

Index:
1. First dabble with Google Android
2. That Sample
3. TipCalc Sample

After my previous post I continued working on this sample Andriod application. One very important thing that I found was that the need for "adb" (step 4/5 in the previous post) seems redundant, at least in relation to this specific sample. Intead, in step 4, Apache Ant can be used to compile, install, uninstall and recompile the application as needed. Simply use one of the and parameters, for example, "ant install", "ant uninstall" etc. This seems to emulate the compile and adb install from the previous post.

Now let us continue on the actual TipCalc application, as defined in the sample blog. I started by playing around with DroidDraw a little, but it soon became clear that this application was used to generate the form xml needed to define the front end interface. I will write another blog entry on the use and usefulness of DroidDraw in the near future. But for now, I simply followed instructions as stated in the sample blog. This can be summarised in 3 steps:

  1. Uninstall the Hello World application. The short way to do this is simply by typing "ant uninstall" from the /tipcalc directory

  2. Update the main.xml form. This was reasonably straight forward.

  3. Update the main.java file. This needed a little bit of adjustment. Firstly, the package import on the first line reads "package com.android", this is clearly not the package name specified in the original create project command, I needed to change this to "package com.android.tipcalc". Secondly, because I ran multiple tests, my project name was actually tipcalc5, so the package needed to be com.android.tipcalc5 AND my classname in the file should have also been changed to tipcalc5. This may not affect anyone else, depending on your structure. Either way, these issues should be easy to track down using the output from the "ant compile" or "ant install" commands, e.g.



After running "ant install" again, the application installed without any issues. I was able to run it directly from the emulator.

Let's now look at the actual code a little. I am only going to focus on the java code for now. I do this for no other reason than I easily understand what is happening here. I have some (although limited) Java experience, the majority of my coding experience is in C#. But still, you know one OOB language, you know them all... right? Time will tell. For all you Java buffs out there, feel free to correct me. Also, note that the target audience to this blog post may not necessarily be senior developers, although I am not going into the extreme basics, I will be breaking things down into reasonably basic steps, it is an easy sample after all.

Disclaimer: This is not my source code. I do not have permission to reproduce it in any way, shape or form. This blog entry is intended as supplementary material to this blog. I will remove the source code from my blog if I am requested to do so by the owner of the source blog or any of its affiliates. Any other comments and ideas on this blog are my own.

Firstly the application loads all the libraries needed to run. These will vary based on what you are trying to achieve. The first 2 imports are the core libraries needed to create an activity in Android; the other 4 are used to access textfields, buttons, etc.

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;

Next, all the global variables are created. I do notice that all variables in this application are global. It would be interesting to know what effect this will have on memory usage on an Android device. I assume that the focus of mobile device applications will be to keep things lightweight in every aspect, including memory usage, battery (cpu) usage and disk cost. I am sure some future experiments will answer these questions for me. Here’s an extract of the global variable declaration:

private EditText txtbillamount;
private EditText txtpeople;
......
private double tipamount = 0;
private double totaltopay = 0;
private double perperson = 0;


This is now where things get a little interesting. The method for entering into an Android Application (based purely on this application) is the overridden OnCreate method. This method calls the base OnCreate class (the one it is overriding) and then the ContentView initialisation method. This method then reads the information in the main.xml file (as updated manually earlier). The “default” java file only contains these two lines, so it is clear that this should be the starting (entry) point into your application. The tipcalc application then adds a call to the method that initialises the controls on the form.

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}


Now it gets really interesting. I had to go and delve into the main.xml file. Let me show the xml and the java for the calculate button now:


Button android:id="@+id/btncalculate" android:layout_width="87px" android:layout_height="wrap_content" android:text="Calculate" android:layout_x="40px" android:layout_y="182px"

btncalculate = (Button)findViewById(R.id.btncalculate);

From above it can be seen that the objects (button in this example) can be referenced using the android:id xml attribute for each xml element. This is stored as a static object inside the “R” object. I have not played around with this enough to fully understand this “R” object yet, but it is clear that it is the representation of the values in the main.xml file. I would also bet that this “R” object gets initialised during the setContentView(R.layout.main) method call in the onCreate method (probably stating the obvious though). The other attributes on this button (text, layout_x, layout_y) would be accessible via the btncalculate variable. Each of the objects get initialised in the rest of the init method.

Next the onclick events need to be initialised

btncalculate.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }});

Each button needs to be given something to do when it is clicked. The above line pretty much says that the calculate() method will be called when the button is clicked. It would be interesting to see what other events can be triggered from each object type. It would also be interesting to see what is inside the “View” input parameter of the onClick method that is defined, but all that will wait for another day.

Finally, there are two methods, calculate() and reset(), each called by pressing the corresponding button. The calculate button does the calculations and the reset does the reset (wow, stating the obvious). The actual code in these two methods are pretty straight forward, some basic casting, which seems fairly universal across several programming languages.

So in conclusion, my intention was to learn some basics of Android development. This sample gave me a big kick-start in writing my own application. Things like how to access the front end objects, how to enter into the application and how to run it on the emulator has been extremely helpful. Also, putting what I’ve learned in blog form, as I am learning it, has helped me think about these things in greater depth. Question is now, do I continue on with some more samples first, or do I start writing my own stuff? I think I will work with what I know already and at least see if I can write some of my own stuff. The hardest thing to do is to think of a useful idea that is not a rip-off of another application already available in the Android Market Place. Well, I guess it is about what your intention is, mine is to learn first of all. Learning needs some inspiration initially, so attempting to duplicate an existing closed source application on the marketplace may not be a too bad starting point, as long as you do give credit to the original application designers too. I am lucky enough to have an idea that seems to be reasonably unique (from my research) in the Android Market Place. While I do not wish to divulge my idea (some people tend to steal ideas), I will document my progress and any things that I learn along the way in a similar fashion as this blog did. So check back often.

“I cannot give you the idea, but I can give you the tools to achieve it”

No comments:

Post a Comment