Directory Structure of Project and Autogenerated Content

Figure 2.1 shows an example project structure, as seen from the Eclipse Package Explorer.

Figure 2.1 Android project directory structure, as seen in the Eclipse IDE.

With the exception of the Android 2.0 library, the project structure is a mix of usergenerated and auto-generated files.

User-generated files include

■ src/ contains the Java packages the developer writes or imports for the application. Each package can have multiple .java files representing different classes.

■ res/layout/ contains the XML files that specify the layout of each screen.

■ res/values/ contains the XML files used as references by other files.

■ res/drawable-hdpi/, res/drawable-mdpi/, and res/drawable-ldpi/ are directories that contain pictures the application uses. They have high, medium, and low dots-per-inch resolution, respectively.

■ assets/ contains additional nonmedia files the application uses.

AndroidManifest.xml specifies the project to the Android OS. Autogenerated files include

■ gen/ contains autogenerated code, including the generated class

■ contains project settings.Although autogenerated, it should be kept under revision control.

An application's resources include XML files describing the layout, XML files describing values such as strings, labels of UI elements, and additional supporting files such as pictures and sounds. At compile time, references to the resources are gathered into an autogenerated wrapper class called The Android Asset Packaging Tool (aapt) autogenerates this file. Listing 2.4 shows what it looks like for the "Creating a Project and an Activity" recipe.

Listing 2.4 gen/com/cookbook/simple_activity/


* This class was automatically generated by the

* aapt tool from the resource data it found. It

package com.cookbook.simple_activity;

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;

Here, each resource is mapped to a unique integer value. In this way, the class provides a way to reference external resources within Java code. For example, to reference the main.xml layout file in Java, the R.layout.main integer is used. To reference the same within XML files, the "@layout/main" string is used.

Referencing resources from within Java or XML files is demonstrated in Table 2.2. Note that to define a new button ID called home_button, the plus sign is added to the identifying string: @+id/home_button. More complete details on resources are given in Chapter 4, "User Interface Layout," but this suffices to cover the recipes in this chapter.

Table 2.2 How Different Resources Are Referenced from Within Java and XML Files


Reference in Java

Reference in XML







@+id/home button button


<string name="hello">



Was this article helpful?

0 0
Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment