shabda
Comments
Reactions

Starting Android app developement: From zero to app

By : Shabda Raaj

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.

Setting up

  1. Install Java and Eclipse
  2. Install Android SDK and add ons.
  3. Instal ADT for eclipse.
  4. Create a Android project from within Eclipse.

What the project contains:

  1. AndroidManifest.xml: This is the entry point for the app. It decides the properties including which Java file will start the App.
  2. YourApp.java: This is a java class which extends Activity and has the onCreate method, which decides where to get the Layout from.
  3. main.xml: By default YourApp.java will look for Layout in the files res/layout/main.java
  4. 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.

  1. A Uneditable text field. Its called a TextView.
  2. I Editable text view. Its called a EditText
  3. A button. Its called a Button.
  4. 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.

Your code will look like this after you are done.

Let write the Java code

In our Java code we ned to do,

  1. Get the Input value for the fields.
  2. Calculate tax using these values.
  3. Update the UI to show the tax.

All of this needs to be handled after a click, so your public class needs to implements Button.OnClickListener and implement the onClick.

You can get the fields you declared in XML this way:

(EditText)findViewById(R.id.income);

And the values from them as,

income.getText().toString();

Now we can get the values and parse them as int via Integer.parseInt, and calculate tax via normal Java semantics.

Once we have the tax, the UI can be updated via:

tax.setText(""+tax);

Your final Java code will look like this.

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 main.xml.

And we are done

You can set the logo and package it for sale in the Android marketplace now. :)

Resources

  1. Code for this app
  2. This app on Android marketplace
  3. Similar code for Iphone and Objective C
  4. Commonsware: The book I am reading


Related Posts


Can we help you build amazing apps? Contact us today.

Comments

Sandeep

Nice tutorial man!!

commmenttor
rosetta stone

Rosetta Stone is proprietary language-learning software produced by Rosetta Stone;Ltd. Its title and its logo refer to the Rosetta Stone , an artifact inscribed in multiple languages that helped Jean-Fran?ois Champollion to decipher Egyptian hieroglyphics.
The Rosetta Stone software uses a combination of images, text, and sound, with difficulty levels increasing as the student progresses, in order to teach various vocabulary terms and grammatical functions intuitively, without drills or translation. They call this the Dynamic Immersion method;. The goal is to teach languages the way first languages are learned.Now,the Rosetta Stone;Ltd have several kinds of the Rosetta Stone language on sale,such as Rosetta Stone Spanish,Rosetta Stone German and Rosetta Stone Italian, these Rosetta Stone Languages are very popular now!

commmenttor
ERNEST WOODY 25th Sept., 2012

I HAVE A WONDERFUL IDEA FOR A ANDROID GAME IF YOU INTERESTED IN MABYBE GOING INTO BUSINESS CONTACT ME 773 557 9663 MY NAME IS LEGEND.

commmenttor
Maxwell Macias

Mostly people do the work of layout in GUI tool, but your concept of writing it in XML and bridging it to Java is nice. I've seen thing like this first time.

commmenttor

Reactions

newsery1

Starting android application development - http://bit.ly/dGrWQt - [Hacker News FH]

commmenttor
hnfirehose

Starting android application development: http://bit.ly/eln1Ch

commmenttor
scriptrunner

Starting #Android app developement: From zero to app http://bit.ly/eEiqNK

commmenttor
bit_jammer

http://ow.ly/3Siso - Starting #Android app developement: From zero to app

commmenttor
© Agiliq, 2009-2012