Creating the Opening Screen

We'll start with a skeleton Android program created by the Eclipse plugin. Just as you did in Section 1.2, Creating Your First Program, on page 23, create a new "Hello, Android" project, but this time use the following values:

Project name: Sudoku Build Target: Android 2.2 Application name: Sudoku Package name: org.example.sudoku Create Activity: Sudoku Min SDK Version: 8

In a real program, of course, you would use your own names here. The package name is particularly important. Each application in the system must have a unique package name. Once you choose a package name, it's a little tricky to change it because it's used in so many places.

I like to keep the Android emulator window up all the time and run the program after every change, since it takes only a few seconds. If you do that and run the program now, you'll see a blank screen that just contains the words "Hello World, Sudoku." The first order of business is to change that into an opening screen for the game, with buttons to let the player start a new game, continue a previous one, get information about the game, and exit. So, what do we have to change to do that?

As discussed in Chapter 2, Key Concepts, on page 30, Android applications are a loose collection of activities, each of which define a user interface screen. When you create the Sudoku project, the Android plug-in makes a single activity for you in

Download SudokuvO/src/org/example/sudoku/

package org.example.sudoku;

import; import android.os.Bundle;

public class Sudoku extends Activity {

/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

Android calls the onCreate() method of your activity to initialize it. The call to setContentView( ) fills in the contents of the activity's screen with an Android view widget.

We could have used several lines of Java code, and possibly another class or two, to define the user interface procedurally. But instead, the plug-in chose the declarative route, and we'll continue along those lines. In the previous code, R.layout.main is a resource identifier that refers to the main.xml file in the res/layout directory (see Figure 3.2, on the following page). main.xml declares the user interface in XML, so that's the file we need to modify. At runtime, Android parses and instantiates (inflates) the resource defined there and sets it as the view for the current activity.

Package Explorer a ;S Sudoku a L^ src a 0 org.example.sudoku

¡JJ Eft Android 2.2 a gen [Generated Java Files] a ® crg.example.Eudoku [JJ assets a j^r res a §K drawable-hdpi

Qt icon.png a ¡23- drawable-ldpi icon.png a drawable-mdpi icon.png a layout

X main.xrml a values

H stri ngs.xrnl AndroidManifest.xrml

default, properties

Figure 3.2: Initial resources in the Sudoku project

It's important to note that the R class is managed automatically by the Android Eclipse plug-in. When you put a file anywhere in the res directory, the plug-in notices the change and adds resource IDs in in the gen directory for you. If you remove or change a resource file, is kept in sync. If you bring up the file in the editor, it will look something like this:

Download Sudokuv0/gen/org/example/sudoku/


* This class was automatically generated by the

* aapt tool from the resource data it found. It

package org.example.sudoku;

public final class R {

public static final class attr { }

public static final class drawable {

public static final int icon=0x7f020000;

public static final class layout {

public static final int main=0x7f030000;

public static final class string {

public static final int app_name=0x7f040001; public static final int hello=0x7f040000;

The hex numbers are just integers that the Android resource manager uses to load the real data, the strings, and the other assets that are compiled into your package. You don't need to worry about their values. Just keep in mind that they are handles that refer to the data, not the objects that contain the data. Those objects won't be inflated until they are needed. Note that almost every Android program, including the base Android framework itself, has an R class. See the online documentation on android.R for all the built-in resources you can use.1

So, now we know we have to modify main.xml. Let's dissect the original definition to see what we have to change. Double-click main.xml in Eclipse to open it. Depending on how you have Eclipse set up, you may see either a visual layout editor or an XML editor. In current versions of ADT, the visual layout editor isn't that useful, so click main.xml or the Source tab at the bottom to see the XML. The first line of main.xml is as follows:

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

All Android XML files start with this line. It just tells the compiler that the file is XML format, in UTF-8 encoding. UTF-8 is almost exactly like regular ASCII text, except it has escape codes for non-ASCII characters such as Japanese glyphs.


0 0

Post a comment