Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: Android_ A Programmer's Guide
Description: Android_ A Programmer's Guide

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


Android



A Programmer’s Guide

This page intentionally left blank

Android



A Programmer’s Guide

J
...
DiMarzio

New York Chicago San Francisco
Lisbon London Madrid Mexico City
Milan New Delhi San Juan
Seoul Singapore Sydney Toronto

Copyright © 2008 by The McGraw-Hill Companies
...
Manufactured in the United States of America
...

0-07-159989-4
The material in this eBook also appears in the print version of this title: 0-07-159988-6
...
Rather than put a trademark symbol after every occurrence of a trademarked name,
we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the
trademark
...

McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training
programs
...
com or (212) 904-4069
...
(“McGraw-Hill”) and its licensors reserve all rights in and to the work
...
Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the
work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute,
disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent
...
Your right to use the work may be terminated if you fail to
comply with these terms
...
” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE
ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY
INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
...
Neither McGraw-Hill nor its
licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any
damages resulting therefrom
...
Under no
circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages
that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages
...

DOI: 10
...


This book is dedicated to Suzannah, Christian, and Sophia

About the Author
J
...
DiMarzio is a developer with over 15 years of
experience in networking and application development
and is the author of seven books on computing technologies
...
He lives in Central Florida
...
Polo is a software developer with over 20
years of experience working in the telecommunications,
financial, and, most recently, educational industries
...


Copyright © 2008 by The McGraw-Hill Companies
...


Contents at a Glance
1 What Is Android?
...


9

3 Downloading and Installing the Android SDK


...


35

5 Application: Hello World!
...


83

4 Exploring the Android SDK

7 Using Intents and the Phone Dialer
...
149
9 Using the Cell Phone’s GPS Functionality
...
239
11 Application: Find a Friend
...
307

Index
...

INTRODUCTION
...

Brief History of Embedded Device Programming
...

Introduction to Android
...

Why Eclipse?
...

Downloading and Installing Eclipse
...

Downloading the Android SDK
...

Configuring the Android Plugin for Eclipse
...

What Is in the Android SDK?
...

Android Samples
...

Android Tools
...

Application Life Cycle
...

Android Application Life Cycle
...

Creating Your First Android Project in Eclipse
...

AndroidManifest
...

Referenced Libraries
...

Hello World! Again
...

Hello World! Code-Based UI
...

Try This: Use TextView and ImageView
...
83
Creating a Shell Activity Using the Windows CLI
...
bat
...
88
Creating the Hello World! Activity in the Windows CLI
...
95
Adding the JAVA_HOME Variable
...
97
Hello World! on Linux
...
109
Try This: Create an Image-Based Hello World! in the CLI
...

What Are Intents?
...

Placing a Call from Your Activity
...

Editing Activity Permissions
...

Adding a Button
...

Try This: Modify the AndroidPhoneDialer Project
...

Building the Activities
...
xml File
...
java File
...
xml
...

Creating the Activity for AutoComplete
...

CheckBox
...

RadioGroup
...

Try This: Modify More View Attributes
...

Using the Android Location-Based API
...

What Is a track File?
...

Getting the nmea File in Linux
...

Creating the AndroidLBS Activity
...

Adding Zoom Controls
...


203

10 Using the Google API with GTalk
...

Implementing GTalk in Android
...
xml
...
java
...
OnClickListener
...

Try This: Add a Settings Feature to Your GoogleAPI Activity
...

Creating a SQLite Database
...

Editing the strings
...

Creating Your Content Provider
...

Editing AndroidManifest
...

Creating the NameEditor Activity
...

Creating the FriendsMap Activity
...

Running the FindAFriend Activity
...


257
259
263
263
265
276
276
278
283
293
299
302
305

12 Android SDK Tool Reference
...
308
Android Debug Bridge Commands
...
313

Acknowledgments
I

would like to thank everyone who participated in the creation of this book
...

I would also like to thank my family, Suzannah, Christian, and Sophia; Brett, Robert,
Roger, Zack, Mark, Kurt, Walter, Walter, Walter, Steve, Steve, Steve, and Gary—and all
my colleagues in Central Florida; and anyone else whom I may have forgotten
...
Click here for terms of use
...
This book has been designed to give
you the best first step toward the exciting new frontier of open source mobile
development
...

Through the course of this book, you will be introduced to the fundamentals of mobile
device application development using the Open Handset Alliance’s Android platform
...

The format of this book is such that it will take you through Android application
development in a logical manner
...
After discussing and installing the
development tools, Android SDK, and the Eclipse development environment
(Chapters 2, 3, and 4), the book dives directly into designing and creating Android
applications (Chapter 5)
...
A quick reference guide is also included in Chapter 12
...
Foremost among these skills
is a working knowledge of Java programming fundamentals
...
Click here for terms of use
...
6 kernel
...
Chapter 2 explains how to download and install the preferred
integrated development environment, Eclipse
...

Any comments, questions, or suggestions about any of the material in this book can
be forwarded directly to the author at jfdimarzio@jfdimarzio
...


Chapter

1

What Is Android?

Copyright © 2008 by The McGraw-Hill Companies
...


2

Android: A Programmer’s Guide

Key Skills & Concepts


History of embedded device programming



Explanation of Open Handset Alliance



First look at the Android home screen

I

t can be said that, for a while, traditional desktop application developers have been
spoiled
...
However, as traditional desktop application developers, we
have had the ability to create almost any kind of application we can imagine
...

One aspect that has made desktop programming more accessible is that we have
had the ability to interact with the desktop operating system, and thus interact with any
underlying hardware, pretty freely (or at least with minimal exceptions)
...


NOTE
I refer to two different kinds of developers in this discussion: traditional desktop
application developers, who work in almost any language and whose end product,
applications, are built to run on any “desktop” operating system; and Android
developers, Java developers who develop for the Android platform
...

Rather, the distinction is made for purposes of comparing the development styles
and tools of desktop operating system environments to the mobile operating
system environment, Android
...
Seen as a less “glamorous” sibling to
desktop—and later web—development, embedded device development typically got the

Chapter 1:

What Is Android?

proverbial short end of the stick as far as hardware and operating system features, because
embedded device manufacturers were notoriously stingy on feature support
...

