0110234 5678
!
"#
$%&
'()*+,-)(.-
/01
5
Android Programming Tutorials
by Mark L. Murphy
Subscribe to updates at http://commonsware.com
Special Creative Commons BY-NC-SA 3.0 License Edition
Android Programming Tutorials
by Mark L. Murphy
Copyright © 2009-2011 CommonsWare, LLC. All Rights Reserved.
Printed in the United States of America.
CommonsWare books may be purchased in printed (bulk) or digital form for educational or
business use. For more information, contact direct@commonsware.com.
Printing History:
Jan 2011: Version 3.1 ISBN: 978-0-9816780-4-7
The CommonsWare name and logo, “Busy Coder's Guide”, and related trade dress are
trademarks of CommonsWare, LLC.
All other trademarks referenced in this book are trademarks of their respective firms.
The publisher and author(s) assume no responsibility for errors or omissions or for damages
resulting from the use of the information contained herein.
Subscribe to updates at http://commonsware.com
Special Creative Commons BY-NC-SA 3.0 License Edition
Table of Contents
Welcome to the Warescription!.........................................................xvii
Preface..................................................................................................xix
Welcome to the Book!...............................................................................xix
Prerequisites...............................................................................................xix
Using the Tutorials.....................................................................................xx
Warescription...........................................................................................xxii
What's New...............................................................................................xxii
About the "Further Reading" Sections...................................................xxiii
Errata and Book Bug Bounty..................................................................xxiii
Source Code License................................................................................xxiv
Creative Commons and the Four-to-Free (42F) Guarantee.................xxiv
Lifecycle of a CommonsWare Book........................................................xxv
Roster of Tutorials...................................................................................xxvi
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
iii
Subscribe to updates at http://commonsware.com
Special Creative Commons BY-NC-SA 3.0 License Edition
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: Compile and Install the Application.....................................13
Step #4: Run the Application in the Emulator...................................14
Step #5: Create a Model Class..............................................................14
Step #6: Save the Form to the Model..................................................15
Extra Credit..................................................................................................16
Further Reading...........................................................................................17
A Fancier Form.......................................................................................19
Step-By-Step Instructions...........................................................................19
Step #1: Switch to a TableLayout.........................................................19
Step #2: Add a RadioGroup..................................................................21
Step #3: Update the Model..................................................................23
Step #4: Save the Type to the Model..................................................24
Extra Credit.................................................................................................26
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 List....................................................................30
Step #3: Implement toString().............................................................31
Step #4: Add a ListView Widget..........................................................31
Step #5: Build and Attach the Adapter...............................................33
iv
Subscribe to updates at http://commonsware.com
Special Creative Commons BY-NC-SA 3.0 License Edition