by Dennis Sheil of Panacea Supplies
Last updated: June 19, 2011
You can e-mail me: "dsheil at vartmp dot com". E-mail me especially if you find errors, or the like. If you have questions about how to do something in Android, I would try Stack Overflow first before e-mailing me though. And read the Android Developer's Guide.
This guide is intended for people who have an intermediate knowledge of Java, but are completely or mostly unfamiliar with the Android API. A little bit of knowledge of how a Linux system works is helpful as well, although if you know Java well, you can learn the Linux stuff as you go. It's expected you know your way around a computer and the Internet somewhat, but know little to nothing about the Android API
The first thing you need to know about developing for Android is you don't need an Android phone to develop for the Android! It's a good thing to have, but there are free Android emulators from Google, free IDEs like Eclipse to develop on and so forth, so you can start developing without spending a dime on anything aside from your computer. It's good to test your application on a real phone before pushing it out to the whole world, but you can borrow a friend's Android to test it if need be, or even go down to your local phone/electronics store where they often have working, net-connected Androids on display.
Android has instructions for what to do next which you can read if you want. I just cut a lot of the blather out here, and also tell you the good things to do which they won't, such as to avoid installing the Android 3 API, since it is currently (June 19, 2011) slow and buggy, and you should wait a few months to download it as they should be releasing a nicer one in a few months, hopefully.
So go in the unpacked SDK directory and run tools/android. The Android SDK and AVD manager will pop up. Go to Available Packages and then Android Repository. It will say a bunch of stuff. What you need the most (as of June 19, 2011) in order of importance is:
1) Android SDK Tools
2) Android SDK Platform-tools
3) "SDK Platform Android 2.3.3, API 10".
With those three things you can start. But also get:
3) Samples for SDK API 10, revision 1
4) Documentation
It's also a good idea to get an earlier version of Android, 2.1 would be my choice. So get
5) "SDK Platform Android 2.1-update1, API 7".
6) Samples for SDK API 7
If you have room and time, grab everything, why not.
Note: These are not the best specs for *every* emulator, just for your first one. Later you will make other emulators - with target 2.1, API 7. Or with an HVGA skin. Or whatever. But you have good specs for the first one. So Create AVD.
So now you made your first emulator. Highlight it and hit Start. If it is too big for your screen, that is often adjustable. It takes a while for the emulator to load. So learn this lesson while you are impatiently waiting for the emulator to load - you should keep your emulators running while developing. They take a minute (or more) to start, so why keep wasting that time opening and closing it? Keep it open all the time until you're done working for the day and then close it.
OK your Android emulator came up. If your computer is Internet connected, go to the Browser application on the Android emulator and see if you can browse the web. You should be able to. Your emulator does not have the Market app, does not have a camera and so forth, but it has a lot of what a normal Android has. Play around with the emulator a little, especially the dev tools and settings apps.
OK you know how to write Hello World apps with Eclipse now. Now download the latest Android Development Tools plug-in for Eclipse. This page explains how to do that. When the ADT plug-in first came out, it was buggy and had a lot of hassles associated with it. But now it is years later - and the ADT plug-in is still buggy and causes hassles! But it is a little better then it used to be. You should be able to at least install it nowadays without a problem. One thing you'll do when installing the plug-in is pointing to that directory you unpacked the SDK into. If you ever move that directory, you will have to point your Eclipse ADT plug-in to the new location.
"Tip: After you have pasted sample code into an Eclipse project, press Ctrl (or Cmd) + Shift + O to import the required packages."
This is a very good tip and will save you many headaches.
After doing Linear layouts and Table layouts, do a tab layout. It's a little harder, but not that hard. Then do a ListView. ListView's are a little complicated, but if you follow the tutorial, that one should work. You can do the Spinner widget as well. Like the ListView, this is more complex then the other stuff, but it works from the tutorial.
Later on that page it says: "Therefore, unlike applications on most other systems, Android applications don't have a single entry point (there's no main() function, for example)." Correct. Android is first going to look in your AndroidManifest.xml. The main action and the launcher category usually will be associated with one particular activity class, and that is the class which will launch initially. Android won't be looking for main(), it will be looking for that Activity's onCreate() method.
What is an Activity? "An activity represents a single screen with a user interface." Correct. Your Activity classes will be normal Java classes that extends the Activity class. They will display something on the screen. Period.
"You can start an activity (or give it something new to do) by passing an Intent to startActivity() or startActivityForResult() (when you want the activity to return a result)."
Correct. You have one class. A class that extends Activity. A class that displays something to the screen. Maybe it is a TextView view. Maybe it has a LinearLayout Layout. Maybe it is a table layout. Maybe it is a ListView. Maybe is is a screen with some kind of layout or View with a button on the screen.
You want to go to a different activity. A second Activity-extending class you wrote, that does something different than your first Activity extending class. That looks different than your Activity extending class. An Intent gets you from one Activity class to another.
(Can you pass variables like Strings through the Intent? Can you send a URI "pointer" to data through an Intent? Yes. But don't worry about that yet.)
You create an Intent object. One of the parameters sent to the constructor will be the other Activity class you want to launch. You then run startActivityforResult() (or if you want, startActivity() ) with that Intent object as a parameter. Boom, you should now be in your other Activity class. Does this sound confusing? Believe me, it is 1000 times less confusing then everything that I read explaining this.
I said to make a WVGA800 screen for your first emulator. Which is 480x800. Which is what most people have (many have 480x854). Most people have this, in high density (about 240 dots per inch). The screen is usually about four inches. This is the most common. So test in this emulator first.
Some people have older (or cheaper) phones of the same size (four inches) but in medium density (about 160 dots per inch). You should have an emulator for this as well - make an emulator with an HVGA screen, with a density of 160. Test your app in this as well. Less and less people have these nowadays, but there are still a significant number of people who still have these phones (as of June 19th, 2011). So see how your app looks on this emulator as well. When doing layout, use "dp" instead of "px", to avoid some of the work of dealing with all of these sizes. Theoretically, you can test a million sizes, and densities, and so forth, but testing for normal sized medium density and normal sized high density covers 95% of what is out there - the older medium density phones, and the more common newer high density phones. Also make sure to make a WVGA854 target emulator, not just a WVGA800. You normally have to do very little work to make WVGA854 look nice if WVGA800 looks nice.
What else? Google recommends you target the latest API, and fallback for other stuff. Once you know what you're doing, that is a good idea. For now, target the 2.1 API, and do the 2.2, 2.3 bells and whistles if the phone running your app is running those versions. About 28% of phones still run an API that is 2.1 or earlier. As time goes on that number will diminish.
Here is the deal: Eventually, you *do* want to use a lot of XML. It makes your life easier on Android. But. There is a steep learning curve, or at least a decent-sized one. Not just for xml, but for the Andoid way of using xml. So do a little xml dabbling right off the bat, but learn how to do everything programmatically in java code. Write some Android programs that don't use xml at all, except for maybe looking up the name of the application. You are a programmer, not an XML layouter! Learn how to do absolutely everything in Java code if need be - LinearLayout, Tables, Buttons, TextViews, ListViews etc. Once you can do it one time in pure Java, once you at least are capable of doing it in pure Java, if only doing it that one time, *then* start using it the XML way.
XML does make life easier on Android. You lay it out and that's it. XML is especially good for static screens. XML is even good for screens where the widgets and views (buttons, TextViews etc.) are statically placed, but the words on them are dynamic - you can target things by the ID before you do a setContentView() call. When things are really, really dynamic, it runs into a little trouble, or a need for XML wizardry anyhow. Learn how to make everything in code if you have to. Then start adding the XML parts when it makes life easier for you. Especially if the screen layout is somewhat static. To learn how to do things in Java code, search stackoverflow.com for stuff like "Android programmatically textview" or "Android programmatically button" and the like. Android's documentation explains how to do this as well, although sometimes in a convoluted manner.
In all sincerity though, I like what Google has done, I like the open platform nature of Android and a lot about it. But their documentation, their Eclipse ADT plug-in, their slow as hell Android 3 emulator and the like can sometimes cause frustration. I have a lot of positive feelings about Android, even though this document might not reflect it. Maybe reading this will help you avoid some of the anguish and anxiety I went through initially.