Introduction
Android is an Operating System for mobile devices developed by Google, which is built upon Linux kernel. Android competes with Sambian OS, Apple's iOS (for iPhone/iPad), RIM's Blackberry, Microsoft's Windows Phone (previously called Windows Mobile), and many other proprietary mobile OSes.
Android Platform
Android is based on Linux with a set of native core C/C++ libraries.
Android applications are written in Java. However, they run on Android's own Java Virtual Machine, called Dalvik Virtual Machine (DVM), which is optimized to operate on the mobile devices. The DVM, unfortunately, is not binary-compatible with the standard Java Virtual Machine (JVM) provided by Sun Microsystem (now Oracle).
Android 2.3 supports the core Java SE API (aka JDK), except the graphics packages AWT and Swing. Android supports 2D graphics via its custom library, 3D graphics via the OpenGL ES, databases via a lightweight SQLite, web browser based on the open-source WebKit engine. It supports common audio, video, and image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF), GSM Telephony, Bluetooth, EDGE, 3G, and WiFi, Camera, GPS, compass, and accelerometer.
Android 3.0 targets the tablets, with bigger screen and better processors.
Android 4.0 [TODO]
The mother site for Android is www.android.com. For developers, visit http://developer.android.com to download the SDK, tutorials and sample codes.
How to Install Android SDK
Step 0: Read the Documentations - Start from Android's mother site @ http://www.android.com. Goto "Developers" @ http://developer.android.com/index.html. Select "Download" @ http://developer.android.com/sdk/index.html. Read "Installing the SDK" @ http://developer.android.com/sdk/installing.html.
Step 1: Download - Download the Android SDK from http://developer.android.com/sdk/index.html. Choose the ZIP version for your platform (e.g., "android-sdk_r15-windows.zip" (about 34M)).
Step 2: Unzip - Unzip the downloaded file into a directory of your choice. DO NOT unzip onto the Desktop (because its path is hard to locate). I suggest using "d:\myproject". Android SDK will be unzipped into directory "d:\myproject\android-sdk-windows". For ease of use, we shall shorten and rename this directory to "d:\myproject\android". Hereafter, I shall denote the android installed directory as $ANDROID_HOME.
Step 3: Setup PATH - Include the android's tools directory ($ANDROID_HOME\tools) to your PATH environment variable.
For Windows: Start "Control Panel" ⇒ "System" ⇒ (Vista/7) "Advanced system settings" ⇒ Switch to "Advanced" tab ⇒ "Environment variables" ⇒ Choose "System Variables" for all users (or "User Variables" for this login user only) ⇒ Select variable "PATH" ⇒ Choose "Edit" for modifying an existing variable ⇒ In variable "Value", APPEND your $ANDROID_HOME\tools directory (e.g., "d:\myproject\android\tools"), followed by a semi-colon ';', IN FRONT of all the existing path entries. DO NOT remove any existing entry; otherwise, some programs may not run.
Step 4: Install Eclipse ADT Plugin - Eclipse is an open-source IDE that supports Android application development via a plugin called ADT (Android Development Tools).
If you have yet to install Eclipse, read "How to Install Eclipse and Get Started".
To install Eclipse Android Development Tools (ADT): Launch Eclipse ⇒ "Help" ⇒ "Install New Software" ⇒ In "Work with" box, enter https://dl-ssl.google.com/android/eclipse/ ⇒ Check "Development Tools" ⇒ "Next" ⇒ "Finish" ⇒ Restart Eclipse to use ADT plugin.
Step 5: Add Android Platform and Other Components - The Android SDK does not include the various Android platform versions (e.g., Andriod 2.3, Android 3.2) and google API. Run (double-click) Android's "SDK manager" from the Android installed directory (e.g., "d:\myproject\android") ⇒ "Accept All" ⇒ "Install".
Write your First Android Program Using Eclipse
Android apps are written in Java, and use XML extensively. I shall assume that you have basic knowledge of Java programming and XML.
Step 0: Read - Read "Hello, world" tutorial at http://developer.android.com/resources/tutorials/hello-world.html.
Step 1: Create a Android Virtual Device (AVD) - AVDs are emulators that allow you to test your application without the physical device. You can create AVDs for different android platforms (e.g., Android 2.3 for phone, Android 3.2 for tablet) and configurations (e.g., screen sizes and orientations, having SD card and its capacity).
- Launch Eclipse. From "Window" menu ⇒ "Preferences" ⇒ "Android" ⇒ In "SDK Location", enter your Android SDK installed directory (e.g., "
d:\myproject\android"). - From "Window" menu ⇒ "AVD Manager" ⇒ "Virtual Devices" ⇒ "New" ⇒ "Create New Android Virtual Device dialog appears.
- In "Name" field, enter a name such as "
my_avd23". Choose a target platform, such as Android 2.3 for testing on smart phone ⇒ "Create AVD". - Repeat the steps to create another AVD called "
my_avd32" with target platform of Android 3.2 for testing on tablet.
Step 2: Create a new Android Project
- From Eclipse's "File" menu ⇒ "New" ⇒ "Project..." ⇒ "Android Project" ⇒ "Next" ⇒ The "New Android Project" dialog appears.
- In "Project Name", enter "
Hello" (this is the Eclipse's project name) ⇒ "Next" ⇒ In "Build Target", select your target device ("Android 2.x" for phone; or "Android 3.x" for pad) ⇒ In "Application Name", enter a name that will show up with the program icon, e.g., "Hello". In "Package Name", enter "com.mytest" (this is the Java package name). In "Create Activity", enter "HelloAndroid" (this is the main Java class name for the android application) ⇒ "Finish". Ignore the warning/error messages, if any.
(You should set the target at the lowest possible level that supports your application requirements. This is because your codes will be able to run on the later versions, but not vice versa. Setting to lowest level will maximize the number of devices that your application can run on. These examples should be supported in Android 2.0, or even any version above 1.6.)
Step 3: First Android Program to say "Hello, world"
In Eclipse's "Package Explorer" (left-most panel), expand the "Hello" project node ⇒ "src" ⇒ package "com.mytest" ⇒ Open "HelloAndroid.java" ⇒ Modify the codes as follows:
package com.mytest; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textView = new TextView(this); // Construct a TextView UI textView.setText("Hello, world!"); // Set text for TextView setContentView(textView); // This Activity sets content to the TextView } }
Step 4: Run the Android Program - Run the application by selecting the "Run" menu ⇒ "Run" ⇒ "Android Application". Be patient! It takes quite sometimes to fire up the emulator. Watch the Eclipse's console for messages. Unlock the Android device, by holding and sliding the unlock bar to the right (or left). You shall see "Hello, world!" displayed on the emulator. (If your program is not launched automatically, try launching it from the application launcher manually, after the emulator is up.)
![]() |
![]() |
| Android 2.3 | Android 3.2 |
Dissecting HelloAndroid.java - Activity and View
An application could have one or more Activity. An Activity is a single, focused thing that the user can do. Our Android Java class HelloAndroid extends from the Activity class, and overrides the onCreate() method. The onCreate() is a call-back method, which will be called by the Android System when the activity is first created.
A View is a drawable object (or UI component). We construct a TextView (which is a subclass of View), and set it to contain the String "Hello, world". We then set the Activity screen to this TextView.
Android Application Structure
The Android project (under Eclipse ADT) consists of several folders:
src: Java Source codes. The Java classes must be kept in a proper package with at least two levels of identifiers (e.g.,com.mytest).res: Resources, including drawable (e.g., images, icons), layout (UI components and layout), values (e.g., locale strings).gen: Generated Java codes (e.g., to reference the resources) by Eclipse's ADT.AndroidManifest.xml: The manifest to describe the structure of the application.bin: Compiled bytecodes and packaging information.
The binaries together with their resources are bundled into an Android Package Archive file (with ".apk" file extension) via the "aapt" tool, for distribution and installation into the mobile devices.
Android Application Descriptor File - AndroidManifest.xml
Each Android application has a manifest file named AndroidManifest.xml in the project's root directory. It descibes the application components.
For example, our "Hello" application, with an activity HelloAndroid, has the following manifest (generated automatically by the Eclipse ADT):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mytest"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".HelloAndroid"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
- The
<manifest>element specifies the package name. It contains one<application>element. - The
<application>element specifies the icon and label used in mobile device's "apps" menu. It contains one ore more<activity>elements. - This application has one activity named
HelloAndroid. The<activity>element declares its program name (.HelloAndroidwhere'.'is relative to the packagecom.mytest); and label (displayed as window title). It may contain<intent-filter>. - The
<intent-filter>declares that this activity is the entry point (android.intent.action.MAIN) of the application when launched (android.intent.category.LAUNCHER).
Beside declaring the application's components, it also provides references to external libraries, and specifies the permissions.
Using XML Layout
Instead of writing program codes to create the UI. It is better, more flexible, and recommended to layout your UI components via a descriptive XML layout file. In this way, you don't need to hardcode the views, and you can easily modify the look and feel of the application by editing the XML markups. The programming codes can therefore focus on the business logic. (This is similar to the Model-View-Control (MVC) framework used in web applications where the views are written in JSPs using markups and the controls in Servlets, which are clearly separated. This is also similar to client-side programming where HTML is used for contents, CSS for presentation and JavaScript for programming logic. Again, views, contents and programs are clearly separated).
To improve the performance, the XML files are compiled into binary using the Android Asset Packaging Tool (AAPT). The mobile devices stores them as binary, and the file is read in binary, instead converting back to XML.
Let's rewrite our hello-world to use XML layout.
- Create a new Android project called "
HelloXmlLayout". Use "Hello in XML" for the application name, "com.mytest" for package name, "HelloAndroidXML" for the activity. - Expand the "
HelloXmlLayout" project node. Expand theres/layoutnode and open themain.xml. Eclipse provides two different views: Graphics Layout and XML. Select the XML view and enter the followings:<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/hello" />
Ignore the warning on "no DTD/XML Schema". - The
main.xmlfile declares aTextViewthat holds a text string. Instead of hardcoding the string content, we create a string reference calledhello, with the actual string coded inres/values/strings.xml. This approach is particular useful to support internationalization (i18n) and localization (l10n), as you can customize different strings for different locales. - Expand
res/valuesnode and openstrings.xml, and enter the followings:<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello, world! (via XML layout)</string> <string name="app_name">Hello in XML</string> </resources>We write the actual string for the referencehello. The reference "app_name" holds the string for the application name (defined while we create the project). - Next, modify the
HelloAndroidXML.javato use the XML layout (instead using programming codes to produce the layout), as follows:package com.mytest; import android.app.Activity; import android.os.Bundle; public class HelloAndroidXML extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Use "res\layout\main.xml" to layout UI components } } - Run the application (select "Run" menu ⇒ "Run"). You shall see the new string displayed on the emulator.
- The Eclipse ADT automatically generates a
R.java, which keeps track of all the application resources, inhello\genas follows:package com.mytest; public final class R { public static final class attr { } public static final class drawable { // Inner class R.drawable public static final int icon=0x7f020000; // Resource ID R.drawable.icon } public static final class id { // Inner class R.id public static final int textview=0x7f050000; // Resource ID R.id.textview } public static final class layout { // Inner class R.layout public static final int main=0x7f030000; // Resource ID R.layout.main } public static final class string { // Inner class R.string public static final int app_name=0x7f040001; // Resource ID R.string.app_name public static final int hello=0x7f040000; // Resource ID R.string.hello } }TheR.java(R stands for resources) indexes all the resources used in this application in the inner classesattr,drawable,id,layout, andstring. For example, the inner classlayout's propertymain(R.layout.main) referencesres\layout\main.xml; the inner classstringreferencesres\values\strings.xml; the inner classdrawable's propertyiconreferencesres\drawable\iconwhich keeps the images used as icons in "apps" menu. - Check out the manifest
AndroidManifest.xml. Note the change in the activity name.
Sending the Program to Actual Devices
[TODO]
Next?
Try running the Hello-world targeting Android 2.3 (phone), Android 3.2 (tablet), and Android 4.
Read the "Hello-xxx" tutorials in Android Developers, under the "resource" tag (http://developer.android.com/resources/index.html).
Study the sample codes (in Android's "samples" directory, with some explanations in "Sample Code" as above), especially "API Demos".
To run the sample program in Eclipse:
- Select "File" menu ⇒ "New" ⇒ "Project..." ⇒ "Android" ⇒ "Android Project" ⇒ the "New Android Project" dialog appears.
- First, choose your "Build Target" ⇒ Then, check "Create project from existing sample" ⇒ Browse and select the desired sample. Once the sample is selected, the rest of the fields will be filled automatically ⇒ Finished.
- Select "Run" menu ⇒ "Run" ⇒ "Android Application".

