Download

I started with the quintessential hello-world example. The text I used, Professional Android 2 Application Development by Reto Meier, pretty much uses the Google hello world and the SDK’s empty project default.http://senkaimon.ath.cx/wp-admin/post-new.php?preview=true

The entire source code is below:

package com.vromtr.paad.ch2.helloworld;
 
import android.app.Activity;
import android.os.Bundle;
 
public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

First, the package statement:

package com.vromtr.paad.ch2.helloworld;

Programs, and parts of programs, compile into packages. They’re like namespaces in C++ with the exception that a compiled package can potentially be linked to in strange ways by other programs, which is why pains must be taken to keep them unique.

Next, the import statements:

import android.app.Activity;
import android.os.Bundle;

Import statements are like C’s “#include” directives. They tell the compiler that you plan on making use of a specific package in your program. “Activity” in Android parlance can be analogous to either a “window” or an “application” in Windows land; it is a self-contained user interface element with associated code.

“Bundles” contain information for saving application state – your program can be killed at any time, so a program with stateful data needs to store and retrieve it using this structure.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

This is Java-ese for void main(). We are creating a new HelloWorld class derived from the base Activity (window) class that comes with the SDK. Because the base class does nothing interesting, we want to override its onCreate() function to do something useful:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

@Override specifies that we’re replacing a function in the base Activity class – this is exactly like a virtual function in C++. The onCreate function is passed a Bundle; if our program had any stateful data, we would keep it within this Bundle so it looks like our application kept running after the user closed it, turned off the phone, or switched focus to another application.

We start the function by calling the base class’s implementation of onCreate. I’m sure this takes care of a lot of housekeeping for us.

The final line of the function is the most interesting:

        setContentView(R.layout.main);

setContentView sets the active View – analogous to a control or a sub-window. This line actually instantiates and displays our user interface. “R” is a class generated by the ADT toolkit; it’s how we reference Resources (get it?) like user interfaces (“layouts”), strings, graphics, and the like.

The source code for the layout looks like this:

<?xml version="1.0" encoding="utf-8"?>
<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:text="I am placing text in this box!  And even more text!"
	 android:id="@+id/TextView01"
	 android:layout_width="wrap_content"
	 android:layout_height="wrap_content">
	 </TextView>
</LinearLayout>

The LinearLayout specifies how sub-views “flow” within your user interface. You don’t get to specify per-pixel locations and sizes out of deference to the plethora of screen sizes and orientations available on Android phones. Instead, you get to nest Layouts within Layouts within Layouts to try to get things to line up as you think they would, and hope that particular (valid) combination of Layouts doesn’t crash the IDE.

The TextView is exactly as it sounds – a “label” or a “textbox” control.

The XML layout is compiled into our applications resource file, which like Windows resource scripts are baked into our compiled application binary. As shown above, we access the main layout via R.layouts.main.

Lengthy exposition for a Hello World! program. Google’s version covers some more points on IDE configuration, and has pictures.