ptg
ptg
800 East 96th Street, Indianapolis, Indiana 46240 USA
Lauren Darcey
Shane Conder
Sams Teach Yourself
24
in
Hours
Android
™
Application Development
ptg
Sams Teach Yourself Android™ Application Development in 24 Hours
Copyright © 2010 Lauren Darcey and Shane Conder
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or
transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without
written permission from the publisher. No patent liability is assumed with respect to the use of
the information contained herein. Although every precaution has been taken in the preparation of
this book, the publisher and author assume no responsibility for errors or omissions. Nor is any
liability assumed for damages resulting from the use of the information contained herein.
Visible Earth images owned by NASA, http://visibleearth.nasa.gov/.
ISBN-13: 978-0-321-67335-0
ISBN-10: 0-321-67335-2
Library of Congress Cataloging-in-Publication Data
Darcey, Lauren, 1977-
Sams teach yourself Android application development in 24 hours / Lauren Darcey, Shane
Conder.
p. cm. — (Sams teach yourself in 24 Hours)
Includes index.
ISBN 978-0-321-67335-0 (pbk.)
Trina MacDonald
Development
Editor
Michael Thurston
Managing Editor
Kristy Hart
Project Editor
Betsy Harris
Copy Editor
Kitty Wilson
Indexer
Erika Millen
Proofreader
Sheri Cain
Technical Editor
Jonathan Jackson
Publishing
Coordinator
Olivia Basegio
Book Designer
Gary Adair
Senior Compositor
Gloria Schurick
ptg
Contents at a Glance
Introduction. 1
Part I: Android Fundamentals
HOUR 1 Getting Started with Android . 7
2 Mastering the Android Development Tools . 27
Part VI: Appendixes
A Configuring Your Android Development Environment 409
B Eclipse IDE Tips and Tricks . 415
C Supplementary Materials. 423
ptg
Table of Contents
Introduction 1
Part I: Android Fundamentals
HOUR 1: Getting Started with Android 7
Introducing Android . 7
Familiarizing Yourself with Eclipse 9
Running and Debugging Applications . 17
Summary . 23
Q&A. 23
Workshop 24
HOUR 2: Mastering the Android Development Tools
27
Using the Android Documentation . 27
Debugging Applications with DDMS . 29
Working with the Android Emulator . 35
Using Other Android Tools . 38
Summary . 39
Q&A. 40
Workshop 40
HOUR 3: Building Android Applications
43
Designing a Typical Android Application . 43
Using the Application Context . 46
Working with Activities . 47
95
Designing an Android Trivia Game 95
Implementing an Application Prototype . 102
Running the Game Prototype . 107
Summary 109
Q&A 110
Workshop . 110
vi
Teach Yourself Android Application Development in 24 Hours
ptg
Part II: Building an Application Framework
HOUR 7: Implementing an Animated Splash Screen 113
Designing the Splash Screen . 113
Implementing the Splash Screen Layout . 114
Working with Animation . 119
Summary 123
Q&A 124
Workshop . 124
HOUR 8: Implementing the Main Menu Screen
127
Designing the Main Menu Screen 127
Implementing the Main Menu Screen Layout 131
Working with the ListView Control 134
Working with Other Menu Types . 138
Summary 141
Q&A 141
Workshop . 141
HOUR 9: Developing the Help and Scores Screens
143
HOUR 12: Adding Application Logic
197
Designing the Game Screen . 197
Implementing the Game Screen Layout . 200
Working with ViewSwitcher Controls . 203
Wiring Up Game Logic. 208
Summary 214
Q&A 215
Workshop . 215
Part III: Enhancing Your Application with Powerful Android Features
HOUR 13: Working with Images and the Camera 217
Designing the Avatar Feature . 217
Adding an Avatar to the Settings Screen Layout . 219
Working with ImageButton Controls . 221
Working with Image Media . 223
viii
Teach Yourself Android Application Development in 24 Hours
ptg
Working with Bitmaps . 228
Summary 230
Q&A 230
Workshop . 231
HOUR 14: Adding Support for Location-Based Services
233
Designing the Favorite Place Feature . 233
Implementing the Framework for the Favorite Place Feature . 237
Using Location-Based Services . 240
Using Geocoding Services . 246
Working with Maps 248
Integrating with Social Networking Services. 300
Summary 302
Q&A 302
Workshop . 303
HOUR 18: Creating a Home Screen App Widget
305
Designing an App Widget. 305
Handling App Widget User Events 313
Working with Widget Background Operations 314
Summary 318
Q&A 318
Workshop . 319
Part IV: Adding Polish to Your Android Application
HOUR 19: Internationalizing Your Application 321
General Internationalization Principles. 321
How Android Localization Works. 322
Android Internationalization Strategies . 327
Using Localization Utilities 329
Summary 330
Q&A 331
Workshop . 332
HOUR 20: Developing for Different Devices
333
Configuration Management for Android. 333
Summary 343
Q&A 343
Workshop . 344
x
Teach Yourself Android Application Development in 24 Hours
xi
ptg
HOUR 24: Publishing on the Android Market 395
Selling on the Android Market 395
Exploring Other Android Publishing Options . 402
Summary 405
Q&A 405
Workshop . 406
Part VI: Appendixes
APPENDIX A: Configuring Your Android Development Environment 409
Development Machine Prerequisites . 409
Installing the Java Development Kit . 410
Installing the Eclipse IDE . 410
Installing the Android SDK 411
Installing and Configuring the Android Plug-in for Eclipse (ADT) . 412
Upgrading the Android SDK 413
Configuring Development Hardware for Device Debugging . 413
APPENDIX B: Eclipse IDE Tips and Tricks
415
Creating New Classes and Methods . 415
Organizing Imports 415
Documenting Code 416
Using Auto-Complete. 416
Editing Code Efficiently . 416
Renaming Almost Anything . 417
Formatting Code 418
Organizing Code 418
Fun with Refactoring . 418
Resolving Mysterious Build Errors. 420
Kenya, gotten thirsty in Egypt, narrowly avoided a coup d’état in Thailand, geocached her
way through the Swiss Alps, drank her way through the beer halls of Germany, slept in the
crumbling castles of Europe, and gotten her tongue stuck to an iceberg in Iceland (while
being watched by a herd of suspicious wild reindeer).
Shane Conder has extensive development experience and has focused his attention on
mobile and embedded development for the past decade. He has designed and developed
many commercial applications for BREW, J2ME, Palm, Windows Mobile, and Android—
some of which have been installed on millions of phones worldwide. Shane has written
extensively about the mobile industry and evaluated mobile development platforms on his
tech blogs and is well known within the blogosphere. Shane received a B.S. in Computer
Science from the University of California.
A self-admitted gadget freak, Shane always has the latest phone or laptop. He can often be
found fiddling with the latest technologies, such as Amazon Web Services, Android, iPhone,
Google App Engine, and other exciting, state-of-the-art technologies that activate the creative
part of his brain. He also enjoys traveling the world with his geeky wife, even if she did make
him dive with 4-meter-long great white sharks and almost get eaten by a lion in Kenya. He
admits that it was his fault they got attacked by monkeys in Japan, that he snickered and
whipped out his Android phone to take a picture when Laurie got her tongue stuck to that ice-
berg in Iceland, and that he still hasn’t learned his lesson about writing his own bio.
Other Publications by the Authors
The authors have also published Android Wireless Application Development, part of the
Addison-Wesley Developer’s Library series, as well as numerous online technical articles for
http://developer.com, http://informIT.com, and their own Android blog, http://android-
book.blogspot.com.
xiv
ptg
Dedication
To grandparents the world over, especially those who are kind and generous, bake awesome
pecan pies, and like to watch America’s Most Wanted. You are not a bother but a blessing.
and phone or email address. I will carefully review your comments and share them with the
author and editors who worked on the book.
Email: [email protected]
Mail: Mark Taub
Editor-in-Chief
Sams Publishing
800 East 96th Street
Indianapolis, IN 46240 USA
Reader Services
Visit our website and register this book at http://informit.com/register for convenient access
to any updates, downloads, or errata that might be available for this book.
xvi
ptg
Introduction
The Android platform is packing some serious heat these days in the mobile marketplace
and gaining traction worldwide. The platform has seen numerous advancements in terms
of SDK functionality, handset availability, and feature set. A wide diversity of Android
handsets and devices are now shipping and (finally) in consumers’ hands—and we’re not
just talking about phones: Android has begun to ship on netbooks, Internet tablets (such as
the ARCHOS 5), ebook readers (like the Barnes & Noble nook), digital photo frames, and a
variety of other consumer electronics. There are even proof-of-concept appliances such as
an Android microwave and washer/dryer combo. (Hey, why not? See http://bit.ly/bGqmZp.)
Mobile operators and carriers are taking the platform seriously and spending gazillions on
ad campaigns for Android phones—like Verizon’s Droid campaign.
In the past year or so, the Android platform has transitioned from a “gearheads-only” plat-
form to providing some serious competition to more established platforms. (Yes, we’re talk-
ing about platforms such as the iPhone.)
But let’s not digress into an argument over whose platform is better so early, okay? Because,
honestly, you’re wasting your time if you think there’s one platform to rule them all. The
We do assume that you’re somewhat comfortable installing applications on a computer (for
example, Eclipse, the Java JDK, and the Android SDK) and tools and drivers (for USB access
to a phone), and we assume that you can navigate your way around an Android handset
well enough to launch applications and such. No wireless development experience is neces-
sary.
How This Book Is Structured
In 24 easy one-hour lessons, you’ll design and develop a fully functional network- and LBS
(Location-Based Services)-enabled Android application, complete with social features. Each
lesson builds on your knowledge of newly introduced Android concepts, and you’ll iterative-
ly improve your application from chapter to chapter.
This book is divided into six parts:
.
Part I: Android Fundamentals
In Part I, you’ll get an introduction to Android, become familiar with the Android
SDK and tools, install the development tools, and write your first Android application.
Part I also introduces the design principles necessary to write Android applications,
including how Android applications are structured and configured, as well as how to
incorporate application resources such as strings, graphics, and user interface compo-
nents into your projects.
.
Part II: Building an Application Framework
In Part II, you’ll begin developing an application framework that will serve as pri-
mary teaching-tool for the rest of the book. You’ll start by developing an animated
splash screen, followed by screens for main menu, settings, help, and scores. You’ll
2
Introduction
ptg
learn basic user interface design principles, how to collect input from the user, and
how to display dialogs to the user. Finally, you’ll implement the core application logic
examination of all the features of the Android platform, we recommend our other, more
advanced Android book, Android Wireless Application Development, which is part of the
Addison-Wesley Developer’s Library series.
What Is (and Isn’t) in This Book
3
ptg
What Development Environment Is Used?
The code in this book was written using the following development environments:
.
Windows 7 and Mac OS X 10.6
.
Eclipse Java IDE Version 3.5 (Galileo)
.
Eclipse JDT plug-in and Web Tools Platform (WTP)
.
Sun Java SE Development Kit (JDK) 6 Update 18
.
Android SDK Version 2.1 (Primary target, developed and tested on a variety of SDK
versions)
.
Various Android handsets (Android SDK 1.6, 2.0.1, and 2.1)
What Conventions Are Used in This Book?
This book presents several types of sidebars for special kinds of information:
.
Did You Know? messages provide useful information or hints related to the current
text.
.
By the Way messages provide additional information that might be interesting or rel-
evant.
screen, we walk you step-by-step through the implementation of the first button but
leave the implementation of the other two buttons as an exercise for you. In a later
chapter on a different topic, we might simply ask you to implement some buttons on
another screen.
.
Where we tell you to navigate through menu options, we separate options using com-
mas. For example, if we told you to open a new document, we’d say “Select File, New
Document.”
What Conventions Are Used in This Book?
5
ptg
This page intentionally left blank
ptg
HOUR 1
Getting Started with Android
What You’ll Learn in This Hour:
. A brief history of the Android platform
. Familiarizing yourself with Eclipse
. Creating Android projects
. Running and debugging applications
Android is the first complete, open, and free mobile platform. Developers enjoy a compre-
hensive software development kit, with ample tools for developing powerful, feature-rich
applications. The platform is open source, relying on tried-and-true open standards devel-
opers will be familiar with. And best of all, there are no costly barriers to entry for devel-
opers: no required fees. (A modest fee is required to publish on third-party distribution
mechanisms such as the Android Market.) Android developers have numerous options for
distributing and commercializing their applications.
Introducing Android
Android developer program at http://developer.android.com. This developer website
is your go-to site for downloading the Android SDK, getting the latest platform docu-
mentation, and browsing the Android developer forums. Google also runs the most
popular service for selling Android applications to end users: the Android Market.
The Android mascot is the little green robot shown in Figure 1.1.
FIGURE 1.1
The Android
mascot.