Ground-Up Android, Part 2: Our First Project

by Yoni Samlan on Oct 7, 2009

This is part 2 of a 5-part series on introductory Android development. If you’re just arriving, you should head on over to Part 1.

In the first part of the tutorial, we got Eclipse, the Android SDK, and the ADT set up. In this part, we’ll get into our environment, create our first project and poke around in it, and test out the Android emulator.

Fire It Up

Go ahead and fire up Eclipse, and if it asks, choose a workspace (the default’s probably fine for now). Click on the “new android project” button or go to File -> New -> Android Project.

Click the New Android Project button.

Click the New Android Project button.



Choose a name and settings for your project – we’ll call ours Twitternator. I’m choosing to target Android 1.5 for now, since that’s what’s running on most current production phones. 1.6 allows for some neat new features, but nothing we’re going to use here, so we may as well go for the lowest baseline possible. We’ll also leave out the Google APIs for now (those are used for Maps), since we won’t need them.

The New Android Project screen.

The New Android Project screen.

By targeting a specific SDK version and specific vendor libraries, developers have the ability to write apps for a base platform without worrying about users on older hardware and software – they simply won’t see the app in the Android Market, and will see a helpful error if they try to install it manually.

Then click Finish (there’s also an option to create testing code, which we’ll skip for now) and we’ll explore the structure of the folders we have now.

The structure of the project in Eclipse

The structure of the project in Eclipse

The AndroidManifest.xml describes your application to the rest of the Android system – what version of the OS it supports, what permissions it needs (for Internet access, GPS information, etc.), how other applications will use it (via Intents and ContentProviders, advanced ways for applications to call each other and share data), what the different screens (Activities) are in the app, and so on.

“src” contains our Java code, and the wizard pre-created an Activity for us. Think of Activities as the code representation of one screen of your application. Here’s the pre-created outline of an activity, which simply tells the system to render the view from a layout file called “main” (more on those in Part 3).

The initial code for our Activity

The initial code for our Activity

The “res” folder contains non-code resources; usually “drawable” will contain image files, “layout” will contain XML files you can use to build UI for your application in an easy-to-read way rather than writing it all out in Java code, and “values” will contain things like internationalized strings files, color definitions, or theme styling definitions to apply to your views.

Finally, the “gen” folder contains automatically created code you can think of as placeholders for items in your res folder, so you can easily point to a particular button, string, or image from your code. This speeds up references to that information and makes code completion easy, and it’s done automatically in the background while you work.

We actually already have a working Android app created here. Let’s test it out in the emulator.

Emulation Nation

There are already a half dozen phones in the wild running Android; dozens more will be coming out in the months to come. Each one has a variety of screen resolutions, software versions, and hardware controls. There’s no substitute for testing on actual devices, of course, but the Android SDK includes a good solution for modeling a variety of these in software.

The emulator in Android is based on the open-source qemu emulator, and you can create models called AVDs (Android Virtual Devices) with a variety of hardware and software capabilities. For this project, we’ll create a basic Android 1.6 device with an HVGA screen, a 32MB SD card, and the basic APIs.

Where the AVD Manager is hiding.

Where the AVD Manager is hiding.

Open the AVD Manager and create the new device.

The AVD Creator.

The AVD Creator.

Then go back to the Java view of our project. Let’s go back to TwitterPoster.java and debug it from Run -> Debug -> Android Project. It should auto-launch the emulator here.
Give it a minute for the first boot, then go ahead and your app should launch right up:

emulator-running

Aww, look, our first running Android process! You should go have that bronzed to commemorate the occasion.

Usually, you’ll keep the emulator open for your entire development session. One really great thing about debugging Android applications is that you can quickly edit your code, which builds in the background, then push it into a running instance of the emulator and see the results in seconds. You don’t have to wait to rebuild your code or launch new emulators; you get feedback nearly instantly.

So far, we’ve gotten our environment set up, explored the project layout, and launched the emulator. In our next post, we’ll pretty things up and explore the layout editing tools.

