Android Development with Eclipse–HelloWorld Tutorial

David Carron / Monday, December 31, 2012

Starting work with an unfamiliar tool can feel a little intimidating – there are so many unknowns that you never really know where to start and it’s hard to tell the important stuff from the things you’ll basically never touch.

In these cases I usually start with something that works and make tiny changes until it morphs into something that does what I want it to (and still works). The idea is that I’ll be able to try things out straight away and  work out what’s actually important as I’m going along.

Call it what you will, the venerable “hello world” program is often a very good place to start. Although it doesn’t seem do anything useful, this program establishes that you’ve understood the basics of editing source files, that the compiler is installed and targeting the correct platform, that programs can connect to the output device and – if you’re feeling adventurous – that the debugger works too.

Hello, World

“Hello world”, made its first public appearance in Kernigan and Richie’s The C Programming Language (more widely known as The White Book) and in those days it looked like this:

main()
{
   
printf("hello, world\n");
}

Get that into a file using vi (which was itself very much an adventure), type in the mystical cc incantation

cc –g helloworld.c

to get it to compile, followed by the equally mystical

./a.out

and we’d be gratified by seeing hello, world appear. The next step in the development cycle was typically to replace “world” with something obscene and see if the whole thing still worked.  That might not look like much but for undergrads hunched over their vt100s in the seventies and eighties, this was all ground breaking stuff and more than enough justification to slope off down to the bar for a celebratory drink.

In any case, times have changed and what seemed pretty neat in those days now seems dull. so let’s spruce up the whole thing by getting it to run on a fancy mobile device.

Android Style

In principle, there’s a lot of ways of writing an Android app, but as usual I’m going to assume that you’ve made the wise decision to use Eclipse and the ADT plugin.

Before we get started on actually writing the program, we’re going to have to make sure that we’ve got somewhere to run it. You can have as many real or virtual devices in as any many different configurations as you like, but to make sure you get the chance to choose between them, select “Always prompt to pick device” from the “Target” Tab of the “Run Configurations” dialog:

image

By selecting this, you’ll always the get the chance to choose how to run your app.

Virtual Devices

In case you don’t have an actual Android device to play around with, the SDK comes with a whole bunch of emulated devices. Open the device manager by selecting Window.. Android Virtual Device Manager. The device manager does quite a few things, but we’re interested in creating a device, so select “New” and fill in the device details:

image

Select “Ok” when you’re done, and you’ll see that your device has been added to the list in the device manager. Close the device manager and you’re ready to get on with the actual project.

Create The Project

Open the “New Project” dialog by selecting “File.. New.. Project..”, select “Android.. Application Project” then press “Next”

image

The “New Android Application” wizard opens.  Type in “HelloWorld” as the Application Name and the wizard fills in the rest of the required details itself. The other fields fall into the category of “stuff to worry about once you’ve managed to get something running” so you can leave it all as it is.

image

Click “Next” for this and the following wizard pages without changing anything else.

Run Your First Android Application

Select “Run.. Run As… Android Application” and choose an android device in the chooser window which opens

image

Starting the emulator takes a little while, but when it finally manages to boot up you’ll be rewarded by greetings from your very first Android application. That was pretty easy, wasn’t it?