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 [email protected]. Printing History: Nov 2011:Version 4.1 ISBN: 978-0-9816780-7-8 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!............................................................xv Preface.................................................................................................xvii Welcome to the Book!..............................................................................xvii Prerequisites.............................................................................................xvii Getting Help............................................................................................xviii Using the Tutorials..................................................................................xviii This Book and Eclipse................................................................................xx Warescription............................................................................................xxi What's New................................................................................................xxi About the "Further Reading" Sections....................................................xxii Errata and Book Bug Bounty...................................................................xxii Source Code License...............................................................................xxiii Creative Commons and the Four-to-Free (42F) Guarantee.................xxiv Lifecycle of a CommonsWare Book.......................................................xxiv Roster of Tutorials....................................................................................xxv TUTORIAL 1: Your First Android Project................................................1 Step #1: Create the New Project...................................................................1 Step #1: Eclipse.......................................................................................2 Step #2: Command Line........................................................................6 iii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Step #2: Build, Install, and Run the Application in Your Emulator or Device............................................................................................................7 Step #1: Eclipse........................................................................................7 Step #2: Outside of Eclipse....................................................................8 TUTORIAL 2: A Simple Form.................................................................13 Step-By-Step Instructions...........................................................................13 Step #1: Generate the Application Skeleton........................................13 Step #2: Modify the Layout..................................................................14 Step #3: Support All Screen Sizes........................................................32 Step #4: Run the Application..............................................................36 Step #5: Create a Model Class.............................................................38 Step #6: Save the Form to the Model.................................................39 Extra Credit.................................................................................................40 Further Reading..........................................................................................40 TUTORIAL 3: A Fancier Form................................................................43 Step-By-Step Instructions..........................................................................43 Step #1: Switch to a TableLayout.........................................................43 Step #2: Add a RadioGroup.................................................................50 Step #3: Update the Model..................................................................55 Step #4: Save the Type to the Model..................................................56 Extra Credit.................................................................................................58 Further Reading..........................................................................................59 TUTORIAL 4: Adding a List...................................................................61 Step-By-Step Instructions...........................................................................61 Step #1: Hold a List of Restaurants......................................................61 Step #2: Save Adds to List....................................................................62 Step #3: Implement toString()............................................................63 iv Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Step #4: Add a ListView Widget.........................................................63 Step #5: Build and Attach the Adapter..............................................66 Extra Credit.................................................................................................68 Further Reading..........................................................................................69 TUTORIAL 5: Making A More Elaborate List........................................71 Step-By-Step Instructions...........................................................................71 Step #1: Create a Stub Custom Adapter..............................................72 Step #2: Design Our Row.....................................................................72 Step #3: Override getView(): The Simple Way..................................80 Step #4: Create a RestaurantHolder....................................................81 Step #5: Recycle Rows via RestaurantHolder....................................82 Extra Credit.................................................................................................85 Further Reading..........................................................................................85 TUTORIAL 6: Splitting the Tab.............................................................87 Step-By-Step Instructions..........................................................................87 Step #1: Rework the Layout.................................................................87 Step #2: Wire In the Tabs....................................................................89 Step #3: Get Control On List Events...................................................91 Step #4: Update Our Restaurant Form On Clicks.............................92 Step #5: Switch Tabs On Clicks...........................................................93 Extra Credit.................................................................................................96 Further Reading..........................................................................................97 TUTORIAL 7: Menus and Messages......................................................99 Step-By-Step Instructions..........................................................................99 Step #1: Add Notes to the Restaurant.................................................99 Step #2: Add Notes to the Detail Form.............................................100 Step #3: Define the Options Menu....................................................102 v Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Step #4: Support the Options Menu.................................................105 Step #5: Show the Notes as a Toast...................................................106 Step #6: Add a ScrollView...................................................................112 Extra Credit................................................................................................114 Further Reading.........................................................................................114 TUTORIAL 8: Sitting in the Background.............................................115 Step-By-Step Instructions..........................................................................115 Step #1: Initialize the Progress Bar.....................................................115 Step #2: Create the Work Method.....................................................116 Step #3: Fork the Thread from the Menu..........................................117 Step #4: Manage the Progress Bar......................................................119 Extra Credit.................................................................................................121 Further Reading.........................................................................................122 TUTORIAL 9: Life and Times...............................................................123 Step-By-Step Instructions.........................................................................123 Step #1: Lengthen the Background Work..........................................123 Step #2: Pause in onPause()...............................................................124 Step #3: Resume in onResume()........................................................125 Extra Credit................................................................................................132 Further Reading.........................................................................................132 TUTORIAL 10: A Few Good Resources.................................................133 Step-By-Step Instructions.........................................................................133 Step #1: Review our Current Resources.............................................133 Step #2: Create a Landscape Layout..................................................134 Extra Credit................................................................................................137 Further Reading.........................................................................................138 vi Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition TUTORIAL 11: The Restaurant Store....................................................139 Step-By-Step Instructions.........................................................................139 Step #1: Create a Stub SQLiteOpenHelper........................................139 Step #2: Manage our Schema.............................................................140 Step #3: Remove Extraneous Code from LunchList.........................141 Step #4: Get Access to the Helper.....................................................142 Step #5: Save a Restaurant to the Database......................................142 Step #6: Get the List of Restaurants from the Database.................144 Step #7: Change our Adapter and Wrapper......................................145 Step #8: Clean Up Lingering ArrayList References..........................147 Step #9: Refresh Our List...................................................................148 Extra Credit................................................................................................153 Further Reading.........................................................................................153 TUTORIAL 12: Getting More Active.....................................................155 Step-By-Step Instructions.........................................................................155 Step #1: Create a Stub Activity...........................................................155 Step #2: Launch the Stub Activity on List Click...............................156 Step #3: Move the Detail Form Layout.............................................158 Step #4: Move the Detail Form Code................................................159 Step #5: Clean Up the Original Layout..............................................161 Step #6: Clean Up the Original Code................................................163 Step #7: Pass the Restaurant _ID.......................................................164 Step #8: Load the Restaurant Into the Form....................................165 Step #9: Add an "Add" Menu Option................................................166 Step #10: Detail Form Supports Add and Edit..................................168 Extra Credit................................................................................................181 Further Reading.........................................................................................182 vii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition TUTORIAL 13: What's Your Preference?..............................................183 Step-By-Step Instructions.........................................................................183 Step #1: Define the Preference XML..................................................183 Step #2: Create the Preference Activity............................................189 Step #3: Connect the Preference Activity to the Option Menu.......191 Step #4: Apply the Sort Order on Startup........................................195 Step #5: Listen for Preference Changes............................................196 Step #6: Re-Apply the Sort Order on Changes.................................197 Extra Credit................................................................................................198 Further Reading........................................................................................199 TUTORIAL 14: Turn, Turn, Turn..........................................................201 Step-By-Step Instructions.........................................................................201 Step #1: Add a Stub onSaveInstanceState()......................................201 Step #2: Pour the Form Into the Bundle..........................................202 Step #3: Repopulate the Form...........................................................202 Step #4: Fix Up the Landscape Detail Form.....................................203 Extra Credit...............................................................................................204 Further Reading........................................................................................204 TUTORIAL 15: Feeding at Lunch.........................................................205 Step-By-Step Instructions........................................................................205 Step #1: Add a Feed URL to the Data Model....................................205 Step #2: Update the Detail Form......................................................209 Step #3: Add a Feed Options Menu Item..........................................213 Step #4: Add Permissions and Check Connectivity.........................215 Step #5: Fetch the Feed......................................................................220 Step #6: Install the RSS Library.........................................................223 Step #7: Fetch and Parse the Feed (For Real This Time)................224 viii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Step #8: Display the Feed Items........................................................226 Extra Credit...............................................................................................235 Further Reading........................................................................................236 TUTORIAL 16: Serving Up Lunch.........................................................237 Step-By-Step Instructions........................................................................237 Step #1: Create and Register a Stub IntentService...........................237 Step #2: Move Feed Fetching and Parsing to the Service...............239 Step #3: Send the Feed to the Activity.............................................240 Step #4: Display the Feed Items, Redux...........................................243 Extra Credit...............................................................................................248 Further Reading........................................................................................249 TUTORIAL 17: Locating Lunch.............................................................251 Step-By-Step Instructions.........................................................................251 Step #1: Add Latitude and Longitude to the Data Model...............252 Step #2: Save the Restaurant in onPause().......................................256 Step #3: Add a TextView and Options Menu Item for Location....258 Step #4: Update the Permissions......................................................265 Step #5: Find Our Location Using GPS............................................266 Step #6: Only Enable Options Menu Item If Saved........................268 Extra Credit...............................................................................................274 Further Reading........................................................................................275 TUTORIAL 18: Putting Lunch on the Map..........................................277 Step-By-Step Instructions........................................................................277 Step #1: Make Sure You Are Ready....................................................277 Step #2: Add an Options Menu Item for Map.................................279 Step #3: Create and Use a MapActivity.............................................281 Step #4: Create an ItemizedOverlay.................................................284 ix Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition