Table of Contents
Prerequisites xv
Using the Tutorials xvi
Warescription xviii
What's New xviii
About the "Further Reading" Sections xix
Errata and Book Bug Bounty xix
Source Code License xx
Creative Commons and the Four-to-Free (42F) Guarantee xxi
Lifecycle of a CommonsWare Book xxi
Roster of Tutorials xxii
Your First Android Project 1
Step #1: Create the New Project 1
Step #1: Eclipse 2
Step #2: Command Line 5
Step #2: Build, Install, and Run the Application in Your Emulator or Device 6
Step #1: Eclipse 6
Step #2: Command Line 7
A Simple Form 11
Step-By-Step Instructions 11
Step #1: Generate the Application Skeleton 11
Step #2: Modify the Layout 12
Step #3: Support All Screen Sizes 14
Step #4: Compile and Install the Application 15
Step #5: Run the Application in the Emulator 15
Step #6: Create a Model Class 16
Step #7: Save the Form to the Model 16
Extra Credit 18
Further Reading 18
A Fancier Form 19
Step-By-Step Instructions19
Step #1: Switch to a TableLayout 19
Step #2: Add a RadioGroup21
Step #3: Update the Model 23
Step #4: Save the Type to the Model 24
Extra Credit26
Further Reading 27
Adding a List 29
Step-By-Step Instructions 29
Step #1: Hold a List of Restaurants 29
Step #2: Save Adds to List30
Step #3: Implement toString() 31
Step #4: Add a ListView Widget 31
Step #5: Build and Attach the Adapter 33
Extra Credit 35
Further Reading36
Step-By-Step Instructions 37
Step #1: Create a Stub Custom Adapter38
Step #2: Design Our Row38
Step #3: Override getView(): The Simple Way40
Step #4: Create a RestaurantHolder41
Step #5: Recycle Rows via RestaurantHolder 42
Extra Credit 45
Further Reading45
Splitting the Tab47
Step-By-Step Instructions 47
Step #1: Rework the Layout 47
Step #2: Wire In the Tabs49
Step #3: Get Control On List Events51
Step #4: Update Our Restaurant Form On Clicks51
Step #5: Switch Tabs On Clicks52
Extra Credit56
Further Reading56
Menus and Messages 57
Step-By-Step Instructions 57
Step #1: Add Notes to the Restaurant 57
Step #2: Add Notes to the Detail Form 58
Step #3: Define the Option Menu 60
Step #4: Show the Notes as a Toast61
Extra Credit 67
Further Reading67
Sitting in the Background69
Step-By-Step Instructions69
Step #1: Initialize the Progress Bar 69
Step #2: Create the Work Method70
Step #3: Fork the Thread from the Menu 71
Step #4: Manage the Progress Bar73
Extra Credit 75
Further Reading76
Life and Times 77
Step-By-Step Instructions 77
Step #1: Lengthen the Background Work77
Step #2: Pause in onPause() 78
Step #3: Resume in onResume() 79
Extra Credit86
Further Reading86
A Few Good Resources 87
Step-By-Step Instructions 87
Step #1: Review our Current Resources 87
Step #2: Create a Landscape Layout 88
Extra Credit 91
Further Reading 92
The Restaurant Store 93
Step-By-Step Instructions 93
Step #1: Create a Stub SQLiteOpenHelper 93
Step #2: Manage our Schema 94
Step #3: Remove Extraneous Code from LunchList 95
Step #4: Get Access to the Helper 95
Step #5: Save a Restaurant to the Database 96
Step #6: Get the List of Restaurants from the Database 97
Step #7: Change our Adapter and Wrapper 98
Step #8: Clean Up Lingering ArrayList References 100
Step #9: Refresh Our List 101
Extra Credit 106
Further Reading 107
Getting More Active 109
Step-By-Step Instructions 109
Step #1: Create a Stub Activity 109
Step #2: Launch the Stub Activity on List Click 110
Step #3: Move the Detail Form UI 111
Step #4: Clean Up the Original UI 115
Step #5: Pass the Restaurant _ID 116
Step #6: Load the Restaurant Into the Form 117
Step #7: Add an "Add" Menu Option 118
Step #8: Detail Form Supports Add and Edit 119
Extra Credit 132
Further Reading 133
What's Your Preference? 135
Step-By-Step Instructions 135
Step #1: Define the Preference XML 135
Step #2: Create the Preference Activity 136
Step #3: Connect the Preference Activity to the Option Menu 137
Step #4: Apply the Sort Order on Startup 141
Step #5: Listen for Preference Changes 142
Step #6: Re-Apply the Sort Order on Changes 143
Extra Credit 144
Further Reading 145
Turn, Turn, Turn 147
Step-By-Step Instructions 147
Step #1: Add a Stub onSaveInstanceState() 147
Step #2: Pour the Form Into the Bundle 148
Step #3: Repopulate the Form 148
Step #4: Fix Up the Landscape Detail Form 148
Extra Credit 150
Further Reading 151
Feeding at Lunch 153
Step-By-Step Instructions 153
Step #1: Add a Feed URL to the Data Model 153
Step #2: Update the Detail Form 157
Step #3: Add a Feed Options Menu Item 161
Step #4: Add Permissions and Check Connectivity 162
Step #5: Install the RSS Library 166
Step #6: Fetch and Parse the Feed 167
Step #7: Display the Feed Items 170
Extra Credit 179
Further Reading 180
Serving Up Lunch 181
Step-By-Step Instructions 181
Step #1: Create an Register a Stub IntentService 181
Step #2: Move Feed Fetching and Parsing to the Service 183
Step #3: Send the Feed to the Activity 184
Step #4: Display the Feed Items, Redux 186
Extra Credit 192
Further Reading 193
Locating Lunch 195
Step-By-Step Instructions 195
Step #1: Add Latitude and Longitude to the Data Model 195
Step #2: Save the Restaurant in onPause() 200
Step #3: Add a TextView and Options Menu Item for Location 201
Step #4: Update the Permissions 205
Step #5: Find Our Location Using GPS 206
Step #6: Only Enable Options Menu Item If Saved 209
Extra Credit 215
Further Reading 216
Putting Lunch on the Map 217
Step-By-Step Instructions 217
Step #1: Add an Options Menu Item for Map 218
Step #2: Create and Use a MapActivity 218
Step #3: Create an ItemizedOverlay 221
Step #4: Handle Marker Taps 230
Extra Credit 235
Further Reading 236
Step-By-Step Instructions 237
Step #1: Create a TimePreference 238
Step #2: Collect Alarm Preferences 241
Step #3: Set Up a Boot-Time Receiver 242
Step #4: Manage Preference Changes 244
Step #5: Display the Alarm 251
Extra Credit 257
Further Reading 258
More Subtle Lunch Alarms259
Step-By-Step Instructions 259
Step #1: Collect Alarm Style Preference 260
Step #2: Display the Alarm, Redux 260
Extra Credit 268
Further Reading 268
How To Get Started 269
Java 270
Step #1: Install the JDK 270
Step #2: Learn Java 270
Install the Android SDK 271
Step #1: Install the Base Tools 271
Step #2: Install the SDKs and Add-Ons 272
Install the ADT for Eclipse 276
Install Apache Ant 278
Set Up the Emulator 279
Set Up the Device 286
Step #1: Windows 287
Step #2: OS X and Linux 288
Coping with Eclipse 291
How to Import a Non-Eclipse Project 291
How to Get To DDMS 296
How to Create an Emulator 298
How to Run a Project 299
How Not to Run Your Project 300
How to Get Past Eclipse 300
Welcome to the Warescription!
We hope you enjoy this ebook and its updates – subscribe to the Warescription newsletter on the Warescription site to learn when new editions of this book, or other books, are available.
All editions of CommonsWare titles, print and ebook, follow a softwarestyle numbering system. Major releases (1.0, 2.0, etc.) are available in both print and ebook; minor releases (0.1, 0.9, etc.) are available in ebook form for Warescription subscribers only. Releases ending in .9 are "release candidates" for the next major release, lacking perhaps an index but otherwise being complete.
Each Warescription ebook is licensed for the exclusive use of its subscriber and is tagged with the subscriber's name. We ask that you not distribute these books. If you work for a firm and wish to have several employees have access, enterprise Warescriptions are available.
Also, bear in mind that eventually this edition of this title will be released under a Creative Commons license – more on this in the preface.
Welcome to the Book!
If you come to this book after having read its companion volumes, The Busy Coder's Guide to Android Development and The Busy Coder's Guide to Advanced Android Development, thanks for sticking with the series!
CommonsWare aims to have the most comprehensive set of Android development resources (outside of the Open Handset Alliance itself), and we appreciate your interest.
This book is a collection of tutorials, walking you through developing Android applications, from the simplest "Hello, world!" to applications using many advanced Android APIs. Since this book only supplies tutorials, you will want something beyond it as a reference guide. That could be simply the Android SDK documentation, available with your SDK installation or online. It could be the other books in the CommonsWare Android series. Or, it could be another Android book – a list of currently-available Android books can be found on the Android Programming knol. What you do not want to do is attempt to learn all of Android solely from these tutorials, as they will demonstrate the breadth of the Android API but not its depth.
Also, the tutorials themselves have varying depth. Early on, there is more "hand-holding" to explain every bit of what needs to be done (e.g., classes to import). As the tutorials progress, some of the simpler Java bookkeeping steps are left out of the instructions – such as exhaustive lists of import statements – so the tutorials can focus on the Android aspects of the code.
You can find out when new releases of this book are available via:
• The cw-android Google Group, which is also a great place to ask
questions about the book and its examples
• The commonsguyT witter feed
• The CommonsBlog
• The Warescription newsletter, which you can subscribe to off of your Warescription page