Mobile Me(dia)

Shawn Van Every
Spring 2009

Week 9 - Android

Android Introduction

Android, despite being a baby has an interesting history. A group of companies including Google setup an organization called the Open Handset Alliance whose mission is to:

"to accelerate innovation in mobile and offer consumers a richer, less expensive, and better mobile experience. Together we have developed Android&tm;, the first complete, open, and free mobile platform."

As of August 2005, The Android platform was being developed by a company called Android outside of Google. In August 2005, Google purchased the company and we are now seeing the fruits of that acquisition.

Android is only an operating system, the G1, manufactured by HTC, a member of the Open Handset Alliance is merely the first phone that supports Android out in the market. It is sold as a locked device through T-Mobile (although you can unlock it for free after 90 days of service or if you pay for it from companies offering the codes online). (There is also a developer version of the phone available through Google that is unlocked already.)

The Android OS is built on top of Linux and is (almost) completely open source meaning that it can be modified and distributed freely.

The Android OS itself is written using Java but not Sun's Java, a different implementation so that Google or whomever does not have to pay Sun royalties. The same goes for the development of Android applications.

More Information: Dalvik: how Google routed around Sun's IP-based licensing restrictions on Java ME
Android Source Code
Main Android Site

Getting Started - Setup

Setup of the tools for Android development is slightly complicated but not terrible. Let's walk through it:

The Android Developer Site:

First, Get the SDK: You can download the latest version of the Android SDK from

Then, Install the SDK: After you download, extract and move the SDK, you have to setup your environment. The first thing you have to do is tell your computer where the SDK lives. The means to do that requires using Terminal on the Mac.

The following assumes I have moved the sdk to /Developer/Java/ and it is called "android-sdk-mac_x86-1.1_r1".

Launch Terminal and type:
		nano .profile
That should put you in an editor window. Near the bottom of the file, you need to put in the following:
		export SDK_ROOT=/Developer/Java/android-sdk-mac_x86-1.1_r1/
		export PATH=$PATH:$SDK_ROOT/tools
Following that, you need to save the file by typing CNTRL-o, return and then CNTRL-x to exit. Last, you need to execute that file:
		source .profile

That's it for the SDK.. Now we move on to Eclipse.

Download Eclipse: You probably want "Eclipse IDE for Java Developers".

After you install Eclipse, you need to install the Android Development Tools (ADT): (Don't forget about the Eclipse preferences)

Now you are ready to go!!!

More Information:
Getting Started with Android using Eclipse

Hello World

In Eclipse, choose File: New: Project: Android Project
Project Name: Something for your reference, will be the Eclipse project name
Package Name: com.yourname.something
Activity Name: Application Name (no spaces) (Activity == A Java Class)
Application Name: How it will appear on the phone

After you do that it will show up in Eclipse on the left under Package Explorer with your Project name.

Click the triangle to the left and see what is in there.

Under "src", you will see a file. This is the main place to write your code.

Change the public void onCreate method to the following:
public void onCreate(Bundle savedInstanceState) {

	TextView tv = new TextView(this);
	tv.setText("Hello World");
add in import android.widget.TextView; with the rest of the imports.

Now you should have an app that when run, say's Hello World!

You can run it by clicking the green triangle at the top and selecting Android Application. (If you have a G1 plugged in to your computer, it will probably run on it. If not, it will run in the emulator)

To make this an Application that you can send to other people who have Android phones, right click on the package and click Android Tools: Export Unsigned Android Application. That should create an apk (Android Package) file that you can distribute.

Android UI Concepts

You will notice in the Eclipse package that their is a "res" folder which contains a "layout" folder. Inside the layout folder is a "main.xml". This main.xml file is what you can use to define a layout without having to do it programatically. It looks something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:text="Hello, I am a Button" />
The User Interface section of the Android Deveveloper's Guide explains it in detail.
You will notice that I have a button and textview in the above example. Both of these have an id which is set to be @+id/button or @+id/text. Those values are defined in the strings.xml file in the res/values directory: More Information:

Doing Something

Mostly from here:

import android.content.Context;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.hardware.Camera;

public class APicture extends Activity {
	Camera mCamera;
	Preview mPreview;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        // Create our Preview view and set it as the content of our activity.
        mPreview = new Preview(this);

class Preview extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    Camera mCamera;

    Preview(Context context) {

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        mCamera =;

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        mCamera = null;

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);

More Information:
Class Reference
Sample Code

Next week, we'll look at doing some more...