Android Programming Tutorials

447 Pages · 6.15 mb ·

Mark Murphy

Programming Tutorials

Table of contents

- Android Programming Tutorials
(Page 2)
- Welcome to the Book! (Page 20)
- Prerequisites (Page 20)
- Using the Tutorials (Page 21)
- Warescription (Page 23)
- What's New (Page 23)
- About the "Further Reading" Sections (Page 24)
- Errata and Book Bug Bounty (Page 24)
- Source Code License (Page 25)
- Creative Commons and the Four-to-Free (42F) Guarantee (Page 25)
- Lifecycle of a CommonsWare Book (Page 26)
- Roster of Tutorials (Page 27)
- Your First Android Project (Page 34)
- Step #1: Create the New Project (Page 34)
- Step #1: Eclipse (Page 35)
- Step #2: Command Line (Page 38)
- Step #2: Build, Install, and Run the Application in Your Emulator or Device (Page 39)
- Step #1: Eclipse (Page 39)
- Step #2: Command Line (Page 40)
- A Simple Form (Page 44)
- Step-By-Step Instructions (Page 44)
- Step #1: Generate the Application Skeleton (Page 44)
- Step #2: Modify the Layout (Page 45)
- Step #3: Compile and Install the Application (Page 46)
- Step #4: Run the Application in the Emulator (Page 47)
- Step #5: Create a Model Class (Page 47)
- Step #6: Save the Form to the Model (Page 48)
- Extra Credit (Page 49)
- Further Reading (Page 50)
- A Fancier Form (Page 52)
- Step-By-Step Instructions (Page 52)
- Step #1: Switch to a TableLayout (Page 52)
- Step #2: Add a RadioGroup (Page 54)
- Step #3: Update the Model (Page 56)
- Step #4: Save the Type to the Model (Page 57)
- Extra Credit (Page 59)
- Further Reading (Page 60)
- Adding a List (Page 62)
- Step-By-Step Instructions (Page 62)
- Step #1: Hold a List of Restaurants (Page 62)
- Step #2: Save Adds to List (Page 63)
- Step #3: Implement toString() (Page 64)
- Step #4: Add a ListView Widget (Page 64)
- Step #5: Build and Attach the Adapter (Page 66)
- Extra Credit (Page 68)
- Further Reading (Page 69)
- Making Our List Be Fancy (Page 70)
- Step-By-Step Instructions (Page 70)
- Step #1: Create a Stub Custom Adapter (Page 71)
- Step #2: Design Our Row (Page 71)
- Step #3: Override getView(): The Simple Way (Page 73)
- Step #4: Create a RestaurantHolder (Page 74)
- Step #5: Recycle Rows via RestaurantHolder (Page 75)
- Extra Credit (Page 78)
- Further Reading (Page 78)
- Splitting the Tab (Page 80)
- Step-By-Step Instructions (Page 80)
- Step #1: Rework the Layout (Page 80)
- Step #2: Wire In the Tabs (Page 82)
- Step #3: Get Control On List Events (Page 84)
- Step #4: Update Our Restaurant Form On Clicks (Page 84)
- Step #5: Switch Tabs On Clicks (Page 85)
- Extra Credit (Page 89)
- Further Reading (Page 89)
- Menus and Messages (Page 90)
- Step-By-Step Instructions (Page 90)
- Step #1: Add Notes to the Restaurant (Page 90)
- Step #2: Add Notes to the Detail Form (Page 91)
- Step #3: Define the Option Menu (Page 93)
- Step #4: Show the Notes as a Toast (Page 94)
- Extra Credit (Page 100)
- Further Reading (Page 100)
- Sitting in the Background (Page 102)
- Step-By-Step Instructions (Page 102)
- Step #1: Initialize the Progress Bar (Page 102)
- Step #2: Create the Work Method (Page 103)
- Step #3: Fork the Thread from the Menu (Page 104)
- Step #4: Manage the Progress Bar (Page 106)
- Extra Credit (Page 108)
- Further Reading (Page 109)
- Life and Times (Page 110)
- Step-By-Step Instructions (Page 110)
- Step #1: Lengthen the Background Work (Page 110)
- Step #2: Pause in onPause() (Page 111)
- Step #3: Resume in onResume() (Page 112)
- Extra Credit (Page 119)
- Further Reading (Page 119)
- A Few Good Resources (Page 120)
- Step-By-Step Instructions (Page 120)
- Step #1: Review our Current Resources (Page 120)
- Step #2: Create a Landscape Layout (Page 121)
- Extra Credit (Page 124)
- Further Reading (Page 125)
- The Restaurant Store (Page 126)
- Step-By-Step Instructions (Page 126)
- Step #1: Create a Stub SQLiteOpenHelper (Page 126)
- Step #2: Manage our Schema (Page 127)
- Step #3: Remove Extraneous Code from LunchList (Page 128)
- Step #4: Get Access to the Helper (Page 128)
- Step #5: Save a Restaurant to the Database (Page 129)
- Step #6: Get the List of Restaurants from the Database (Page 130)
- Step #7: Change our Adapter and Wrapper (Page 132)
- Step #8: Clean Up Lingering ArrayList References (Page 134)
- Step #9: Refresh Our List (Page 135)
- Extra Credit (Page 140)
- Further Reading (Page 140)
- Getting More Active (Page 142)
- Step-By-Step Instructions (Page 142)
- Step #1: Create a Stub Activity (Page 142)
- Step #2: Launch the Stub Activity on List Click (Page 143)
- Step #3: Move the Detail Form UI (Page 144)
- Step #4: Clean Up the Original UI (Page 147)
- Step #5: Pass the Restaurant _ID (Page 148)
- Step #6: Load the Restaurant Into the Form (Page 149)
- Step #7: Add an "Add" Menu Option (Page 151)
- Step #8: Detail Form Supports Add and Edit (Page 152)
- Extra Credit (Page 166)
- Further Reading (Page 166)
- What's Your Preference? (Page 168)
- Step-By-Step Instructions (Page 168)
- Step #1: Define the Preference XML (Page 168)
- Step #2: Create the Preference Activity (Page 169)
- Step #3: Connect the Preference Activity to the Option Menu (Page 170)
- Step #4: Apply the Sort Order on Startup (Page 174)
- Step #5: Listen for Preference Changes (Page 175)
- Step #6: Re-Apply the Sort Order on Changes (Page 176)
- Extra Credit (Page 177)
- Further Reading (Page 178)
- Turn, Turn, Turn (Page 180)
- Step-By-Step Instructions (Page 180)
- Step #1: Add a Stub onSaveInstanceState() (Page 180)
- Step #2: Pour the Form Into the Bundle (Page 181)
- Step #3: Repopulate the Form (Page 181)
- Step #4: Fix Up the Landscape Detail Form (Page 181)
- Extra Credit (Page 183)
- Further Reading (Page 184)
- Raising (Something Like) a Tweet (Page 188)
- Step-By-Step Instructions (Page 188)
- Step #1: Set Up an Identi.ca Account (Page 188)
- Step #2: Create a Stub Application and Activity (Page 189)
- Step #3: Create a Layout (Page 189)
- Step #4: Listen for Send Actions (Page 191)
- Step #5: Make the Status Post Request (Page 192)
- Extra Credit (Page 195)
- Further Reading (Page 196)
- Opening a JAR (Page 198)
- Step-By-Step Instructions (Page 198)
- Step #1: Obtain the JTwitter JAR (Page 198)
- Step #2: Switch from HttpClient to JTwitter (Page 199)
- Step #3: Create Preferences for Account Information (Page 200)
- Step #4: Use Account Information from Preferences (Page 203)
- Extra Credit (Page 207)
- Further Reading (Page 208)
- Listening To Your Friends (Page 210)
- Step-By-Step Instructions (Page 210)
- Step #1: Create a Service Stub (Page 210)
- Step #2: Set Up a Background Thread (Page 211)
- Step #3: Poll Your Friends (Page 212)
- Step #4: Find New Statuses (Page 213)
- Step #5: Set up the Public API (Page 214)
- Extra Credit (Page 215)
- Further Reading (Page 215)
- No, Really Listening To Your Friends (Page 216)
- Step-By-Step Instructions (Page 216)
- Step #1: Define the Callback (Page 216)
- Step #2: Enable Callbacks in the Service (Page 217)
- Step #3: Manage the Service and Register the Account (Page 219)
- Step #4: Display the Timeline (Page 221)
- Extra Credit (Page 233)
- Further Reading (Page 234)
- Your Friends Seem Remote (Page 236)
- Step-By-Step Instructions (Page 236)
- Step #1: Back Up or Branch Your Project (Page 236)
- Step #2: Create a Fresh Project (Page 237)
- Step #3: Move the Service to the New Project (Page 237)
- Step #4: Implement and Copy the AIDL (Page 238)
- Step #5: Implement the Client Side (Page 239)
- Step #6: Implement the Service Side (Page 241)
- Step #7: Restore Your Project (Page 250)
- Extra Credit (Page 250)
- Further Reading (Page 250)
- A Subtle Notification (Page 252)
- Step-By-Step Instructions (Page 252)
- Step #1: Pick a Word and Icon (Page 252)
- Step #2: Raise the Notification (Page 253)
- Step #3: Watch for the Keyword (Page 254)
- Step #4: Clearing the Notification (Page 256)
- Extra Credit (Page 258)
- Further Reading (Page 258)
- Posts On Location (Page 260)
- Step-By-Step Instructions (Page 260)
- Step #1: Get the LocationManager (Page 260)
- Step #2: Register for Location Updates (Page 261)
- Step #3: Add "Insert Location" Menu (Page 262)
- Step #4: Insert the Last Known Location (Page 263)
- Extra Credit (Page 265)
- Further Reading (Page 266)
- Here a Post, There a Post (Page 268)
- Step-By-Step Instructions (Page 268)
- Step #1: Register for a Map API Key (Page 268)
- Step #2: Create a Basic MapActivity (Page 269)
- Step #3: Launch the Map on Location-Bearing Status Click (Page 270)
- Step #4: Show the Location Via a Pin (Page 273)
- Extra Credit (Page 276)
- Further Reading (Page 276)
- Media (Page 278)
- Step-By-Step Instructions (Page 278)
- Step #1: Obtain and Install a Video Clip (Page 278)
- Step #2: Create the Stub Helpcast Activity (Page 279)
- Step #3: Launch the Helpcast from the Menu (Page 280)
- Extra Credit (Page 282)
- Further Reading (Page 283)
- Browsing Some Posts (Page 284)
- Step-By-Step Instructions (Page 284)
- Step #1: Add Auto-Linking (Page 284)
- Step #2: Draft and Package the Help HTML (Page 285)
- Step #3: Create a Help Activity (Page 285)
- Step #4: Splice In the Help Activity (Page 286)
- Extra Credit (Page 288)
- Further Reading (Page 289)
- High-Priced Help (Page 290)
- Step-By-Step Instructions (Page 290)
- Step #1: Enable Javascript (Page 290)
- Step #2: Create the Java Object to Inject (Page 291)
- Step #3: Inject the Java Object (Page 291)
- Step #4: Leverage the Java Object from Javascript (Page 292)
- Extra Credit (Page 293)
- Further Reading (Page 293)
- Now Your Friends Seem Animated (Page 296)
- Step-By-Step Instructions (Page 296)
- Step #1: Set Up the Option Menu (Page 296)
- Step #2: Show and Hide the Status Entry Widgets (Page 299)
- Step #3: Fading In and Out (Page 301)
- Extra Credit (Page 303)
- Further Reading (Page 303)
- Messages From The Great Beyond (Page 304)
- Step-By-Step Instructions (Page 304)
- Step #1: Broadcast the Intent (Page 304)
- Step #2: Catch and Use the Intent (Page 306)
- Extra Credit (Page 307)
- Further Reading (Page 307)
- Contacting Our Friends (Page 308)
- Step-By-Step Instructions (Page 308)
- Step #1: Fake the Contact Data (Page 308)
- Step #2: Design the Highlight (Page 313)
- Step #3: Find and Highlight Matching Contacts (Page 314)
- Extra Credit (Page 315)
- Further Reading (Page 316)
- Android Would Like Your Attention (Page 318)
- Step-By-Step Instructions (Page 318)
- Step #1: Track the Battery State (Page 318)
- Step #2: Use the Battery State (Page 320)
- Extra Credit (Page 321)
- Further Reading (Page 321)
- Now, Your Friends Are Alarmed (Page 322)
- Step-By-Step Instructions (Page 322)
- Step #1: Import a Reusable Component (Page 322)
- Step #2: Create the Alarm BroadcastReceiver (Page 323)
- Step #3: Doing the Work (Page 324)
- Extra Credit (Page 331)
- Further Reading (Page 331)
- Searching For Food (Page 332)
- Step-By-Step Instructions (Page 332)
- Step #1: Have the List Conduct the Search (Page 332)
- Step #2: Integrate the Search in the Application (Page 335)
- Extra Credit (Page 337)
- Further Reading (Page 337)
- Look Inside Yourself (Page 338)
- Step-By-Step Instructions (Page 338)
- Step #1: Create a Stub Project (Page 338)
- Step #2: Create a Layout (Page 339)
- Step #3: Find the Correct Contact Uri (Page 340)
- Step #4: Attach the Button to the Contacts (Page 340)
- Step #5: Populate the List (Page 341)
- Extra Credit (Page 345)
- Further Reading (Page 346)
- A Restaurant In Your Own Home (Page 348)
- Step-By-Step Instructions (Page 348)
- Step #1: Find An App Widget Background and Icon (Page 348)
- Step #2: Design the App Widget Layout (Page 349)
- Step #3: Add an (Empty) AppWidgetProvider (Page 349)
- Step #4: Add the Widget Metadata (Page 350)
- Step #5: Update the Manifest (Page 351)
- Step #6: Show a Random Restaurant (Page 354)
- Extra Credit (Page 357)
- Further Reading (Page 357)
- More Home Cooking (Page 358)
- Step-By-Step Instructions (Page 358)
- Step #1: Find a Button Graphic (Page 358)
- Step #2: Add the Button to the Layout (Page 359)
- Step #3: Migrate Update Logic to an IntentService (Page 360)
- Step #4: Get Control on Button Clicks (Page 363)
- Step #5: Get Control on Name Clicks (Page 364)
- Extra Credit (Page 366)
- Further Reading (Page 366)
- Take a Monkey to Lunch (Page 368)
- Step-By-Step Instructions (Page 368)
- Step #1: Prep LunchList (Page 368)
- Step #2: Run the Monkey (Page 368)
- Extra Credit (Page 369)
- Further Reading (Page 370)
- Asking Permission to Place a Call (Page 372)
- Step-By-Step Instructions (Page 372)
- Step #1: Add a Phone Number to the Database Schema (Page 372)
- Step #2: Intelligently Handle Database Updates (Page 373)
- Step #3: Add Phone Number Support to the Rest of the Helper (Page 373)
- Step #4: Collect the Phone Number on the Detail Form (Page 375)
- Step #5: Ask for Permission to Make Calls (Page 376)
- Step #6: Dial the Number (Page 377)
- Step #7: Make the Call (Page 379)
- Extra Credit (Page 380)
- Further Reading (Page 380)
- Photographic Memory (Page 382)
- Step-By-Step Instructions (Page 382)
- Step #1: Adjust the Manifest (Page 382)
- Step #2: Create the Photographer Layout (Page 384)
- Step #3: Create the Photographer Class (Page 385)
- Step #4: Tie In the Photographer Class (Page 387)
- Extra Credit (Page 388)
- Further Reading (Page 388)
- Sensing a Disturbance (Page 390)
- Step-By-Step Instructions (Page 390)
- Step #1: Implement a Shaker (Page 390)
- Step #2: Hook Into the Shaker (Page 393)
- Step #3: Make a Random Selection on a Shake (Page 394)
- Extra Credit (Page 395)
- Further Reading (Page 395)
- Getting the Word Out (Page 396)
- Step-By-Step Instructions (Page 396)
- Step #1: Add a "Send SMS" Option Menu (Page 397)
- Step #2: Find Contacts' Mobile Numbers (Page 398)
- Step #3: Pick a Person (Page 399)
- Step #4: Send the Message (Page 399)
- Extra Credit (Page 402)
- Further Reading (Page 403)
- Seeking the Proper Level (Page 404)
- Step-By-Step Instructions (Page 404)
- Step #1: Define an Interface for Mobile Numbers (Page 404)
- Step #2: Implement the Interface: the New Way (Page 405)
- Step #3: Implement the Interface: the Old Way (Page 406)
- Step #4: Choose and Use the Bridge (Page 407)
- Extra Credit (Page 408)
- Further Reading (Page 408)
- Appendix A:
How To Get Started (Page 410)
- Java (Page 410)
- Step #1: Install the JDK (Page 411)
- Step #2: Learn Java (Page 411)
- Install the Android SDK (Page 412)
- Step #1: Install the Base Tools (Page 412)
- Step #2: Install the SDKs and Add-Ons (Page 412)
- Install the ADT for Eclipse (Page 416)
- Install Apache Ant (Page 418)
- Set Up the Emulator (Page 419)
- Set Up the Device (Page 426)
- Step #1: Windows (Page 427)
- Step #2: OS X and Linux (Page 428)
- Appendix B:
Coping with Eclipse (Page 430)
- How to Import a Non-Eclipse Project (Page 430)
- How to Get To DDMS (Page 435)
- How to Create an Emulator (Page 437)
- How to Run a Project (Page 438)
- How Not to Run Your Project (Page 439)
- How to Get Past Eclipse (Page 439)