8 Starting An Android Application Project
8 Starting An Android Application Project
8 Starting An Android Application Project
On the Eclipse desktop, select File, New, Project. You are presented with a new dialog
box. Select Android Project under the Android heading, and then select Next. You
will see the dialog box in Figure 2.1.
Figure 2.1
Eclipse New Android Project panel.
Choose a project name, such as Project_1. Notice the check box labeled Use
Default Location. It is checked and contains a path with workspace as the final
directory in the path. This is what I referred to earlier; you can leave it as is. Next,
the Build Target area refers to the version of the Android operating system (OS) you
want your application to be used on. The lower the application programming interface (API) you choose, the more inclusive you will be of devices that will run your
application. On the other hand, the lower the number you choose, the fewer features
of devices and advances in the Android platform your application will be able to take
advantage of. Table 2.1 shows the complete list of versions and corresponding APIs
at the time of this writing. For basic applications such as our first application here,
you can choose platform 1.5 at the top of the list.
API Level
VERSION_CODE
Android 3.2
13
HONEYCOMB_MR2
Android 3.1.x
12
HONEYCOMB_MR1
Android 3.0.x
11
HONEYCOMB
Android 2.3.4
Android 2.3.3
10
GINGERBREAD_MR1
Android 2.3.2
Android 2.3.1
Android 2.3
GINGERBREAD
Android 2.2.x
FROYO
Android 2.1.x
ECLAIR_MR1
Android 2.0.1
ECLAIR_0_1
Android 2.0
ECLAIR
Android 1.6
DONUT
Android 1.5
CUPCAKE
Android 1.1
BASE_1_
Android 1.0
BASE
Source: http://developer.android.com/guide/appendix/api-levels.html
Figure 2.2 shows the bottom half of the dialog box, which may not show immediately
because of your monitors resolution settings.
Next, you need to choose an application name. Android device owners who download your application will see this name and identify the application on their device
by this name, so make it plain English, such as Tip Calculator.
10
Figure 2.2
Bottom portion of Eclipse New Android Project panel.
The Package Name is a little more complicated and refers to the Java structure
referred to as a package. Simply put, a package is a related set of classes. Your project
could contain many classes, related to each other by a single purpose: to provide the
functionality of your Android app. All the package names in the Android system
must be unique. To be sure whatever you are adding will not coincidentally match
anything else, a domain-style naming system is used. The Java convention for naming packages is sort of a web domain name backward, such as
com.sheusi.TipCalculator. You should note that reverse-domain naming is
the convention here. You need not own a domain name; you can just make one up.
(You can learn more about package naming at http://download.oracle.com/javase/
tutorial/java/package/namingpkgs.html.)
The activity name is actually the class name for the primary Java class of your project, so you must follow the Java class naming conventions; for instance, dont include
spaces in the name. Otherwise, it could be any name you want, but it should be consistent with the rest of the project, such as TipCalc. Just as all Java programs are
subclasses of the Java class called Object, all Android applications are subclasses of
the Activity class, or subclasses of some subclass of the Activity class. An example we will see later is the MapActivity class, which is a subclass of the Activity
class. The full name for your Activity class will be the package name plus the
activity name, such as com.sheusi.TipCalculator.TipCalc. An old but useful
guide to naming Java files as well as other Java programming best practices is the
document Java Code Conventions, found and downloaded at www.oracle.com/
technetwork/java/codeconventions-150003.pdf.
You need to enter an integer to indicate the minimum SDK version for your project.
Entry of this value determines which versions of the Android OSs on devices that
ultimately will want to download your application will be compatible, and which features
your application will be able to use. You can discover more details on the following website: http://developer.android.com/guide/developing/projects/projects-eclipse.html.
If you are unsure, simply use the number listed under the API column corresponding to
the Target Name you chose in the Build Target window. The lowest usable value is 1.
After you click the Finish button, Eclipse creates an empty project framework for you.
Eclipse is a powerful, versatile development environment that can be complicated and
intimidating. It is easy to click the wrong place with the mouse and change the appearance of the whole editor. If nothing else, it takes practice. Figure 2.3 is an illustration of
the entire Eclipse screen with the essential areas labeled. If any of these accidently disappear on you, you can probably restore them by choosing Window on the menu bar,
choosing Show View, and finally choosing the panel you want to restore. If you accidentally minimize the editor screen, look for an icon on the right side of the screen
displaying a pencil on a piece of paper. Click this to restore the editor.
Figure 2.3
Eclipse IDE screen.
The right side of the screen is the editor space. If you are familiar with Eclipse, you
will recognize this. On the left side is the Package Explorer window. You will find a
list for all the projects in your workspace as described earlier. If you click on your
newly created project, you see a breakdown of the components of the project Eclipse
created. Its a combination of directories, subdirectories, and files. As mentioned
11
12
Figure 2.4
Eclipse Package Explorer.
The first listing is a subdirectory called src. This subdirectory or subfolder contains
all the source code for the class(es) you intend to create for your application. You
should recognize the package name that you used in the dialog box in the first level
below the src icon, and the activity name you chose with the .java extension added.
This is the primary class of the application.
Next is the directory named gen. The primary file here is called R.java, and it is created based on your configuration of the main.xml and strings.xml at a minimum.
You can read this file; it looks like any other Java class, but as Eclipse warns, you do
not modify it. It will take care of itself as you modify other components of the project.
The next component is a collection of API files based on the version you chose in the
dialog box in the beginning. It will be numbered based on your choice2.2 in this
caseand the number can be different for different projects. You will likely not need
to deal with this section of the project.
Following that is a directory called res, which is short for resources. It contains
folders whose names begin with drawable; these contain graphics files such as the
launch icons for the application. If you intend to use a launch icon other than the
standard Android robot icon, you need to put a graphic of a specified size and
type in each of these folders. You will find more on that topic later in the book.
The layout and values subdirectories under res contain XML files. The layout
directory contains XML files that configure the screens of the application. The
values folder contains values for text strings and a couple of other data types you
might use in your application. Assignments can be made here in the XML file, and
they will be available throughout the coded application. These XML files together
allow you to design the whole user interface without having to write a single line of
Java code.
Finally, we come to the AndroidManifest.xml file. This file can be viewed as the
instruction book the target device uses to run the application. It contains things like
permissions to use features on the device such as the GPS system, references to the
files that should be included when the application is bundled up for deployment
(hence the inclusion of the word manifest in the filename), version and revision numbers, API information, and so on. This file is important to the application, and you
should take care to construct it correctly. Following is a basic manifest file. It belongs
to one of the tutorials found in Chapter 9, Text Files, Data Tables, and XML.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sheusi.CheckBook"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CheckBook"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
13