We recently started with Mobile Application development. I am learning Android using the Commonsware book, and highly recommend it. This is a very short guide to getting you running your first Android app.
What will we build
We will build a simple tax calculator for India, per the rules given here for tax calculation. In this app, we need to gets users income and various tax deductible expenses. After that we need to update the UI to show the tax. You can see the final app and code on github here.
- Install Java and Eclipse
- Install Android SDK and add ons.
- Instal ADT for eclipse.
- Create a Android project from within Eclipse.
What the project contains:
- AndroidManifest.xml: This is the entry point for the app. It decides the properties including which Java file will start the App.
- YourApp.java: This is a java class which extends
Activityand has the
onCreatemethod, which decides where to get the Layout from.
- main.xml: By default
YourApp.javawill look for Layout in the files res/layout/main.java
- strings.xml: While not necessary you will have your strings in an external xml file here.
There are a lot of other files, but these are the ones you are going to need for our App.
How does the Layout go to the Java file
You will need to modify the Layout in your Java code, but you defined your layout in main.xml. How does Java get it? Eclipse auto-generates a file called R.java, which will get your layout and all declared fields in Java. R.java, should not be edited directly, it is automatically generated. (As is everything in the gen folder.)
Lets write the layout.
You can build the Layout in a GUI tool provided by Eclipse, but I found it very underpowered. I preferred to write it directly in XML.
There are a lot of widgets available, but for this app we are going to need three.
- A Uneditable text field. Its called a TextView.
- I Editable text view. Its called a EditText
- A button. Its called a Button.
- Make it scrollable. Its called a ScrollView
Layout your app in the preferred order. I added 5 EditText to get the income and tax deductible values. We will read these values and display them in a TextView. We need a button to know when to calculate the tax.
You will notice that instead of setting the text values via normal strings we are doing it via
android:text="@string/income_tax_init". This references
values from the Strings.xml file which looks like this.
You can set a lot of values on your UI elements for setting things like color. The import one is
android:id. You must set a value here if you want to make
the widget available in your Java code.
Let write the Java code
In our Java code we ned to do,
- Get the Input value for the fields.
- Calculate tax using these values.
- Update the UI to show the tax.
All of this needs to be handled after a click, so your public class needs to
and implement the
You can get the fields you declared in XML this way:
And the values from them as,
Now we can get the values and parse them as
Integer.parseInt, and calculate tax via normal Java semantics.
Once we have the tax, the UI can be updated via:
Bridging XML and Java
Most of the Java<->XML bridging is done via R.java, by making the XML widgets available in Java. You need to set the function to be called on button click.
This is done via the
android:onCLick property on the
Button, in the
And we are done
You can set the logo and package it for sale in the Android marketplace now. :)
- Code for this app
- This app on Android marketplace
- Similar code for Iphone and Objective C
- Commonsware: The book I am reading
Thank you for reading the Agiliq blog. This article was written by shabda on Feb 6, 2011 in java .
You can subscribe ⚛ to our blog.
We love building amazing apps for web and mobile for our clients. If you are looking for development help, contact us today ✉.
Would you like to download 10+ free Django and Python books? Get them here