• No results found

43Building an Android application in Eclipse

Development environment

43Building an Android application in Eclipse

the development tools. Building an Android application is not too much different from creating other types of Java applications in the Eclipse IDE. It all starts with choosing File > New and selecting an Android application as the build target.

Like many development environments, Eclipse provides for a wizard interface to ease the task of creating a new application. We’ll use the Android Project Wizard to get off to a quick start in building an Android application.

2.3.1 Android Project Wizard

The most straightforward manner to create an Android application is to utilize the ser- vices of the Android Project Wizard, which is part of the ADT plug-in. The wizard pro- vides a simple means to define the Eclipse project name and location, the Activity

name corresponding to the main UI class, as well as a name for the application. Of importance also is the Java package name under which the application is created. Once this application is created, it is easy to add new classes to the project.

NOTE In this example, we are creating a brand-new project in the Eclipse work- space. This same wizard may be used to import source code from another developer, such as the sample code for this book. Note also that the spe- cific screens may vary over time as the Android tools mature.

Figure 2.10 demonstrates the creation of a new project named Chapter2 using the wizard.

TIP You will want the package name of your applications to be unique from one application to the next. Clicking Finish creates our sample appli- cation. At this point, the application compiles and is capable of running on the emulator—no further development steps are required. Of course, what fun would an empty project be? Let’s flesh out this sample application, our Android Tip Calculator.

2.3.2 Android sample application code

The Android Application Wizard takes

care of a number of important elements in the Android application structure, includ- ing the Java source files, the default resource files, and the AndroidManifest.xml file. Looking at the Package Explorer view in Eclipse we can see all of the elements of this application. Here’s a quick description of the elements included in our sam- ple application:

Figure 2.10 Using the Android Project Wizard, it is easy to create an empty Android application, ready for customization.

The src folder contains two Java source files automatically created by the wizard.ChapterTwo.java contains the main Activity for the application. We will mod-

ify this file to add our sample application’s tip calculator functionality.

■ R.java contains identifiers for each of the UI resource elements in the applica- tion. It is important that you never modify this file directly, as it automatically regenerates every time a resource is modified, and any manual changes you make will be lost the next time the application is built.

Android.jar contains the Android runtime Java classes. This is a reference to

the android.jar file found in the Android SDK.

■ The res folder contains all of the Android resource files, including:

■ Drawables contains image files such as bitmaps and icons. The wizard includes a default Android icon named icon.png.

Layout contains an xml file called main.xml. This file contains the UI elements

for the primary view of our Activity. We will modify this file but we will not be making any significant or special changes—just enough to accomplish our mea- ger UI goals for our Tip Calculator. UI elements such as Views are covered in detail in chapter 3. It is not uncommon for an Android application to have mul- tiple xml files in the Layout section.

■ Values contains the strings.xml file. This file is used for localizing string values such as the application name and other strings used by your application. It con- tains all of the applications in this book

■ AndroidManifest.xml represents the deployment information for this project. While AndroidManifest.xml files can become somewhat complex, this chapter’s manifest file can run without modification because no special permissions are required.

Now that we know what is in the project, let’s review how we are going to modify the application. Our goal with the Android Tip Calculator is to permit our user to enter the price of a meal, then select a button to calculate the total cost of the meal, tip included. To accomplish this, we need to modify two files, ChapterTwo.java and the UI layout file, main.xml. Let’s start with the UI changes by adding a few new elements to the primary View, as shown in listing 2.1.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"

android:text="Chapter 2 Android Tip Calculator"

Listing 2.1 Main.xml contains UI elements

Static TextView

45