Android First Example: Hello World


Android First Example: Hello World
G.Morreale

Introduction:

Android is a software stack for mobile devices that includes an operating system, middleware and key applications.
More details link: http://code.google.com/intl/it/android/documentation.html

The Android SDK contains API necessary to begin developing applications on the Android platform using the Java programming language.
In this article I point out the steps for making the hello world example and running it in the emulator.

The Steps:

In order to write the example follow these steps

* The Android SDK


o Download the sdk (select your developing platform) http://code.google.com/intl/it/android/download_list.html

o Extract it into your hard drive directory


* Java Code


o Make a new java project
o set in the project class path android.jar (you can find it in sdk zip file)
o make a new java class into Hello.java
o copy this source code in Hello.java:


//package name must be composed of at least two java identifiers
package my.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Hello extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Make a new text view passing Activity object
TextView tv = new TextView(this);
//Set a text into view
tv.setText("Hello World");
//set the view into activity view container
setContentView(tv);
}
}


o compile Hello.java code


* Build and install The apps


o In tools sdk directory you can find activitycreator.bat, launch it by passing the complete package name and class name:

activitycreator -o c:\android my.android.Hello


o The script prepare the android application putting it into c:\android directory


o Now you need ant tools in order to build the android application

Download it from: http://ant.apache.org/bindownload.cgi


o Go in c:\android directory
o Launch: "ant -f build.xml" command

(note: if you go in "Unable to locate tools.jar" error you must set correctly jdk classpath.)

you'll be left with a file named Hello-debug.apk under the 'bin' directory

o Go again in tools sdk directory.

Now you can use adb command in order to install the apk file into android emulator


o First launch the emulator (if you don't adb command fails)

(note: in order to launch emulator you must execute the emulator.exe command in sdk tools directory.)


o Then launch the follow command: adb install c:\android\bin\hello-debug.apk


* Run The example


o Close the emulator
o Re-launch the emulator


o Click on the arrow in the bottom of the screen so you open the application list, now you can