Embedded devices differ from desktops in that an embedded device is typically a
“computer on a chip
...
When any
button is pressed, a chip interprets the signal in a way that has been programmed into the
device
...
This is a
simple form of embedded device programming
...

Most embedded devices ran (and in some cases still run) proprietary operating
systems
...
Simple devices did not need very
robust and optimized operating systems
...
Small-footprint operating systems such
as Linux, or even an embedded version of Microsoft Windows, have become more
prevalent on many embedded devices
...
This branching is evident
when you examine their architecture
...
The practice of manufacturers using
proprietary operating systems began more out of necessity than any other reason
...
As a result, there were no openly available, off-the-shelf software packages
or solutions that would reliably interact with their hardware
...
The downside to this is that anyone who wanted to develop applications
for cell phones needed to have intimate knowledge of the proprietary environment within
which it was to run
...
This isolated many of the “homebrew” developers
...
The term “homebrew” refers to the fact that these developers typically do
not work for a cell phone development company and generally produce small, one-off
products on their own time
...
Until recently, cell phones did little more than execute
and receive phone calls, track your contacts, and possibly send and receive short text
messages; not really the “Swiss army knives” of technology they are today
...

By 1997, small applications such as calculators and games (Tetris, for example) crept
their way onto cell phones, but the overwhelming function was still that of a phone dialer
itself
...
No one yet saw the need for Internet browsing, MP3 playing, or any of the
multitudes of functions we are accustomed to using today
...
However, even if the need was present, a lack of device memory
and storage capacity was an even bigger obstacle to overcome
...

To put the problem simply, it takes memory to store and run applications on any
device, cell phones included
...
Within the last two years, the price of memory has reached very low levels
...

Many cell phones now have more standard memory than the average PC had in the
mid-1990s
...


Chapter 1:

What Is Android?

Device manufacturers still closely guard the operating systems that run on their
devices
...

