Friday, March 25, 2011

Simple but informative Example

Hello Everyone...

Lets take an example of adding two numbers and print the result on a new activity....

For this, Create a simple project and directly go into the main.xml file and make the xml looks like following:

<?xml version="1.0" encoding="utf-8"?>
        android:text="First Number:"/>
        android:text="Second Number:"/>
        android:text="Show Result"

Now in above layout i have tried to tell as many things, which could be helpful if we know how to use them and when. Lets discuss them first,

  • android:layout_width="150dp"
as in other places, i have used "fill_parent" or "wrap_content"; meaning is clear from the name itself, like "fill_parent" will fill the parent control and etc.. now if we want to limit the size then better approach then giving size in 'pixels' is to give in 'dp'... by giving in 'dp' the limitation of variable pixel size screens is rule out.

  • android:onClick="@string/HandleShowResultButton"
When declaring '@string' to a property, now it treat the value as name and we must provide the value of this variable in "strings.xml" like:

<string name="HandleShowResultButton">OnClickShowResultButton</string>

Now coming to activity file,
  public class InputNumbers extends Activity
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    public void OnClickShowResultButton(View view)
        EditText firstNumber = (EditText)findViewById(;
        EditText secondNumber = (EditText)findViewById(;
        String str_firstNumber = firstNumber.getText().toString();
        String str_secondNumber = secondNumber.getText().toString();
        int i_firstNumber = Integer.parseInt(str_firstNumber);
        int i_secondNumber = Integer.parseInt(str_secondNumber);
        int result = i_firstNumber + i_secondNumber;
        Bundle b = new Bundle();
        b.putInt("Result", result);
        Intent newIntent = new Intent(getApplicationContext(), ShowResult.class);

I dont think i have to explain further.. its pretty much clear now...
In above example:
  • you can create new activity
  • pass some value around an activity
  • destroy/finish current activity
  • Retrieve GUI controls from code
  • Implement a Click Listener for a button etc...
If you didn't understand anything, feel free to ask. Source code is attached.


Wednesday, March 23, 2011

Start Androiding....


In this post, we'll try to cover following things:
  • Some things to look upon before starting
  • Creating simple test project
  • Run it on emulator/device
I assume that android environment is set in your machine; if not goto setting android environment
Now open eclipse and create new project by clicking
Under Project heading choose "Android Project" in Android Category and press next. you will see the following screen.

Now fill the properties like :

                       Project Name          = HelloWorldProject
                       BuildTarget              = any; i choose Android 2.1-update1
                       Application Name    = Hello World App
                       Package Name        = com.HelloWorld
                       Activity                    = HelloWorldActivity

I add property names in the end of each property value to distinguish them. Later you can check, which file goes where...
Press Finish and you have successfully created your first project. Simply right click on project name from project explorer view and click build; there should be no errors in the project.

Creating a virtual device:
To create a virtual device; Goto Window->Android SDK and AVD manager, selecting virtual devices a new window will open. Select new and set name and platform for the device, give size of sd card like 24/32 MB and press "create AVD". A new virtual device has been created. Select this device and press start. New Virtual device will run.

Now coming back to our project, right click on the project again and click RunAs->Android Application... our application will install on this virtual device and it will run automatically. if you didn't start the virtual device then by clicking RunAs, it will select appropriate virtual device by checking the platform of application.

Output will be like this:

Now lets talk about some things... :)

Things to look into:
  • Activity class
  • layout file
  • manifest file
For every application having a GUI, there should be a class which extends from Activity. so Activity is the class given by android which handles the screen or gives you the full screen and you can set controls/views on this. Lets c what our class looks like...

package com.HelloWorld;

import android.os.Bundle;

public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

as you can see our class extends from Activity which is responsible for handling GUI components. Every activity is started by calling its onCreate function or you can say that from onCreate function the scope of any activity starts. when we extend our class from Activity; it by default inherits its create/destroy functions and we have to override them in order to meet our specifications, like we have to set any control/view in our window then we would override onCreate function and set our window but still we needs to call its original onCreate function as shown in code above:
Please also read the official documents about activity from here: Reference document for Activity

This line adds functionality and sets a layout to our window:

Now the design of the screen is created by two methods; from xml or from code. In res/layout/main.xml you will see something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

First you have to set a layout for your screen. Android provides 5 to 6 layouts, i have mostly used LinearLayout i.e all controls will be added up linearly one after another (horizontally or vertically). you can check further details from here: layout-objects

As shown above, we have add a TextView control in our layout, similarly we can add different controls. open main.xml and switch to graphical layout and you can see different controls. You can also change the properties of any control, a TextView in our case, by switching to graphical layout and look for properties view; if not found, simply add it from Window->Show View->Other->General->Properties

In Menifest.xml file, we declare application icon, app name, activities and permissions which our application uses like 'internet'.

That was all.... I try my best to get you start easily but still if you find anything difficult, leave a comment.


Wednesday, December 29, 2010

Setting up Android Development Environment

Hi guys,

This is my first blog in android Development. It was a little tricky from android home website to setup the environment so i thought of writing a blog for you as well as for me... :)
I'll start sharing with "how to set up Android Development Environment". You can also suggest topics for other blogs. I'll try my best to stay as simple as i can...

Ok now:
For Android Development you just need a pc, Android really make it easy to develop and test in emulator not like symbian which take much time only in loading. Android emulator is very powerfull, almost every application can be tested in emulator.

There are total of 3 downloads to be done...

First of all, download "Eclipse for Java" from as Android is in Java... Simple unzip it to C:\program files\ or any where else.

Download Starter package for Android SDK from Unzip it and run the SDKManager.exe. Select the version of sdk which you want to work with. I prefer to download two versions 1.5 and 2.1/2.2

The last download is the ADT plugin for eclipse. which is a bit tricky. Goto Help->Install New Software and press Add. Insert this in address tab :  "". It will show "Developer Tools", select all and press Finish; Downloading will start. Restart Eclipse after installation....

Also set the android sdk location in Window->Preference->Android in Eclipse.

Thats it.... You have set the Android Development Environment... 

Note: Above is not the only way to develop for android. its just what i thinks to be easy to configure...

Feel free to ask any thing... your comments will be highly appreciated.

Farhan Ahmad