Part II

Activities

Chapter 7

Rewriting Your First Project

The project you created in Chapter 3 is composed of just the default files generated by the Android build tools—you did not write any Java code yourself. In this chapter, you will modify that project to make it somewhat more interactive. Along the way, you will examine the basic Java code that comprises an Android activity.

NOTE: The instructions in this chapter assume you followed the original instructions in Chapter 3 in terms of the names of packages and files. If you used different names, you will need to adjust the names in the following steps to match yours.

The Activity

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.

Open Now.java in your editor and paste in the following code (or, if you downloaded the source files from the Apress web site, you can just use the Skeleton/Now project directly):

packagecom.commonsware.android.skeleton;

importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importjava.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());
  }
}

Dissecting the Activity

Let's examine this Java code piece by piece, starting with the package declaration and imported classes:

packagecom.commonsware.android.skeleton;

importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importjava.util.Date;

The package declaration needs to be the same as the one you used when creating the project. Then, 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.

NOTE: Not every Java SE class is available to Android programs. Visit the Android class reference to see what is and is not available.

Activities are public classes, inheriting from the android.app.Activity base class. In this case, the activity holds a button (btn):

public class Now extends Activity implements View.OnClickListener {
  Button btn;

Since, for simplicity, we want to trap all button clicks just within the activity itself, we also have the activity class implement OnClickListener.

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:

@Override
public void onCreate(Bundle icicle) {
  super.onCreate(icicle);

  btn=new Button(this);
  btn.setOnClickListener(this);
  updateTime();
  setContentView(btn);
}

In our implementation, we then create the button instance btn (via new Button(this)), tell it to send all button clicks to the activity instance itself (via setOnClickListener()), call a private updateTime() method, and then set the activity's content view to be the button itself (via setContentView()). We will take a look at that magical Bundle icicle in a later chapter. For the moment, consider it an opaque handle that all activities receive upon creation.

public void onClick(View view) {
  updateTime();
}

In Java's traditional Swing UI world, 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 theOnClickListener instance configured for the button. The listener has passed to it the view that triggered the click (in this case, the button). All we do here is call that private updateTime() method:

private void updateTime() {
  btn.setText(new Date().toString());
}

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.

Building and Running the Activity

To build the activity, use your IDE's built-in Android packaging tool, or run ant clean install in the base directory of your project (as described in Chapter 3). Then, run the activity. It should be launched for you automatically if you are using Eclipse; otherwise, find the activity in the home screen launcher. You should see an activity akin to what's shown in Figure 7–1.

images

Figure 7–1. The Now demonstration activity

Clicking the button—in other words, clicking pretty much anywhere on the device'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. We can control that formatting, which will be covered in a later chapter.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset