Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Creating a "Hello, World" Application using Eclipse

Author: Ian Darwin
Published? true
FormatLanguage: WikiFormat

Problem:

You want to use Eclipse to develop your Android application.

Solution:

Install Eclipse, the Android SDK and the ADT plug-in. Create your project and start writing your app. Build it, and test it under the Emulator, from within Eclipse.

Discussion:

Once you have these items installed, you are ready to begin:

To get started, create a new project from the File->New menu.

Click Next. Give your new project a name, and click Next.

Select an SDK version to target. 2.1 gives you almost all the devices in use today; 3.x or 4.x gives you the latest features. You decide.

This figure shows the project structure expanded in the Project Panel at the right. It also shows the extent to which you can use Eclipse Auto-completion within Android - I added the 'gravity' attribute for the label, and Eclipse is offering a full list of possible attribute values. I chose center-horizontal, so the label should be centered when we get the application running.

In fact, if you set gravity to center_vertical on the LinearLayout and set it to center_horizontal on the TextView, the text will be centered both vertically and horizontally. Here's the version of the layout file main.xml which achieves this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:gravity="center_horizontal"
    />
</LinearLayout>

As always, Eclipse generates a compiled version whenever you save a source file. Also, in an Android project, it also runs an Ant Build to create the compiled, packaged APK that is ready to run. So you only need to run it. Right click on the project itself, and do Run As -> Android Project.

This will start the Android Emulator if it's not already running. The emulator will start with the word Android in typewriter text, then switch to the fancier Android Font with a moving white patch over blue lettering - remember the Microsoft Windows'95 startup?

After a little longer, your application should start up (and here we'll only show the screen shot of the application itself, since the rest of the Emulator view is redundant).

See Also:

Hello World - Command Line

GR8DAN 2012-02-27 07:31:58.427 Spelling mistake in second screenshot, MyAndroyd instead of MyAndroid
idarwin 2012-01-13 12:54:33.58 Thanks Dan, I'll try to get those repaired before the book gets into print.
GR8DAN 2012-01-12 05:08:56.12 The first two screenshots now differ from the currently shipped ADT plugin for Eclipse (more options under Android in the new project wizard, order of setting project properties is different). Final screenshot does not match the main.xml shown (only centered horizontally and not vertically as per the layout file, but does match screen shot of label setting).