Even the systems that do allow some Java apps to run do not allow the kind of access
to the “core” system that standard desktop developers are accustomed to having
...
The Open Handset Alliance
is a group of hardware and software developers, including Google, NTT DoCoMo,
Sprint Nextel, and HTC, whose goal is to create a more open cell phone environment
...
(For more information about the Open Handset Alliance, see
www
...
com
...
Help files, the platform
software development kit (SDK), and even a developers’ community can be found at
Google’s Android website, http://code
...
com/android
...


NOTE
Google, in promoting the new Android operating system, even went as
far as to create a $10 million contest looking for new and exciting Android
applications
...
HTC, LG
Electronics, Motorola, and Samsung are members of the Open Handset Alliance, under
which Android has been released, so we can only hope that they have plans for a few
Android-based devices in the near future
...
This is good news for developers because it gives us
a rare advance look at a future system and a chance to begin developing applications that
will run as soon as the hardware is released
...


Introduction to Android
Android, as a system, is a Java-based operating system that runs on the Linux 2
...

The system is very lightweight and full featured
...


Figure 1-1 The current Android home screen as seen on the Android Emulator
...
If you have not yet downloaded Java or are unsure about which version you
need, I detail the installation of the development environment in Chapter 2
...

If you are familiar with Java programming or are an OOP developer of any sort, you
are likely used to programmatic user interface (UI) development—that is, UI placement
which is handled directly within the program code
...
XML
UI layout is a fairly new concept to the average desktop developer
...

One of the more exciting and compelling features of Android is that, because of its
architecture, third-party applications—including those that are “home grown”—are
executed with the same system priority as those that are bundled with the core system
...
Also, each application is executed within its own thread using a very
lightweight virtual machine
...
In other words, if you want to
create an application that dials the phone, you have access to the phone’s dialer; if you
want to create an application that utilizes the phone’s internal GPS (if equipped), you have
access to it
...

On top of all the features that are available from the Android side of the equation,
Google has thrown in some very tantalizing features of its own
...
Suppose you want to write an
application that pulls up a Google map of where an incoming call is emanating from,
or you want to be able to store common search results with your contacts; the doors of
possibility have been flung wide open with Android
...
You will learn the hows
and whys of using specific development environments or integrated development
environments (IDE), and you will download and install the Java IDE Eclipse
...
Google, after purchasing the original
developer of Android, released the operating system under the Open Handset Alliance
...
While I am sure that there will be ways to get around most any open
source system, applications need to be compiled using the Android SDK to run on
Android
...


Chapter

2

Downloading and
Installing Eclipse

Copyright © 2008 by The McGraw-Hill Companies
...


10

Android: A Programmer’s Guide

Key Skills & Concepts


Selecting a development environment



Downloading Eclipse



Installing and configuring Eclipse

A

ndroid applications are developed in Java
...
As such, you can theoretically use
any distribution or integrated development environment (IDE) you have at your disposal
to begin your development
...


TIP
In later chapters of this book, I will give you an introduction to developing Android
applications without the use of an IDE—or “in the command-line interface (CLI)
...


If you are more comfortable with one Java IDE over any other, such as JBuilder
by Borland or the open source NetBeans, feel free to use it
...
However, the Open Handset Alliance and Google do endorse one Java
IDE over any others: Eclipse
...
The
examples in this book give instructions only for compiling and testing in Eclipse, using
the Android plugin for Eclipse
...
Too many times, installation guides and

Chapter 2:

Downloading and Installing Eclipse

tutorials, in a quest to not shut out more technologically advanced readers, tend to skip
simple steps such as this
...
For this reason, I am including all of the download and
installation steps in this chapter
...
Eclipse is also very easy to use, with a minimal learning curve
...




The Open Handset Alliance has released an Android plugin for Eclipse that allows
you to create Android-specific projects, compile them, and use the Android Emulator
to run and debug them
...
You can still create Android apps in other IDEs, but
the Android plugin for Eclipse creates certain setup elements—such as files and
compiler settings—for you
...


NOTE
Eclipse is also available for Mac and Linux
...
However, the examples and screenshots in this book are given from
the Microsoft Windows version of Eclipse
...
If there is a major change
in operation of Eclipse under Linux, I will include an example of that change
...
The majority of these
examples will be from the Linux/Android command-line environment
...
Because
Eclipse as an application was written in Java, it requires the JRE to run
...


NOTE
Most people who have used the Web, or applications that are web-based, have the JRE
installed
...
To create Java applications, you need to download and install the Java
Development Kit (JDK), which contains all the tools and libraries needed to create Java
applications
...
For the
examples in this book, I will be downloading the JDK, because it also includes the JRE
...


Chapter 2:

Downloading and Installing Eclipse

Navigate to the Sun Developer Network (SDN) Downloads page at http://
developers
...
com/downloads/, as shown in the following illustration
...
The reason for downloading the full JDK is that
later in the book I will also give some examples of how to create Android applications
outside Eclipse, using just the JDK tools
...


13

14

Android: A Programmer’s Guide

From the SDN Downloads page, navigate to the download section for the proper JDK
...
To
download the Java 5 JDK, select the platform for which you want to download
...

However, if you do want to download the older JDK 5, you need to click the Previous
Releases link, as shown next:

Chapter 2:

Downloading and Installing Eclipse

NOTE
You must agree to and accept the Sun licensing agreement on this page before you can
initiate your download
...
0 Downloads
link, and then click the Download button for JDK 5
...


15

16

Android: A Programmer’s Guide

If you are downloading to a Microsoft Windows environment, when you see
the notification in the following illustration, click Run to begin the installation of
the JDK
...

However, if you choose to save the JDK, be sure to note the location
...


During the installation process, you will be prompted to read and accept the License
Agreement, shown next
...


Chapter 2:

Downloading and Installing Eclipse

There is very little you need to change here, unless you are a more seasoned Java
veteran and have particular options that you want to choose, in which case you should feel
free to change the selections as you see fit
...


To keep the process simple, and fairly standardized, you should accept the suggested
packages—by default everything is selected—and continue the installation by clicking
Next
...

However, if you have trouble in later chapters, you will want to modify your installation
options
...


17

18

Android: A Programmer’s Guide

Once you complete the Java JDK installation—and by default the JRE
installation—you can begin to install Eclipse
...
eclipse
...
As the opening paragraph states, the JRE is required (Java 5 JRE
recommended) to develop in Eclipse, which you took care of in the previous section
...
The package is relatively
small (79MB) and should download fairly quickly
...


Chapter 2:

Downloading and Installing Eclipse

After you have downloaded Eclipse, it is time to install it
...
As of the writing of this book, the latest
Eclipse package file for Microsoft Windows is eclipse-java-europa-fall2-win32
...

Expand the package and run the eclipse
...
Eclipse installs to your User directory by
default (under Microsoft Windows), but you may want to install it to your Program Files
directory
...
The following illustration shows the Eclipse title screen that
appears upon startup
...

If rebooting does not help, download and install the Java 5 JRE only
...
Just as in most development environments, projects are created in, and
saved to, a workspace
...
To select a different location, click Browse and navigate to it
...
By checking this box, you will have one less thing to worry about
when creating new projects and you will always know in what directory structure to find
your source files
...

After you select a location for your workspace, click OK
...
While the
installation of Eclipse seemed deceivingly quick, you still need to do some configuration
work before you can create your first Android project
...

Next you need to download and install the Android SDK, download and install the
Android plugin for Eclipse, and configure the Eclipse settings
...
You will then explore the Android SDK and begin creating your
first Hello World! application in Chapter 5
...


Q:

Can you use Eclipse (and the Android SDK) with a version of the JRE other than
version 5?

A:

Technically you can use Eclipse with versions 5 and newer
...


Chapter

3

Downloading and Installing
the Android SDK

Copyright © 2008 by The McGraw-Hill Companies
...


22

Android: A Programmer’s Guide

Key Skills & Concepts


Downloading the Android SDK



Using the Update feature of Eclipse



Downloading, installing, and configuring the Android plugin for Eclipse



Checking the PATH statement

I

n the previous chapter, you downloaded and installed your primary development
environment, Eclipse
...
You must
configure it in a way that will facilitate Android development
...
However, given that you have no libraries yet for understanding
how Android applications should behave, you cannot develop anything that will run on an
Android-based device
...
You must then download the related Android plugin for Eclipse
to utilize the SDK within the Eclipse IDE
...

If you have any development experience, you are most likely familiar with the process
of using an SDK
...
The Android SDK is no different from any other SDK in that it
contains all the Java code libraries needed to create applications that run specifically
on the Android platform
...


NOTE
Chapter 4 covers most of the functionality of the Android SDK in depth
...
google
...
The Google Android

Chapter 3:

Downloading and Installing the Android SDK

development home page contains a host of valuable tools and documents about developing
for the Android platform, including links to the Android developer forum (or
“community”)
...


TIP
If you ever encounter a problem while you are developing an Android application,
the first place you should look for an answer is the Android developers’ forum at
http://code
...
com/android/groups
...
There are discussion groups for
beginners, developers, and “hackers,” and a general-issue discussion group
...


Figure 3-1 The Google Android development home page
...
google
...

From the development home page, click the Download the SDK link under Getting
Started
...
The Android SDK is downloaded in a 79MB
(for Windows) package and it should download fairly quickly
...


NOTE
Download sizes for other operating systems may vary
...
The first of these steps is to obtain the Android plugin for Eclipse, after
which you will configure it
...
Both tasks
of downloading and installing the plugin can be performed at the same time, and
are relatively easy to do:
1
...
You will download the Android plugin for Eclipse from

within the Eclipse IDE
...
Choose Help | Software Updates | Find and Install
...
In the Install/Update window, which allows you to begin the process of downloading

and installing any of the plugins that are available to you for Eclipse, click the Search
for New Features to Install radio button and then click Next
...
The Update Sites to Visit page of the Install window, shown next, lists all the default

websites used for obtaining Eclipse plugins
...
To download the Android plugin,
you must tell Eclipse where to look for it, so click the New Remote Site button
...
In the New Update Site dialog box, shown next, you must enter two pieces of

information to continue: a name for your new site, and its associated URL
...

In the Name field, enter Android Plugin
...
google
...
Click OK
...
Feel free to use something other than Android Plugin
...
A new site named Android Plugin should now be in your list of available sites:

27

28

Android: A Programmer’s Guide

At this point Eclipse has not yet looked for the plugin; this is just a list of paths that you
can tell Eclipse to check when looking for new plugins to install
...
Check the check box next to Android Plugin and then click Finish
...

8
...


Chapter 3:

Downloading and Installing the Android SDK

9
...


NOTE
Keep in mind that all Eclipse plugins are installed to the %installpath%/eclipse/plugins
directory
...


29

30

Android: A Programmer’s Guide

10
...
At the time of this writing, the plugin version is

0
...
0
...
On the final plugin installation page, Feature Verification, click
Install All to complete the installation of the Android plugin
...


Configuring the Android Plugin for Eclipse
After installing the Android plugin for Eclipse, Eclipse should have prompted you to
restart the application
...
Restarting Eclipse
will ensure that the program has a chance to reinitialize with the plugin installed
...

The Android plugin for Eclipse is configured from the Preferences window of Eclipse
...
From the main Eclipse window, choose Window | Preferences
...
In the Preferences window, shown next, select Android in the menu on the left
...
Eclipse needs this information to be
able to access all the tools that are supplied with Android, such as the emulator
...
Check the Automatically Sync Projects to Current SDK check box and then

click Apply
...
It may help you in
future chapters, especially when command-line programming, to rename this directory
to something more manageable
...


4
...
If

you are using a Microsoft Windows machine, right-click Computer (or My Computer,
depending on your version of Windows) and select Properties to open the System
Properties dialog box
...

5
...
This is where you can edit your PATH statement
...
Under System Variables, find the variable PATH and double-click it
...
In the Edit System Variable dialog box, shown next, add the location of your Android

SDK—separated from the existing paths by a semicolon—and click OK to apply your
changes, and click OK again in the Environment Variables window
...
In the next chapter, you will explore the
Android SDK, and learn about its features
...


Ask the Expert
Q:

Is the Android SDK available for any languages other than Java?

A:

No
...


Q:

Will there be updates to the Android SDK?

A:

Yes! Even during the writing of this book, an SDK update was released that addresses
many issues within the platform
...


(continued)

33

34

Android: A Programmer’s Guide

Q:

If an update is released, how do I upgrade my SDK?

A:

Upgrading the SDK can be very tricky
...
During the writing of this book, both a new SDK and a new
plugin were released
...
However, this proved fruitless and left me with two conflicting versions,
neither of which worked correctly
...
The newest SDK then worked correctly
...


Chapter

4

Exploring the Android SDK

Copyright © 2008 by The McGraw-Hill Companies
...


36

Android: A Programmer’s Guide

Key Skills & Concepts


Using the Android SDK documentation



Using the Android SDK tools



Using the sample applications



Learning the life cycle of an Android application

N

ow that you have your development environment established, you are ready to
explore the Android SDK, which contains multiple files and tools specifically
intended to help you design and develop applications that run on the Android platform
...

You really need to be familiar with the Android SDK and its tools before you begin
programming
...
These libraries make up the core of the SDK and
will be the ones that you use most often, so take the time to learn all about these core
libraries
...
By
the end of the chapter, after familiarizing yourself with the contents of the Android SDK,
you will be comfortable enough to begin writing applications
...


CAUTION
I am not going to go over every minute detail of the Android SDK; Google does a very
good job of that within its documentation
...
I cover only the most important topics and items,
leaving you free to explore the rest in more depth yourself, at your own pace
...

The bulk of the Android SDK, in number of files, consists of documentation, with
programming APIs, tools, and samples comprising the rest
...


TIP
Chapter 3 suggested that you extract the Android SDK to the Program Files folder,
so that it would be easier to track
...


Navigate to the folder where you unpacked the Android SDK so that you can begin
to explore the folder structure within
...
jar (a compiled Java application containing the core SDK libraries and APIs) and
some release notes, the remainder of the Android SDK is divided into three main folders:


Docs

Contains all of the accompanying Android documentation

NOTE
Much of the documentation found in the Docs folder can also be found on the
http://code
...
com/android Android development site
...
Each API demo is compiled and run to illustrate the capabilities of Android
...


37

38

Android: A Programmer’s Guide

Android Documentation
The Android documentation is located in the Docs folder within the Android SDK at

...
The documentation that is supplied with the SDK includes steps
on downloading and installing the SDK, “Getting Started” quick steps for developing
applications, and package definitions
...
html file in the root of the SDK folder
...


You can navigate to all of the documentation that is included in the Android SDK by
using the links within documentation
...


Chapter 4:

Exploring the Android SDK

CAUTION
As you are navigating the Android SDK, you may think some pages are mislinked or
missing, because the right side of the screen may be blank when you click some links
...


In working with the Android SDK, I have found that there are sections of the
documentation that I refer to more than others
...
As you progress through the book and begin to develop your own
Android applications, you will find that the Reference Information section of the
documentation is more helpful
...
Take some
time to familiarize yourself with the Android SDK documentation and the hundreds of
documents that have been provided for you
...
/%sdk folder%/SAMPLES, contains six sample applications that
demonstrate a good cross-section of Android functionality:


API Demos



Hello, Activity!



Lunar Lander

39

40

Android: A Programmer’s Guide



Note Pad



Skeleton App



Snake

These sample applications are provided by Google to give you a quick idea of how to
develop an Android application
...
You can open and run these applications from within Eclipse
...


API Demos
The API Demos application is a host application that demonstrates multiple API functions
in a single Activity
...
Activities are covered in more depth in the
following chapters
...


Try This

Run the API Demos Sample Application

Using Eclipse, load the API Demos application as a New Android Project
...
Do not worry about the options in this wizard for now
...
When the project is loaded, choose Run to see it execute in the Android Emulator
...
Use each
application to become familiar with the terminology and function of each API tool
it demonstrates
...
Though simple in its design, Hello, Activity! does a good job
of showing off the abilities of the platform
...


41

42

Android: A Programmer’s Guide

Lunar Lander
Lunar Lander, shown next, is a small game that plays on the Android Emulator
...
The controls are fairly simple,
and the game is not very complex
...


Lunar Lander implements a simple control scheme (Up, Down, Left, and Right)
...
Complex game theories such as collision detection are used in a simple
way
...


Chapter 4:

Exploring the Android SDK

Note Pad
Note Pad, as shown in the illustration that follows, allows you to open, create,
and edit small notes
...
However, it does
a good job as a demonstration tool to show what is possible with a relatively
small amount of code
...
This is more of a base application that
demonstrates a couple of different application features, such as fonts, buttons, images, and
forms
...
You will be better served by referring to Skeleton App as a resource for how to
implement specific items
...
This is a
small, SNAFU-style game
that is far more simplistic
than Lunar Lander
...


Chapter 4:

Exploring the Android SDK

NOTE
If you navigate to the base folder of each of the sample applications, you will see a
folder named src
...
You
can use this to view, edit, and recompile the code for any of the applications
...


Android Tools
The Android SDK supplies developers with a number of powerful and useful tools
...
This section takes a
quick look at just a few of these tools, which will be covered in much more depth in the
following chapters, as you dive into command-line development
...


emulator
...
exe
...
exe launches the Android Emulator
...
Given that, as of the writing of this book,
there were no hardware devices yet released for the Android platform, emulator
...

You can run emulator
...

In this book, you’ll usually let Eclipse launch the Android Emulator environment for you
...
exe
when you create your Hello World! applications
...
First, the emulator comes with usable buttons, as shown in
Figure 4-1
...


TIP
The Power On/Off, Volume Up, and Volume Down buttons are slightly hidden to the
sides of the virtual device
...


45

46

Android: A Programmer’s Guide

Volume up

Volume down

Power on/off

Full QWERTY keyboard

Full telephone keypad

Menu
Back

Home

End call

Send call

Left, right, up, down, and Select pad

Figure 4-1 Navigating with the Android Emulator

Given that many higher-end phones now include a touch screen, the second input
choice you have when using the emulator is a simulated touch screen
...
The objects on the emulator’s screen can be interacted with using
the mouse
...
exe
Another tool that will become very useful to you when you are using command-line
programming is Android Debug Bridge or adb (adb
...
This tool allows you to issue

Chapter 4:

Exploring the Android SDK

commands to the Emulator
...
When you are working in a command-line
environment, the adb tool allows you to do the following:


Start and stop the server



Install and uninstall applications



Move files to and from the emulator

MKSDCARD
...
exe is a very useful tool if you are testing an application that will need
to read or write files to or from an SD Memory Card inserted into the mobile device
...
exe creates a small partition drive on your drive that will hold and retain
the test files
...


DX
...
exe is the compiler of the Android SDK
...
exe
will create files with
...
These files are in the
correct format to be understood by, and run on, an Android device
...
The Dalvik virtual machine
runs each application in its own thread with the same priority as core Android
applications
...
bat or
...
When run from the command line, activityCreator will set up the shell files
needed to create a basic Android application
...
The Android plugin for Eclipse sets up these shell files for
you by calling the activityCreator when you create a new project
...
If you are in a Windows
environment, this will be a
...
pn) script
...


47

48

Android: A Programmer’s Guide

APIs
The API, or application programming interface, is the core of the Android SDK
...
The Android
API contains all the specific information that you need to create applications that can
work on and interact with an Android-based application
...
Subsequent chapters will focus much more on these APIs as you
begin writing applications that utilize them
...


Google APIs
The Google APIs are included in the Android SDK and contain the programming
references that allow you to tie your applications into existing Google services
...

Located in the android
...
google
...
There are quite a few packages that are included with the Google API
...
However, the packages devoted to Google Maps will be
the primary focus in this book
...
google
...
maps package, which contains information for Google
Maps, you can create applications that interact seamlessly with the already familiar
interface of Google Maps
...

The Google API also contains a useful set of packages that allows you to take
advantage of the newer Extensible Messaging and Presence Protocol (XMPP) developed
by the Jabber open source community
...
The API packages dealing with XMPP are very useful if you want
to create a “chat”-style program that utilizes the phone messaging capabilities
...
These Optional APIs are considered optional
because they deal with functionality that may or may not be present on a given handset

Chapter 4:

Exploring the Android SDK

device
...

One of these optional features (which you will use later in the book) is a cell-phone-based
GPS
...
(Combine the
information in the Android LBS API with that in the Google Maps API, and you might
have a very useful application that can automatically display a map of where you are
located at any given point in time
...


Application Life Cycle
If you have a decent amount of experience as an application developer, you are familiar
with the concept of an application life cycle
...
Every
application, regardless of the language it was written in, has a specific life cycle, and
Android applications are no exception
...


Standard ASP Application Life Cycle
The life cycle of a standard ASP application is similar enough to that of an Android
application to make this a good comparison
...
These steps are required to be implemented
by all ASP applications, and really define what an ASP application is
...
Application_Start
2
...
HTTPApplication
...
Disposal
5
...
However, the Disposal call can be intercepted before it is passed to
Application_End
...


Application_Start is called when the application is requested from the server
...
When all associated application modules
have loaded, HTTPApplication
...
The application executes its events, and
when the user attempts to close it, Dispose is called
...

This is a fairly standard application life cycle
...
The following
section demonstrates how this compares to the Android application life cycle
...
All Android applications, or Activities, are run within their own
process
...


NOTE
When deciding whether an activity should be shut down, Android takes into account
several factors, such as user input, memory usage, and processing time
...
Though there are a few differences, many application developers
should be comfortable with the steps in the life cycle
...
From the time I started writing this book, Google had already updated the Android
SDK three times
...


Q:

Do any of the API Demos represent applications that will be in the finished product?

A:

Probably not
...

Although there may be core “release” applications that contain some of the elements
found in the API Demos, we probably will not see Lunar Lander in the finished version
...
Click here for terms of use
...
xml file



Running applications on the Android Emulator

I

n this chapter, you will be creating your first Android Activity
...
I will show you how to create an
Android project in Eclipse, add code to the initial files, and run the finished application
in the Android Emulator
...

Actually, as you move through this chapter, you will be creating more than one
Android Activity
...
Then,
for the sake of enabling you to get to know the language better, the next section explains
in detail the files automatically created by Android for your Hello World! application
...
You will also create two different versions of a
Hello World! application that will display an image that delivers the “Hello World!”
message
...


NOTE
You will often see “application” and “Activity” used interchangeably
...
Each “window” or screen of your
application is a separate Activity
...
In future chapters you will create applications with multiple Activities
...
In other words, this chapter covers
the creation process in Eclipse, and Chapter 6 covers the creation process using the
command-line tools
...
Review the steps in Chapter 3 for setting the PATH
statement for the Android SDK
...


TIP
If you have configuration-related issues while attempting to work with any of the
command-line examples, try referring to the configuration steps in Chapters 2 and 3;
and look at the Android SDK documentation
...
When you open Eclipse for the first
time, it opens to an empty development environment (see Figure 5-1), which is where
you want to begin
...
Choose File | New | Android Project, which will launch the New Android
Project wizard
...
While Android applications are written
in Java, and you are doing all of your development in Java projects, this option will
create a standard Java application
...

If you do not see the option for Android Project, this indicates that the Android plugin
for Eclipse was not fully or correctly installed
...


The New Android Project wizard creates two things for you:


A shell application that ties into the Android SDK, using the android
...
This allows you to code using all of
the Android libraries and packages, and also lets you debug your applications in
the proper environment
...
These shell files contain some of the
vital application blocks upon which you will be building your programs
...
NET application in Visual Studio
generates some Windows-created program code in your files, using the Android
Project wizard in Eclipse generates your initial program files and some
Android-created code
...


Chapter 5:

Application: Hello World!

For the Project Name field, for purposes of this example, use the title HelloWorldText
...

In the Contents area, keep the default selections: the Create New Project in Workspace
radio button should be selected and the Use Default Location check box should be checked
...
The
advantage of keeping the default options is that your projects are kept in a central location,
which makes ordering, managing, and finding these projects quite easy
...


57

58

Android: A Programmer’s Guide

If you are working in a Microsoft Windows environment, the workspace path will be
C:/Users//workspace, as shown in the previous illustration
...
One reason you may
want to specify a different location here is simply if you want to choose a location for this
specific project that is separate from other Android projects
...
If so, simply override the Location option to specify
your own custom location directory for this project
...
Checking that box during the Eclipse setup defaults all new projects to the workspace
directory (and provides the default location shown in the Location field of the New Android
Project wizard)
...

The final three options in the New Android Project wizard are in the Properties area
...
In
the Package Name field, you specify the namespace given to your application package
...
app
...
google
...
map
...


CAUTION
The package name adheres to the standard Java package-naming guidelines, which
were established to lower the risk of two packages being released with the same name
...
This is followed by the domain name, such as google
...
For purposes of this chapter,
my package name for the Hello World! application will omit “com” to identify that it is
a text application and not meant to be published
...


For the HelloWorldText application, use the package name android_programmers_
guide
...
This name uniquely identifies the code that belongs to this
application and differentiates this test application from others you will develop in this book
...
This error message is premature and
can be a bit confusing because you have not even attempted to fill out the other fields in
the Properties area
...


Chapter 5:

Application: Hello World!

The next Properties field, Activity Name, is required because it is the reference to the
main screen of your application
...
Without an Activity, your application would not
do very much
...
Activity Name is a required field and has no default, so you must supply one
to continue (as indicated in the preceding caution)
...
This keeps the application simple and is just about as descriptive
as it needs to be for the moment
...

This is the name that will be used to manage your application when it is installed on
the device
...
The following illustration shows the completed New Android
Project wizard
...
In fact, many
programmers are used to the older conventions whereby the “starting” screen of an
application is usually called Main or Startup
...
For purposes of demonstration, this chapter assumes that you are
using the names suggested
...
The wizard runs a background process
that facilitates the auto-generation of some required files, and the setup of the directory
structure needed to support an Android application
...


Figure 5-2 Your first Android application project

Chapter 5:

Application: Hello World!

TIP
If the Finish button is not available to you, you may have made an error in one of the
fields in the Properties area
...
Go back and make sure that all of the Properties fields are
correctly filled in
...


Examining the Android-Created Files
This section discusses the new files that Android has just created for you
...
There are files provided by
Android that you need to modify, and there are ones that you should not modify; knowing
the difference may save you from having to re-create your project
...
The following
illustration shows what the Package Explorer should look like
...


61

62

Android: A Programmer’s Guide

You should see a root directory, in this case named HelloWorldText
...
Both your user-created files and
the Android auto-generated files will be placed in the directory, easily accessible from
the Package Explorer
...
xml file, a package included in the Referenced Libraries, and three
directories (res, assets, and src)
...


AndroidManifest
...
xml file is where your global settings are made
...
NET developer, you can think of AndroidManifest
...
config and
Global
...
(If you are not an ASP
...
xml is a place for storing settings
...
xml will include
such settings as application permissions, Activities, and intent filters
...
xml file should contain the following information:
...
android
...
HelloWorldText">



...
com/apk/res/android
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, HelloWorldText"
/>


The last directory under res, values, holds an XML file named strings
...
xml
file is used to hold global string values that can be referenced by your application
...
The files contained in the assets
directory can include audio files for streaming and animation assets
...


src Directory
The src directory contains all the source files for your project
...
java and
...
java), described next
...
java is always named according to your Activity name
...
java File The file R
...
This file contains pointers into the drawable, layout, and values
directories (or the items within the directories, as is the case with strings and icons)
...
You will be referencing R
...
The code that was auto-generated for the HelloWorldText
application follows:

Chapter 5:

Application: Hello World!

/* AUTO-GENERATED FILE
...

*
* This class was automatically generated by the
* aapt tool from the resource data it found
...

*/
package testPackage
...
java file provides an explanation of the origin of the file
...
In Chapter 6, when you create a
command-line–only version of the Hello World! application, you will use command-line
tools to create all of the auto-generated files
...
java File The file in the src directory that you will spend the most time with
is
...
java in this example), which is created by the Android
plugin and named to match the Activity name that you specified in the New Android
Project wizard
...

After briefly looking at what is in your HelloWorldText
...

package android_programmers_guide
...
app
...
os
...
*/
@Override
public void onCreate(Bundle icicle) {
super
...
layout
...
In this case, you have the definition and inclusion of your
package android_programmers_guide
...

The next two lines import specific packages from the Android SDK via android
...
app
...
os
...
These two lines are critical for your base Android application
and should not be removed
...
os
...
Eclipse rolls up all the import statements under
the first one, so you must expand the tree to see the rest of them
...
Activity is imported from the previous lines
...
For something to
run and be displayed on the screen, it must be derived from Activity
...
Right now there is only one method in your HelloWorldText class that is
defined with code in it, onCreate( )
...
That is, all of the current
state information is bundled as an icicle object and held in memory
...


Chapter 5:

Application: Hello World!

The next line in the file is the one that really does some perceptible action:
setContentView(R
...
main);

The method setContentView( ) sets the Activity’s content to the specified resource
...
xml file from the layout directory via the pointer in
the R
...
The main
...
The TextView is derived from View and is used
to display text in an Android environment
...
xml, you can
see that it contains the following line:
android:text="Hello World, HelloWorldText"

Considering that the setContentView( ) method is being told to set main
...
xml contains a TextView that says “Hello World, HelloWorldText,”
it may be safe to assume that compiling and running HelloWorldText now will give
you your Hello World! application
...
Choose Run | Run to open the Run As dialog box, select Android Application,
and click OK
...
However, that is not very engaging, nor does it teach you very
much about programming an Android application
...

What happened when you created the new Android project is that the Android plugin
modified main
...
This is a perfect example of one way to modify the UI in Android
...
xml by the Android SDK when the project
is created:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, HelloWorldText"
/>

While I have discussed the existence of this TextView in the xml, I have not yet
discussed why it works without any corresponding code
...
xml file
...
” Edit this line of the main
...

Take some time
and experiment with
the xml TextView
...


Chapter 5:

Application: Hello World!

Hello World! Again
In this section, you will create another Hello World! application for Android
...
The first step here is to remove the TextView code that is in
main
...
The following section of code represents the TextView
...

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, HelloWorldText"
/>

After you have removed the TextView code, your main
...
0" encoding="utf-8"?>
...
com/apk/res/android
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>


Now that you have a clean main
...
Start by opening the
HelloWorldText
...
layout
...


This line uses setContentView( ) to draw the main
...
Since you
will not be using main
...
Instead, you will be building the TextView in code
...
widget
...
Place this
code near the top of your current HelloWorldText
...
widget
...
By creating the TextView instance, you can use
it to display text to the screen without directly modifying main
...
Place the following
code after the onCreate( ) statement is fired:
TextView HelloWorldTextView = new TextView(this);

NOTE
TextView takes a handle to the current context as an argument
...
If you follow the hierarchy
through the SDK, HelloWorldText extends Activity, which extends
ApplicationContext, which in turn extends Context
...


The preceding line creates an instance of TextView named HelloWorldTextView
and then instantiates HelloWorldTextView, by setting it to a new TextView
...

Now that the TextView is defined, you can add your text to it
...
setText("Hello World!");

This line lets you set the text of your TextView
...

Your TextView has been created and now contains the message that you want to
display
...
As discussed previously, you need to set the ContentView to

Chapter 5:

Application: Hello World!

display something to the screen
...
The
preceding three lines of code are what it takes to make your Hello World! application
...
All things
considered, this is not very complicated at all
...
java file should look like the following:
package android_programmers_guide
...
app
...
os
...
widget
...
*/
@Override
public void onCreate(Bundle icicle) {
super
...
setText("Hello World!");
/**Set ContentView to TextView */
setContentView(HelloWorldTextView);
/**END
*/
}
}

Now compile and run your new Hello World! application in the Android Emulator
...
The following illustration depicts the results of your Hello World!
application
...
This small project demonstrated
a fairly common execution of a Hello World! application
...
The following section looks at a slightly different way of
implementing Hello World!, using an image
...
Modern computer
displays would be exceedingly uninteresting without a graphical display
...


Chapter 5:

Application: Hello World!

As late as five years ago, displaying images was a fairly difficult thing to do on a cell
phone
...
We look at windows of all types everyday without even considering that they
are really images sent to a screen
...


With the application project created, navigate to and remove the TextView code from
main
...
If you do not remove this code, you will end up
with a text-based Hello World! program again
...
Create a small
image in the graphics program of your choice
...
The image I am using is
shown here:

Name your image helloworld
...


CAUTION
Be careful not to mix upper- and lowercase letters in your image names
...
If you mix in some uppercase letters, you
will get an error message from Eclipse when you try to use the file
...
The helloworld
...


Chapter 5:

Application: Hello World!

Open R
...
Eclipse should have added a pointer to
helloworld
...
Your R
...
DO NOT MODIFY
...
It
* should not be modified by hand
...
HelloWorldImage;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int helloworld=0x7f020000;
public static final int icon=0x7f020001;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040000;
}
}

