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. 

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

    • Extract it into your hard drive directory

  • Java Code

    • Make a new java project
    • set in the project class path android.jar (you can find it in sdk zip file)
    • make a new java class into Hello.java
    • 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);
    }
}

    • compile Hello.java code

  • Build and install The apps

    • 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

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

    • Now you need ant tools in order to build the android application
Download it from: http://ant.apache.org/bindownload.cgi

    • Go in c:\android directory
    • 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

    • Go again in tools sdk directory.
Now you can use adb command in order to install the apk file into android emulator

    • 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.)

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

  • Run The example

    • Close the emulator
    • Re-launch the emulator

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

    • find hello application.. click on it to see the hello word string..


Conclusion 

It is only a small example introducing android development.
The next step is to go deep into

Please leave a feedback in the comment to this post.

6 comments:

Android said...

Hi, we should do an android project. I'm still thinking my first real world application for a tutorial.

Anonymous said...

Documentazione in italiano:

http://redomino.com/it/labs/progetti/google-android/

Tseng said...

Nice guide for people who don't use eclipse. Updated my Post to link to this entry.

For people who are interested in another starters tutorial which goes bit further than "Hello World", check out my Blog post too

http://tseng-blog.nge-web.net/blog/2009/01/29/android-your-first-android-application/

Giuseppe Morreale said...

You are welcome tseng. I see you blog, I find it very good!

jessica said...

Here is complete details for a starter from creating environment to writing an helloworld app

http://www.ceveni.com/2009/06/writing-android-hello-world-program.html

Anonymous said...
This comment has been removed by a blog administrator.