Android: XML layouts

So, I’ve been fiddling around w/ ADT for a couple of hours now. It’s possible to programmatically create layouts (i.e. an Android UI), but it’s not a great solution. It’s way easier to define the layout in XML, and then bind whatever event handlers are required. Here we have a simple class:

package com.waynekhan;

import android.os.Bundle;
import android.view.*;
import android.widget.*;
import java.util.*;

public class HWA extends Activity implements View.OnClickListener {
protected void onCreate(Bundle savedInstanceState) {

Button b = (Button) findViewById(;

public void onClick(View v) {
switch (v.getId()) {
case {
Button b = (Button) v;
b.setText(new Date().toString()); break;

As you can see, it’s quite bare. Load an XML layout (R.layout.main), define a button ( and then bind a click listener to said button. Then we have onClick() which simply sets the button text to the current String-ified date. Our code is really clean, and we do all the UI-related stuff in /res/layout/main.xml, and put all our conveniently l10n strings in /res/values/strings.xml. Unfortunately, I can’t post the content of the XML files, since WordPress groks ’em and all I get is an empty code block.


Android: Developing w/ the SDK ADT bundle

This semester, I’m taking a course on Android development. So I’ll be posting a bit more Android-related stuff as we go along. I’m developing on the MacBook Air, so get comfy w/ the command line.

To get started, grab a copy of the Android SDK. The Android Development Toolkit (ADT) bundle is pretty awesome. It comes w/ Eclipse and the plugin all setup already:

I unzipped the .zip file in Downloads; i.e. /Users/kzhiwei/Downloads, so the full path would be something like:


Now you need to connect up your Android device (I use the Nexus S). The truth is, the ADT plugin comes w/ the Android Virtual Device (AVD) Manager. So you could, theoretically emulate any Android device, but the emulated device takes too long a time to boot up, it’s insane not to use a real hardware device. You’ll need to setup Android debugging: Settings > Developer options > On > Android debugging. There are two other useful options here: Stay awake and USB debugging notify. I keep them on, but it’s not mandatory.

Now, we’ll need to find out what is our device serial number. Fire up Terminal, and navigate to /Users/kzhiwei/Downloads/adt-bundle-mac-x86_64/adt/sdk/platform-tools:

woteba:platform-tools kzhiwei$ pwd
woteba:platform-tools kzhiwei$ ./adb devices
List of devices attached
3xxxxxxxxxxxxxxx device

I’ve masked out my device ID, of course. Just remember the starting digit, and then you are basically ready to start working w/ Eclipse. I do this by using the Finder, and then navigating to adt-bundle-mac-x86_64/eclipse. There’ll be an somewhere, so double-click, and then wait a bit. Now we’re ready.

File > New > Android Application Project. Under Application Name and Project Name, input “Foo”. Under package name, input “”. So it’s basically my domain name in reverse, plus the app name (foo). Next we’ll need to define SDK levels. The defaults are good enough:

Minimum: Froyo
Target: Jelly Bean
Compile with: Jelly Bean
Theme: Holo Light w/ Dark Action Bar

Stay with me! Click Next (just use the defaults for the next few pages). You should see your newly created project (Foo) in the Project Explorer. Right-click the project, and then Run As > Android Application. The first time you do that, there’ll be an option to choose either an AVD, or an Android hardware device. We’ll check our device, of course, and then check “Use same device for future launches”, and then go. There’ll be some activity on the device, and then your new application should run. It just says “Hello world!”. Awesome, we are done here.

Hopefully I’m really hardworking and I’ll continue posting up stuff w/ real code in a bit.

#android, #eclipse, #mac