On to Part 3!

Tags: ,

About Yoni Samlan

Yoni Samlan is a partner and developer at Active Frequency. He's been programming since he was yea tall, and has been a professional Python and Django developer since 2005.

  • Pingback: Ground-Up Android: Getting Started with App Development « Active Frequency

  • Pingback: Ground-Up Android, Part 3: Describing Your Layout « Active Frequency

  • http://bozosort.com/ Seth H.

    I like how in depth you're going, which is good for application design, but baby sitting people through setting up eclipse is kind of pointless: If you plan on making real Android apps, you really ought to be able to follow the straightforward directions Google has already posted on d.android.com.

    Anyway, I hope you make tutorials for more complex apps, such as a guide on OpenGL on Android.

  • ysamlan

    Seth: That's a fair point, and I try not to get too specific about the setup of Eclipse itself. I assume the reader is competent enough to set their paths up, use the Eclipse plugin installer, and so on. I did want to make the tutorial is at least somewhat self-contained, though, and make it coherent and visually appealing to readers who are just skimming, not actually following along in the IDE.

    Also, the docs don't cover the newer ADT layout tools at all; they're not even shown in the tutorials. The drag-n-drop and XML properties editor are actually really nice for beginners who might not know much about XML namespaces and have trouble visualizing nested relative layouts.

    I probably won't get into OpenGL stuff, but I do plan to post a widget tutorial — the official examples from Google and Jeff Sharkey don't really explain some of the basic parts of the AppWidget framework as well as I'd like, while getting into far too much code that does more general non-widget-specific stuff like rolling their own calls to webservices and so on.

    If there's enough demand I may put up a post or two about undocumented high performance audio APIs (SoundPool), too.

  • Matt J.

    Why are people praising this as 'detailed'? It omits to mention many things, such as the need to enter what is in the PNG files. What is more, my up-to-date installation of Android and Eclipse does NOT show the same options as do the PNG files.

    For example, right now I am looking at “Create New AVD”, which offers no Android 1.6 option: only 1.5 level 3, 1.1 level 2 and Google API Level 3.

  • Matt J.

    Another thing that certainly should have been mentioned for beginners with Android: I had to unlock the screen of my emulated phone, wait for a warning message about failing to connect a debugger, and only then did I see the promised screen, for Twitternator.

  • ysamlan

    I'm not sure what you mean by “the need to enter what is in the PNG files.” There's only one PNG file used in this project, and it should be a drop-in replacement of the PNG file that already comes in the skeleton project ADT creates for you.
    As far as your AVD manager not showing you Donut options, make sure you have the 1.6 SDK installed, too, not just the newest ADT/Eclipse. http://developer.android.com/sdk/1.6_r1/index.html – and that the Android config settings in Eclipse are pointing to that installation. If you do a clean Android/Eclipse install according to Google's directions, that should be the case.

  • Matt J.

    I was referring to the PNG files in the tutorial web page, not to the PNG file in the project. If I follow the instructions in the text of your web page, I leave many of the text fields blank. But in the PNG file, you show them filled in.

  • ysamlan

    Matt,

    I was trying to strike a balance between giving as much detail for beginners as possible and overwhelming the user with unecessary or obvious directions, which would increase the length and apparent complexity of the tutorial without actually making the tutorial any easier to follow (see Seth's comment above — he felt the tutorial was *too* detailed in some ways).

    I felt that “unlock the emulator screen and wait for the debugger to connect” fell into the “unecessary” category, since just reading the screens presented to you should tell you what to do there (press Menu, and wait one second). That was really just a judgment call, and it could have gone either way… but I think it's OK this way, as you yourself illustrated: Although you seem to disagree about the necessity of telling that in advance, you did manage to figure out the necessary steps yourself :)

  • Matt J.

    I never do see a screen “press Menu, and wait one second”. And after pressing Menu, I have to wait much longer than just one second before I see anything.

    Could this be because my emulator is picking up a different AVD? I didn't see anything in the tutorial telling the user how to pick the right AVD: you seem to assume that the AVD created in the tutorial is the only one. Or could it be my emulator is already out of date? Somehow, this seems highly unlikely, since I first downloaded Android just a few days before 1.6 was released. Or is it really that different on the Linux platform? Have you tried following your own tutorial steps on a Linux installation of Eclipse?

  • ysamlan

    Ah – That's a fair criticism, but for the fields I don't step through, it's because what you actually put down in those fields is really unimportant — for example, you could name your package and Activity class anything you feel like (within the rules of any normal Java project), and your AVD can be named whatever you'd like. I didn't want to dump in excessive detail and give readers the false impression that there are some magic values for those fields that need to be filled in, and Eclipse itself warns you if you enter anything invalid for those fields. The fields I called out in the text are the important ones (SDK version, for example) where entering a wrong value might actually interfere with the reader's ability to follow along.

  • Matt J.

    I do understand the need for avoiding excessive detail. And to be sure, explicitly calling out for each field what can be entered would be a lot of detail. But there is a third way: you can say much as you have in this post, that the fields called out in the text should be entered as called out, the others are optional. And the PNG file should not show them filled in, since this contradicts the directions in the text.

  • ysamlan

    What I meant by reading the screens: when you switch to the emulator, the screen says “Press Menu to unlock” (like on an actual phone). Then when you unlock, it says “Waiting for debugger to connect…” which I think is pretty self-explanatory. (The exact time you have to wait depends on your hardware, OS, version of SDK, and AVD; I usually find it takes 1-2 seconds during my development on a Core 2 Duo Mac with OS X on a 1.6 SDK and AVD).

    If you are working from a clean SDK 1.6 and Eclipse install, and following along, then yes, you'd only have one AVD option available at runtime (the one you created in the tutorial). And if you installed the 1.5 SDK and created your own AVDs previously, then I assume you know how to manage them.

    I'm posting these tutorials to help the community for free; even most development books I've bought don't post particular instructions for Linux installations over the book's OS of choice. I can't possibly test and give directions for every possible hardware, OS, and legacy SDK/IDE configuration; I say at the top that I assume a clean Android 1.6/Eclipse 3.5 setup on OS X, but it should be straightforward to follow along on other platforms with minor modifications.

  • Matt J.

    First of all, I am simply not seeing the same things you are. Long before I see “Press Menu to unlock”, I see a screen completely blank except for the word 'Android', written in a funny font. Then it changes to another funny font, and I only see a direction to unlock after I myself press something — and even that is over 10 seconds after actually launching.

    Now I do not expect you to debug my qemu installation, nor to accommodate all its peculiarities in your tutorial directions. But make no mistake about it: when Google wrote their tutorials they tested it out on multiple platforms, and made sure that each line of their instructions makes sense on all of them. That is your 'competition'.

    Nevertheless, I do consider it fair to announce at the top of your tutorial that you are assuming a clean installation. But it seems to me that your own memory of what you wrote there is faulty. For what I am finding on the first page is quite different from “clean Android 1.6/Eclipse 3.5 setup on OS X.”. What I found contradicts that, saying, “You'll want a recent version (3.4 or 3.5) of the Eclipse IDE”

    This is a significant difference in wording. Even though you are doing all this for free, you will find it well worth your time to eliminate such discrepancies. More people will benefit from your efforts if you do.

    I found the above wording at http://activefrequency.com/blog/2009/ground-up-…

    Finally, I am not finding it all that straightforward to follow along, but the modifications are not all that major. And I am really learning something from the tutorial, so thank you.

    But I am expecting the most glaring discrepancies in Part 5;)

  • http://sergiandreplace.com/ Sergi and Replace

    Why is everybody blaming? Yoni is offering a free tutorial that, I assume, he's doing on his own time for free. Just welcome it and do not complain about it being too detailed.

    Yoni, thanks for your work.

  • Negi

    thanks for posting this tutorial!

  • Negi

    thanks for posting this tutorial!