Android sample application code

The Android Application Wizard takes care of a number of important elements in the Android application structure, including 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 sample 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 modify this file to add our sample application's tip calculator functionality.

■ R.java contains identifiers for each of the UI resource elements in the application. 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 meager 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 multiple 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 contains 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.

0 0

Post a comment