With a clean application shell as your starting point, and an available handle to the
image you want to display, you can begin to add your code
...


Hello World! Code-Based UI
Assuming you were able to follow along with and understand the HelloWorldText
solution, this version of Hello World! will seem very familiar
...
Whereas text is displayed using a TextView,
images are displayed using ImageView
...
Like TextView, ImageView is contained in android
...
widgets
...
This makes both of them very
similar in structure and easy to implement
...
Instantiating ImageView is the same as instantiating TextView; create an instance
of ImageView and pass it the current context using this:
ImageView HelloWorldImageView = new ImageView(this);

The next line is where a difference between ImageView and TextView can be seen
...
In the
TextView example, you used setText( ) to set the text of the TextView to “Hello World!”
While both TextView and ImageView are derived from View, they are still different and
therefore require some different methods
...
You need to use setImageResource( ) to set the image in your
ImageView
...
png
from R
...
drawable
...
setImageResource(R
...
helloworld);

Finally, to send the image to the screen, you must set the ContentView
...
The job of the
ContentView is then to set the object that it is passed to the screen
...
java file should look like this:
package android_programmers_guide
...
app
...
os
...
widget
...
*/

Chapter 5:

Application: Hello World!

@Override
public void onCreate(Bundle icicle) {
super
...
png */
HelloWorldImageView
...
drawable
...
Your application
should look similar to that in the following illustration
...
png, but this time using XML
rather than code
...
As you are going to
see, the process of sending images to the screen using main
...
However, the syntax differs between the two
processes
...
java file
...
HelloWorldImage;
import android
...
Activity;
import android
...
Bundle;
public class HelloWorldImage extends Activity {
/** Called when the activity is first created
...
onCreate(icicle);
}
}

Now that you have a clean slate to start with, move over to main
...
You need to add
in a definition for an ImageView
...
xml file:
/>

You need to edit four attributes of the ImageView: android:id, android:layout_width,
android:layout_height, and android:src
...


Chapter 5:

Application: Hello World!

The android:id attribute is set to the identifier for the ImageView
...
Use the @+id/
syntax to assign to the ImageView an identity that can be retrieved later using
R
...
imageview:
android:id="@+id/imageview"

This line inserts an auto-generated ID, @+id, into the R
...

The next two attributes that you must define are android:layout_width and android:layout_
height
...
There are two options
you are going to select from when assigning values to these attributes
...
The wrap_content value
keeps the image its defined size, possibly losing some of the image definition in the
process
...
This
attribute points to the image that you want to display to the view
...
xml to
setContentView in HelloWorldImage
...
layout
...
The results should look like the following
illustration
...
Go back to main
...
When you are finished, your main
...
0" encoding="utf-8"?>
...
com/apk/res/android
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/helloworld"
/>


Chapter 5:

Application: Hello World!

Run the application again to see the difference between wrap_content and fill_parent
...


Try This

Use TextView and ImageView

Use some of the skills and techniques that you learned in this chapter to create a new
Hello World! application
...
This is slightly more
difficult than using just one View on an Activity
...


The next chapter takes one more look at Hello World! applications, from the
perspective of command-line programming
...
All text displays are facilitated through the TextView
...


Q:

Is there an advantage to using
...
xml to
create Views?

A:

While there is no documented speed or processor savings in using one over the other,
there is one key advantage: By using main
...
Then, in your code, you can jump from View to View as needed
without having to manually create them in code
...
Click here for terms of use
...
At this point, you should be fairly comfortable using Eclipse to
create and run a small Android application
...
xml and
...
java file
...

In this chapter, you are going to expand and round out those skills by experimenting
with command-line application development
...
The Android SDK offers a host of
command-line tools that can help you develop full applications without the need
of a graphical IDE
...


Creating a Shell Activity Using the Windows CLI
The Android SDK comes with multiple tools to help you create and compile Android
applications
...
However, if you are doing all of
your Android development work within Eclipse, you still should be aware of the Android
SDK command-line tools and their functionality
...
These Android command-line tools, whether run
from a command-line interface or from a GUI IDE, are the real core to the functionality
of the Android SDK
...
The
ActivityCreator
...


Running the ActivityCreator
...
bat should be located in the
...

Most of the forward-facing command-line tools are located in the root of the tools
directory
...
/tools/ directory
...
bat is an example of a tool from the root of the tools
directory that actually calls another tool when it is run
...
bat; it should contain the following lines of code:

NOTE
ActivityCreator
...
In
a later section of this chapter you will also learn about the ActivityCreator
...
This is the
Linux version of the ActivityCreator
...

rem
rem Licensed under the Apache License, Version 2
...

rem You may obtain a copy of the License at
rem
rem
http://www
...
org/licenses/LICENSE-2
...

rem See the License for the specific language governing permissions and
rem limitations under the License
...
exe" %*

Navigating through all of the rem statements (batch file comment statements), you
will see that there is one line of practical code at the bottom of the file
...
bat
is used to call ActivityCreator
...
/tools/lib/activityCreator/ directory
...
bat is an example of a tool that is really just a front end of other tools
in the SDK
...
bat (or ActivityCreator
...
This directory structure is the same structure
discussed in Chapter 5
...
bat creates R
...
xml, and all
the supporting files needed to begin your application
...

From your Start menu, click Run, type CMD or COMMAND in the Run dialog box,
and click OK
...
This window is
the equivalent of the older DOS operating environments
...


TIP
The Microsoft command-prompt interface is not case sensitive, by default
...


Running the command ActivityCreator, which actually runs ActivityCreator
...
ActivityName

Creates the structure of a minimal Android application
...
xml: The application manifest file
...
xml: An Ant script to build/package the application
...

- src : The source directory
...
java the Activity java class
...

...

- bin : The output folder for the build script
...

--ide intellij: creates project files for IntelliJ

This output simply indicates that you need to provide more information to run
ActivityCreator
...


NOTE
The output from the ActivityCreator command gives you a lot more information than just
the fact that you did not provide enough information
...
This list should look similar to that covered in Chapter 5,
although build
...


Go back to the command window and run ActivityCreator with the following option
(ActivityCreator also accepts path parameters with Unix-style forward slashes, if you are
used to programming in a Unix/Linux environment):
--out c:\AndroidHelloWorld\android_programmers_guide
...
This command
option takes two parameters, and ...
The first part of the
preceding line tells ActivityCreator to build the shell application in the nonexistent folder
c:\AndroidHelloWorld
...


The second parameter of the --out option is the package name and the activity name
...


87

88

Android: A Programmer’s Guide

NOTE
The parameters needed to successfully run ActivityCreator and set up your initial
environment are the same as those required by the New Android Project wizard
...
You
should see the following output from the tool:

The following section covers the files created by ActivityCreator, because they do
vary slightly from those created by Eclipse
...
Navigate to the c:\AndroidHelloWorld\ directory to explore its structure
...


Chapter 6:

Using the Command-Line Tools and the Android Emulator

Because you are working outside of the Eclipse environment, you have a slightly
different environment
...
Given that you are working without
any IDE help, ActivityCreator creates a file that outlines what the complier needs to do
to create your project
...
xml file for
your project
...

It contains an instruction set that explains how to turn your
...


89

90

Android: A Programmer’s Guide

The build
...
The
compiler in this example is Apache ANT, a Java-based tool that uses build files as scripts
to compile projects
...

Download ANT from http://ant
...
org/bindownload
...

Once you have ANT downloaded and installed, you must add it to the PATH statement
...

The build
...
It should be located in the root of your project, as shown in the previous
illustration
...
xml with your text editor and take a look at what is inside
...
xml contains code that is editable by the user
...

...
aidl"
/>










91

92

Android: A Programmer’s Guide


value="zip" />









destdir="${outdir-classes}"
bootclasspath="${android-jar}" />



Packaging resources and assets
...
xml" />











Packaging resources
...
xml" />









93

94

Android: A Programmer’s Guide



property="res-target" value="and-assets" />





Packaging java
...
dex files into the output package file
...
7 has an internal zip command,
however Ant 1
...
5 lacks it and is still widely installed
...

























destdir="${outdir-classes}"
bootclasspath="${android-jar}" />




Packaging resources and assets
...
xml" />



101

102

Android: A Programmer’s Guide




value="-A" />
value="${asset-dir}" />
value="-I" />
value="${android-jar}" />
value="${out-package}" />



Packaging resources
...
xml" />










property="res-target" value="and-assets" />





Packaging java
...
dex files into the output package file
...







Chapter 6:

Using the Command-Line Tools and the Android Emulator