Swan-dive into Android development!
I’ve decided to take a crack at developing my own android game. I figure, since I’m getting an android phone soon (hopefully…come on Rogers! I want my Samsung Captivate!) I should learn how to develop things for it
I’m going to use this post to walk you through what I did and what I’ve learned while doing it to hopefully save someone out there some headache.
So first thing’s first, I’m not going to dive in straight to a game. Nope. We have to create a hello, world app first! At this point I’ve already installed the Android SDK and Eclipse, along with the ADT plugin and a virtual device (run: android create avd –target 2 –name my_avd or use the SDK Setup.exe program included in the Android SDK). I really have no idea when I did this, but I found the folders all ready and configured on my system
Hurray past me! There’s lots of documentation on how to set this all up, and it’s relatively straight-forward so I’m not going to go through it all here. Follow the links if you’re having trouble. I’m also going to assume you have at least basic experience working with the Eclipse IDE and know enough Java to follow along.
Hello, World!
Note: A lot of this section is taken from the Hello, World tutorial found on the android website as that’s what I followed myself.
Create Project
Begin by creating a new Android project in Eclipse. Press CTRL+N (or File -> New -> Other from the menu) and choose Android -> Android Project. Give your project a name, and choose a Build Target. If none show up, then you must use the Android SDK to download the libraries you wish to use. Personally, I set mine to target Android 2.1-update1 as that’s what a lot of phones currently have (or will have soon) and also what the phone I’m getting will ship with. After that, set your Application Name – this will show to your users in their android phone menu. It can be changed later if you like. The Package Name is what your java classes will live under; I used theootz.helloworld. The Activity is the class that will be created for you by the IDE. An android application is a collection of one or more activities. I just called mine the same as the Application name. Finally, the Min SDK Version tells Android what your application requires to run. If you look at the Build Targets you will see an API Level column. Any of these numbers is valid for the Min SDK Version. But you’d probably want to set it to be the same as what you chose for your Build Target. If you picked Android 2.1-update1, then this is 7. Once all your information is set, you can click Finish.
O, hai dere world!
And now to get to some actual coding. Creating a Hello, World! app in Android is actually pleasantly simple. The IDE will have created a base class for your application to start with, so lets go ahead and open that up. In your project, use the package explorer to browse to src -> your.package.name -> activity_name.java and open it in the editor. The actual path will vary depending on what you chose when you created the project but there’s only one file so it should be easy enough to find
You’ll get some code that looks similar to the following:
package theootz.helloworld;
import android.app.Activity;
import android.os.Bundle;
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Now we want to create our View (a drawable object to use in our UI) and set the text to “Hello, World!”. So naturally, we’ll use the TextView object
Replace line 11 with the following lines of code:
TextView tv = new TextView(this);
tv.setText("Hello, World!");
setContentView(tv);
What we’re doing is creating a TextView object, setting it’s text value, and then telling our application to use this View for our Activity’s UI. You may notice that the TextView class will not get picked up as a valid object by the IDE. This is because we haven’t imported the right package. You can either add the following line:
import android.widget.TextView;
Or use Eclipse’s awesome package management to do this for you by pressing Ctrl+Shift+O.
Your final code should look like this and is now ready to run:
package theootz.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// display our text
TextView tv = new TextView(this);
tv.setText("Hello, World!");
setContentView(tv);
}
}
Go ahead and run the project, and if all goes well the android emulator should start up and begin your application!
OH NOES! “ERROR: unknown virtual device name”
Oh gawd, an error… but it’s only a Hello, World! app what can possibly go wrong?! ![]()
Not everyone will get this, and if you didn’t then you can go ahead and skip to the next part. If you did, you’re probably on windows :/ Turns out that the Android SDK will look for the Virtual Devices in “%USERPROFILE%\.android” but the SDK will create the .android folder in “X:\.android” where X is the drive letter you installed to. So this leaves you with two options:
- Move the folder to your user profile folder (start -> run -> %USERPROFILE%). The problem with this is, if you create new virtual devices you’ll have to manually move them each time as well.
- Create a symbolic link. This is the route I went.
So how do you create a Symbolic Link? Start by opening a command prompt (start -> run -> cmd). Browse to your User Profile folder (something like C:\users\<username>). Then type the following command:
mklink /D .android X:\.android
Replace X with the drive letter that has your actual .android folder. And that’s it! Run your application from Eclipse again and you should see the Virtual Device pop up with your app ![]()
What’s Next?
Well, for me it will be creating a first game. If you want to take smaller steps in between then you can read the rest of the Hello, World! tutorial or others on their dev site.
Thoughts
So developing on android, at least on a simple app, seems fairly comparable to creating a Windows Phone 7 app. I think I prefer the MS approach at this point because it uses a lot of systems I’m already accustomed to – i.e. .NET and WPF. Also, I’ve grown to like the .NET development environment (at least in ASP .NET) over the last few months as I’ve been using it at work. It seems to grow on you pretty quickly. Granted, I haven’t created a full out app in either system yet, so hard to say how it will be once I dive a bit deeper.
Buuuuuuuut I will be creating my game soon for Android and will be creating a post as I do to keep track of everything that happens (already started, actually..but its hiding from the world as a Draft). Eventually, I’d like to re-create the same game in the Windows Phone 7 system and see what I like/dislike about each platform. I thought about doing the same with the iPhone but I’m not gonna buy a mac just so I can develop on Jobs’ precious – so forget that. If by some chance I end up buying an iPhone instead of the Galaxy S, then I’ll consider finding a work-around
(a VM seems most likely solution).
Possibly related posts:


