Implementing an About

When the user selects the About button, meaning that either they touch it (if they have a touch screen) or they navigate to it with the D-pad (directional pad) or trackball and press the selection button, we want to pop up a window with some information about Sudoku.

After scrolling through the text, the user can press the Back button to dismiss the window.

We can accomplish this in several ways:

• Define a new Activity, and start it.

• Use the AlertDialog class, and show it.

• Subclass Android's Dialog class, and show that.

For this example, let's define a new activity. Like the main Sudoku activity, the About activity will need a layout file. We will name it res/layout/ about.xml:

Download Sudokuvl/res/layout/about.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="" android:layout_width= "fill_parent" android:layout_height="fill_parent" android:padding="10dip"> <TextView android:id="@+id/about_content" android:layout_width= "'wrap_content" android:layout_height= "'wrap_content" android:text="@string/about_text" /> </ScrollView>

We need only one version of this layout because it will look fine in both portrait and landscape modes.

Now add strings for the title of the About dialog box and the text it contains to res/values/strings.xml:

Download Sudokuv1/res/values/strings.xml

<string name="about_title">About Android Sudoku</string> <string name="about_text">\

Sudoku is a logic-based number placement puzzle. Starting with a partially completed 9x9 grid, the objective is to fill the grid so that each row, each column, and each of the 3x3 boxes (also called <i>blocks</i>) contains the digits 1 to 9 exactly once. </string>

Note how a string resource can contain simple HTML formatting and can span multiple lines. In case you're wondering, the backslash character (\) in about_text prevents an extra blank from appearing before the first word.

The About activity should be defined in All it needs to do is override onCreate() and call setContentView(). To create a new class in Eclipse, use File > New > Class. Specify the following:

Source folder: Sudoku/src Package: org.example.sudoku Name: About

Edit the class so it looks like this:

Download Sudokuv1/src/org/example/sudoku/

package org.example.sudoku;

import; import android.os.Bundle;

public class About extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.about);

Next we need to wire all this up to the About button in the Sudoku class. Start by adding a few imports that we'll need to

Download Sudokuv1/src/org/example/sudoku/

import android.content.Intent;

import android.view.View;

import android.view.View.OnClickListener;

In the onCreate() method, add code to call findViewById() to look up an Android view given its resource ID, and add code to call setOnClickLis-tener( ) to tell Android which object to tickle when the user touches or clicks the view:

Download Sudokuv1/src/org/example/sudoku/

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

// Set up click listeners for all the buttons

View continueButton = findViewById(;


View newButton = findViewById(;


View aboutButton = findViewById(;


View exitButton = findViewById(;


While we're in here, we do the same for all the buttons. Recall that constants like are created by the Eclipse plug-in in when it sees @+id/about_button in res/layout/main.xml.

The setOnClickListener() method needs to be passed an object that implements the OnClickListener Java interface. We're passing it the this variable, so we had better make the current class (Sudoku) implement that interface, or we'll get a compiler error. OnClickListener has one method in it called onClick(), so we have to add that method to our class as well:3

Download Sudokuv1/src/org/example/sudoku/

public class Sudoku extends Activity implements OnClickListener { // ...

public void onClick(View v) { switch (v.getId()) { case

Intent i = new Intent(this, About.class);



To start an activity in Android, we first need to create an instance of the Intent class. There are two kinds of intents: public (named) intents that are registered with the system and can be called from any application and private (anonymous) intents that are used within a single application. For this example, we just need the latter kind. If you run the program and select the About button now, you will get an error (see Figure 3.7, on the following page). What happened?

We forgot one important step: every activity needs to be declared in AndroidManifest.xml. To do that, double-click the file to open it, switch to XML mode if necessary by selecting the AndroidManifest.xml tab at the bottom, and add a new <activity> tag after the closing tag of the first one:

<activity android:name=".About"

android:label = "@string/about_title"> </activity>

Now if you save the manifest, run the program again, and select the About button, you should see something like Figure 3.8, on page 62. Press the Back button (Esc on the emulator) when you're done.

3. If you're a Java expert, you may be wondering why we didn't use an anonymous inner class to handle the clicks. You could, but according to the Android developers, every new inner class takes up an extra 1KB of memory.

Figure 3.7: Mountain View, we have a problem

That looks OK, but wouldn't it be nice if we could see the initial screen behind the About text?

0 0

Post a comment