Prepared exclusively for Trieu Nguyen
What readers are saying about Hello, Android
This is a most excellent book: very well written, easy to read, and fun.
In addition, any of Android’s quirks are explained along with just the
right amount of detail to ensure quality programming principles are
followed.
Anthony Stevens
Founder and CTO, PocketJourney and Top 20 Winner of
Google Android Competition
Ed Burnette covers an impressive amount of ground in a nicely com-
pact book while retaining the popular Pragmatic style. For the mate-
rial on 2D and 3D graphics alone, this is worthy of a spot in any
Android developer’s library.
Mark Murphy
Founder, CommonsWare
I remember when I first started to work with Android; it was like a
huge maze. With this book, the introduction would have been much
less painful. I am convinced that by reading this book new Android
programmers will have an easier start.
Gabor Paller
Senior Software Architect, OnRelay, Ltd.
Prepared exclusively for Trieu Nguyen
Download at Boykma.Com
Hello, Android
Introducing Google’s
Mobile Development Platform
Ed Burnette
The Pragmatic Bookshelf
Raleigh, North Carolina Dallas, Texas
Prepared exclusively for Trieu Nguyen
Download at Boykma.Com
Many of the designations used by manufacturers and sellers to distinguish their prod-
ucts are claimed as trademarks. Where those designations appear in this book, and The
Pragmatic Programmers, LLC was aware of a trademark claim, the designations have
been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The
Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g
device are trademarks of The Pragmatic Programmers, LLC.
Portions of the book’s cover are reproduced from work created and shared by Google and
used according to terms described in the Creative Commons 2.5 Attribution License. See
http://code.google.com/policies.html#restrictions for details.
Every precaution was taken in the preparation of this book. However, the publisher
assumes no responsibility for errors or omissions, or for damages that may result from
the use of information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team
create better software and have more fun. For more information, as well as the latest
Pragmatic titles, please visit us at
http://www.pragprog.com
Copyright © 2008 Ed Burnette.
All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmit-
ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or
otherwise, without the prior consent of the publisher.
Printed in the United States of America.
ISBN-10: 1-934356-17-4
ISBN-13: 978-1-934356-17-3
Printed on acid-free paper.
P1.5 printing, July 21, 2009
Version: 2009-7-21
Prepared exclusively for Trieu Nguyen
Download at Boykma.Com
Contents
Acknowledgments
Changes (Cupcake Updates)
P1.5—July 21 . . . . . . . . . . . . . . . . . . . . . . . . . . . .
P1.4—July 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
P1.3—June 22 . . . . . . . . . . . . . . . . . . . . . . . . . . . .
P1.2—June 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . .
P1.1—May 26 . . . . . . . . . . . . . . . . . . . . . . . . . . . .
TODO in future releases . . . . . . . . . . . . . . . . . . . . . .
Preface
What Makes Android Special? . . . . . . . . . . . . . . . . . . .
Who Should Read This Book? . . . . . . . . . . . . . . . . . . .
What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . .
What’s New for Cupcake? . . . . . . . . . . . . . . . . . . . . .
Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . .
Fast-Forward >> . . . . . . . . . . . . . . . . . . . . . . . . . . .
I
Introducing Android
1 Quick Start
1.1
1.2
1.3
1.4
1.5
Installing the Tools . . . . . . . . . . . . . . . . . . . . .
Creating Your First Program . . . . . . . . . . . . . . . .
Running on the Emulator . . . . . . . . . . . . . . . . .
Running on a Real Phone . . . . . . . . . . . . . . . . .
Fast-Forward >> . . . . . . . . . . . . . . . . . . . . . . .
2 Key Concepts
2.1
2.2
2.3
2.4
The Big Picture . . . . . . . . . . . . . . . . . . . . . . .
It’s Alive!
. . . . . . . . . . . . . . . . . . . . . . . . . . .
Building Blocks . . . . . . . . . . . . . . . . . . . . . . .
Using Resources . . . . . . . . . . . . . . . . . . . . . . .
10
11
11
11
12
12
12
13
14
14
15
16
16
17
17
19
20
20
24
24
29
29
30
30
35
39
40
Prepared exclusively for Trieu Nguyen
Download at Boykma.Com
2.5
2.6
Safe and Secure . . . . . . . . . . . . . . . . . . . . . . .
Fast-Forward >> . . . . . . . . . . . . . . . . . . . . . . .
41
42
CONTENTS
6
Prepared exclusively for Trieu Nguyen
this copy is (P1.5 printing, July 21, 2009)
Report erratum
Download at Boykma.Com
CONTENTS
7
II Android Basics
3 Designing the User Interface
Introducing the Sudoku Example . . . . . . . . . . . . .
3.1
Designing by Declaration . . . . . . . . . . . . . . . . . .
3.2
Creating the Opening Screen . . . . . . . . . . . . . . .
3.3
Using Alternate Resources . . . . . . . . . . . . . . . . .
3.4
Implementing an About Box . . . . . . . . . . . . . . . .
3.5
Applying a Theme . . . . . . . . . . . . . . . . . . . . . .
3.6
Adding a Menu . . . . . . . . . . . . . . . . . . . . . . .
3.7
Adding Settings . . . . . . . . . . . . . . . . . . . . . . .
3.8
3.9
Starting a New Game . . . . . . . . . . . . . . . . . . . .
3.10 Debugging with Log Messages . . . . . . . . . . . . . . .
3.11 Debugging with the Debugger . . . . . . . . . . . . . . .
3.12 Exiting the Game . . . . . . . . . . . . . . . . . . . . . .
3.13 Fast-Forward >> . . . . . . . . . . . . . . . . . . . . . . .
43
44
44
45
46
54
58
62
63
66
68
70
71
71
72
4 Exploring 2D Graphics
Learning the Basics . . . . . . . . . . . . . . . . . . . . .
Adding Graphics to Sudoku . . . . . . . . . . . . . . . .
Handling Input
. . . . . . . . . . . . . . . . . . . . . . .
The Rest of the Story . . . . . . . . . . . . . . . . . . . .
73
73
4.1
78
4.2
87
4.3
93
4.4
4.5 Making More Improvements . . . . . . . . . . . . . . . . 102
4.6
Fast-Forward >> . . . . . . . . . . . . . . . . . . . . . . . 103
5 Multimedia
104
Playing Audio . . . . . . . . . . . . . . . . . . . . . . . . 104
Playing Video . . . . . . . . . . . . . . . . . . . . . . . . . 110
Adding Sounds to Sudoku . . . . . . . . . . . . . . . . . 115
Fast-Forward >> . . . . . . . . . . . . . . . . . . . . . . . 118
5.1
5.2
5.3
5.4
6.1
6.2
6.3
6.4
6.5
6.6
6 Storing Local Data
119
Adding Options to Sudoku . . . . . . . . . . . . . . . . . 119
Continuing an Old Game . . . . . . . . . . . . . . . . . . 121
Remembering the Current Position . . . . . . . . . . . . 123
Accessing the Internal File System . . . . . . . . . . . . 125
Accessing SD Cards . . . . . . . . . . . . . . . . . . . . . 126
Fast-Forward >> . . . . . . . . . . . . . . . . . . . . . . . 127
Prepared exclusively for Trieu Nguyen
this copy is (P1.5 printing, July 21, 2009)
Report erratum
Download at Boykma.Com
III Beyond the Basics
128
CONTENTS
8
7 The Connected World
Browsing by Intent
129
7.1
. . . . . . . . . . . . . . . . . . . . . 130
7.2 Web with a View . . . . . . . . . . . . . . . . . . . . . . . 134
From JavaScript to Java and Back . . . . . . . . . . . . 139
7.3
7.4
Using Web Services . . . . . . . . . . . . . . . . . . . . . 146
Fast-Forward >> . . . . . . . . . . . . . . . . . . . . . . . 156
7.5
8 Locating and Sensing
158
Location, Location, Location . . . . . . . . . . . . . . . . 158
Set Sensors to Maximum . . . . . . . . . . . . . . . . . . 164
Bird’s-Eye View . . . . . . . . . . . . . . . . . . . . . . . 168
Fast-Forward >> . . . . . . . . . . . . . . . . . . . . . . . 173
8.1
8.2
8.3
8.4
9.1
9.2
9.3
9.4
9.5
9.6
9.7
9 Putting SQL to Work
175
Introducing SQLite . . . . . . . . . . . . . . . . . . . . . 175
SQL 101 . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Hello, Database . . . . . . . . . . . . . . . . . . . . . . . 178
Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . 186
Using a ContentProvider . . . . . . . . . . . . . . . . . . 189
Implementing a ContentProvider . . . . . . . . . . . . . 192
Fast-Forward >> . . . . . . . . . . . . . . . . . . . . . . . 193
10 3D Graphics in OpenGL
195
10.1 Understanding 3D Graphics . . . . . . . . . . . . . . . . 195
10.2 Introducing OpenGL . . . . . . . . . . . . . . . . . . . . 196
10.3 Building an OpenGL Program . . . . . . . . . . . . . . . 197
. . . . . . . . . . . . . . . . . . . . . 199
10.4 Managing Threads
10.5 Building a Model
. . . . . . . . . . . . . . . . . . . . . . 205
10.6 Lights, Camera, ... . . . . . . . . . . . . . . . . . . . . . . 208
10.7 Action!
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
10.8 Applying Texture . . . . . . . . . . . . . . . . . . . . . . 213
10.9 Peekaboo . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
10.10 Fast-Forward >> . . . . . . . . . . . . . . . . . . . . . . . 217
Prepared exclusively for Trieu Nguyen
this copy is (P1.5 printing, July 21, 2009)
Report erratum
Download at Boykma.Com