Android classes - Day 1 - Installing the SDK

(October 2009)

Contents

Before you start ...

I am using ubuntu-linux 9.04 to develop my applications. Of course you can use different operating systems, but in my opinion ubuntu offers a lot of very good tools for developers - and they are free and easy to install! So maybe you want to checkout http://www.ubuntu.com and download a free ISO-image. You can also install it in parallel to your existing windows or whatever ...

Most of the time I am using just a simple terminal/bash/commandline to install things or configure something ... it's much faster and easier to explain, because you can just copy and paste the commands to your shell and run them. To open a commandline in ubuntu just go to "Applications -> Accessories -> Terminal" or just press Alt+F2 and type "xterm" ...

Agenda Day 1

Android(TM) Logo

installing java

I am using the latest sun java6 JDK ... feel free to use something else ...

sudo apt-get install sun-java6-jdk


download android sdk

Goto http://developer.android.com/ and download the latest SDK suitable for your plattform to your home-folder. For this tutorial I am using 1.6 R1 for linux ... you will have to read/accept a license agreement. Since the SDK is more than 200MB you might have 5 minutes to watch a video from http://developer.android.com/videos/

Once it's downloaded ... unpack the files:

# goto your home folder
cd ~

# verify the integrity of the downloaded file - just to be safe it's not corrupted or hacked ... compare it to the md5sum shown on the website
md5sum android-sdk-linux_x86-1.6_r1.tgz

# extract and unzip the tar file
tar -xvf android-sdk-linux_x86-1.6_r1.tgz -z

# make a symbolic link - just for easier typing and version changing ... it is not really needed
ln -s android-sdk-linux_x86-1.6_r1 android-sdk

Add the tools directory to your PATH environment variable - so you can start the sdk tools in your shell without giving the full path all the time. Open the .bashrc file in some text editor and add the following to the end of the file:

export PATH=${PATH}:/home/mrx/android-sdk/tools

installing eclipse

I recommend that you download the latest version from http://www.eclipse.org - the version shipped with ubuntu is too old. So goto http://www.eclipse.org and download "Classic Eclipse 3.5" or something like that. Just unzip the downloaded file to your home folder and start eclipse.

integrate eclipse plugin

configurations for eclipse

... done. Now you are ready for your first android application!

Run Hello World

Create Android virtual Device

Open a terminal and run:

android create avd --target 2 --name slash4avd --sdcard 256M

Output:

Android 1.6 is a basic Android platform.
Do you wish to create a custom hardware profile [no]
Created AVD 'test_avd' based on Android 1.6, with the following hardware config:
hw.lcd.density=160

The -target 2 option means to use the emulator to run your app on.

... now goto eclipse

Eclipse Project setup

Edit code

Find / Open the HelloWorld.java file in the left panel (maybe you need to click "goto workbench" before).

Paste the following code over the file:

package com.slash4.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);
 
        TextView tv = new TextView(this);
        tv.setText("Hello, World");
        setContentView(tv);
    }
}

Run the app

Now the emulator should start and boot android with your first App! Be patient - first boot takes a while and maybe you have to press "Menu" to unlock the screen!

play around

Emulator Screenshot

To see a bit more of android, we just create another project to demonstrate the basics ...

goto eclipse:

Now open DialogTest.java from your left tab (->DialogTest ->src -> de.slash4.dialogtest -> DialogTest.java) and paste the following into the file:

package de.slash4.dialogtest;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
 
public class DialogTest extends Activity {
 
		private Button b_test1;
		private Button b_test2;
 
		private Builder dialog1;
		private Builder dialog2;
 
		private TextView tv_outputfield;
 
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.dialogtest_start);
                doSomeSetup();
 
                tv_outputfield = (TextView) findViewById(R.id.outputfield);
 
                b_test1 = (Button) findViewById(R.id.dialog1);
                b_test1.setOnClickListener(new OnClickListener(){
                	public void onClick(View v) {
                		dialog1.show();
                	}
                });
 
                b_test2 = (Button) findViewById(R.id.dialog2);
                b_test2.setOnClickListener(new OnClickListener(){
                	public void onClick(View v) {
                		dialog2.show();
                	}
                });
        }
 
        private void doSomeSetup() {
                dialog1 = new AlertDialog.Builder(DialogTest.this);
                dialog1.setTitle("IQ Test");
                dialog1.setIcon(R.drawable.icon);
                dialog1.setMessage("Are you stupid?");
                dialog1.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                                tv_outputfield.setText("You chose that you are stupid");
                        }
                });
                dialog1.setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        	tv_outputfield.setText("You chose that you are NOT stupid");
                        }
                });
 
                dialog2 = new AlertDialog.Builder(DialogTest.this);
                dialog2.setTitle("DialogTest 2");
                dialog2.setMessage("Do you want to recommend this tutorial?");
                dialog2.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        	tv_outputfield.setText("You wanted to recommend this tutorial");
                        }
                });
                dialog2.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        	tv_outputfield.setText("You are not sure ... hhmm");
                        }
                });
                dialog2.setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        	tv_outputfield.setText("You dont want to recommend this tutorial. Why not? Give me some feedback ...");
                        }
                });
        }	
 
 }

And you also need to copy and paste the following to a new xml file in res -> layout -> dialogtest_start.xml ...

<LinearLayout
 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
 
    <TextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="You can choose between the following options:" />
 
    <Button
       android:id="@+id/dialog1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="Launch IQ test" />
 
    <Button
       android:id="@+id/dialog2"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="Launch some other dialog!" />
 
    <TextView
       android:id="@+id/outputfield"	
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="" />
 
</LinearLayout>

... ok that's enough for today. See you tomorrow for Android classes - Day 2 - basic concepts overview

Screencast

Androidâ„¢ is a trademark of Google Inc. Use of this trademark is subject to Google Permissions