Every programming language or environment book starts off with the popular “Hello, World!” demonstration. This is just enough of a program to prove you can build things. However, the typical Hello, World! program has no interactivity (e.g., it just dumps the words to a console), and so it’s really boring.
This chapter demonstrates a simple project, but one using Advanced Push-Button Technology and the current time, making it a bit more interesting than the typical Hello, World! demo.
As described in the previous chapter, to work with anything in Android, you need a project. If you are using tools that are not Android-enabled, you can use the android create project
script, found in the tools/
directory in your SDK installation. You will need to pass to android create project
the API target, the directory where you want the skeleton generated, the name of the default activity, and the Java package where all of this should reside:
android create project --target 2
--path /path/to/my/project/dir --activity Now
--package com.commonsware.android.Now
You can also download the project directories of the samples shown in this book in a ZIP file on the Apress web site. These projects are ready for use; you do not need to run android create project
on those unpacked samples.
Your project' s src/
directory contains the standard Java-style tree of directories based on the Java package you used when you created the project (e.g., com.commonsware.android
results in src/com/commonsware/android/
). Inside the innermost directory, you should find a pregenerated source file named Now.java
, which is where your first activity will go. This activity will contain a single button that displays the time that the button was last pushed (or the time the application was started if the button has not been pushed).
NOTE: If you downloaded the source files from the Apress web site, you can just use the Skeleton/Now
project directly, rather than entering the code.
Open Now.java
in your editor and paste in the following code:
package com.commonsware.android.skeleton;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Date;
public class Now extends Activity implements View.OnClickListener {
Button btn;
@Override
public void
onCreate(Bundle icicle) {
super.
onCreate(icicle);
btn=new
Button(this);
btn.
setOnClickListener(this);
updateTime();
setContentView(btn);
}
public void
onClick(View view) {
updateTime();
}
private void
updateTime() {
btn.
setText(new
Date().
toString());
}
}
Let' s examine this piece by piece.
The package declaration needs to be the same as the one you used when creating the project. And, as with any other Java project, you need to import any classes you reference. Most of the Android-specific classes are in the android
package.
package com.commonsware.android.skeleton;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Date;
It' s worth noting that not every Java SE class is available to Android programs. Visit the Android class reference to see what is and is not available.
public class Now extends Activity implements View.OnClickListener {
Button btn;
Activities are public classes, inheriting from the android.app.Activity
base class. In this case, the activity holds a button (btn
).
NOTE: A button, as you can see from the package name, is an Android widget, and widgets are the user interface elements that you use in your application.
Since, for simplicity, we want to trap all button clicks just within the activity itself, we also have the activity class implement OnClickListener
:
@Override
public void
onCreate(Bundle icicle) {
super.
onCreate(icicle);
btn=new
Button(this);
btn.
setOnClickListener(this);
updateTime();
setContentView(btn);
}
The onCreate()
method is invoked when the activity is started. The first thing you should do is chain upward to the superclass, so the stock Android activity initialization can be done.
In our implementation, we then create the button instance (new Button(this)
), tell it to send all button clicks to the activity instance itself (via setOnClickListener()
), call a private updateTime()
method (shown shortly), and then set the activity' s content view to be the button itself (via setContentView()
).
NOTE: All widgets extend the View
base class. You usually build the user interface out of a hierarchy of views, but in this example, we are using a single view.
We will discuss that magical Bundle icicle
in Chapter 16. For the moment, consider it an opaque handle that all activities receive upon creation.
public void
onClick(View view) {
updateTime();
}
In Swing, a JButton
click raises an ActionEvent
, which is passed to the ActionListener
configured for the button. In Android, a button click causes onClick()
to be invoked in the OnClickListener
instance configured for the button. The listener is provided to the view that triggered the click (in this case, the button). All we do here is call that private updateTime()
method:
When we open the activity (onCreate()
), or when the button is clicked (onClick()
), we update the button' s label to be the current time via setText()
, which functions much the same as the JButton
equivalent.
To build the activity, use your integrated development environment' s (IDE' s) built-in Android packaging tool or run ant
in the base directory of your project. Then do the following to run the activity:
android
command, choosing an AVD in the AVD Manager, and clicking the Start button. You should be able to accept the defaults in the Launch Options dialog. Figure 3–1 shows the Android home screen.
NOTE: The first time you use an AVD with the emulator, it will take substantially longer to start than it will subsequent times.
Figure 3–1. The Android home screen
ant install
).Figure 3–2. The Android application launcher
Figure 3–3. The Now demonstration activity
Clicking the button—in other words, clicking pretty much anywhere on the phone's screen—will update the time shown in the button's label.
Note that the label is centered horizontally and vertically, as those are the default styles applied to button captions. You can control that formatting, as described in Chapter 5.
After you are finished gazing at the awesomeness of Advanced Push-Button Technology, you can click the back button on the emulator to return to the launcher.