Example An Intent to Pick How We Say Hello World

Almost everyone writes "Hello World" programs. So there is a nearly universal need to augment these programs and prevent them from getting dull by providing a choice of greetings. That is what Example 13-1 does.

Example 13-1. An Intent that chooses alternate "Hello World" strings package example.sayhello;

import example.sayhello.R;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

* An activity returning a result

public class SayHello extends Activity {

protected void onCreate(Bundle savedInstanceState)

// Call the parent class super.onCreate(savedInstanceState);

// Put up the view for acquiring some input from the user setContentView(R.layout.main);

// Set up the listeners for the buttons

((Button)findViewById(R.id.hello)).setOnClickListener(helloListener); ((Button)findViewById(R.id.goaway)).setOnClickListener(goAwayListener);

private OnClickListener helloListener = new OnClickListener() {

public void onClick(View v) {

returnResult("Hello, other Android!");

private OnClickListener goAwayListener = new OnClickListener() {

public void onClick(View v) {

returnResult("Get off my lawn, damn kids!");

// Put a result in an Intent object and set the result for this activity void returnResult(String greeting) {

// Create the Intent object Intent i = new Intent();

// Put an extra named "result" in the intent i.putextra("result", greeting);

// Make this Intent the result for this activity setResult(RESULT_OK, i);

// End this activity finish();

Example 13-2 shows the layout file that specifies the user interface provided by this activity.

Example 13-2. Resource for alternate "Hello World" strings <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:gravity="center_horizontal"

android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"

android:layout_weight="0"

android:paddingBottom="8dip"

android:text="Say hello, or not"/>

<Button android:id="@+id/hello"

android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello"> <requestFocus /> </Button>

<Button android:id="@+id/goaway"

android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Go away"> </Button>

</LinearLayout>

</LinearLayout>

Figure 13-1. Output of simple "Hello World" program

This layout describes a screen with two buttons. The listeners for these buttons are called HelloListener and GoAwayListener. In the Java code in Example 13-1, the listener methods call returnResult, passing the string that will be returned.

You can try this program as a standalone application. Create a new Android project with the package named example.sayhello and an activity named SayHello. Use Example 13-1 for the SayHello class and Example 13-2 for the main.xml layout file. When run, the application will display Figure 13-1.

When you click on or press one of the buttons, the program finishes and disappears from the screen. It also creates an Intent object used as a "result" for the activity.

Let's take a closer look at how it does that. You may want to run the program under the debugger and set a breakpoint on the first line of the returnResult method, where we create an Intent object, and follow along using the "step over" command in the debugger.

First, an Intent object is created. This is what gets moved from this process to the process that started this Activity:

// Create the Intent object Intent i = new Intent();

Here we will see how Intent objects facilitate inter-process communications: you can label and associate several types of data with an Intent object and send these "stowaways" with the object from one process to another. Here we call putExtra to add data to the Intent. Its first argument is a String that labels the data; here we use "result" as the label. The second argument, the actual payload, can be any data type supported by the different putExtra methods (which differ in the arguments they take); in our simple example, we use a String for the payload as well:

// Put an extra named "result" in the intent i.putExtra("result", greeting);

The returnResult method "returns" the result, not to the method that calls this method, but through an Intent object to the code that started this instance of SayHello. The following line sets the result:

// Make this Intent the result for this activity setResult(RESULT_OK, i);

In this example, however, nothing happens to our result. Nobody expects it, and nobody uses it. Next we will change that, and see how one application can use a result produced by another.

0 0

Post a comment