ptg Lauren Darcey Shane Conder SamsTeachYourself Android ™ Application Development ptg 24 in Hours 800 East 96th Street, Indianapolis, Indiana 46240 USA Sams Teach Yourself Android™Application Development in 24 Hours Editor-in-Chief Mark Taub Copyright ©2010 Lauren Darcey and Shane Conder All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or Acquisitions Editor transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without Trina MacDonald written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of Development this book, the publisher and author assume no responsibility for errors or omissions. Nor is any Editor liability assumed for damages resulting from the use of the information contained herein. Michael Thurston Visible Earth images owned by NASA, http://visibleearth.nasa.gov/. Managing Editor ISBN-13: 978-0-321-67335-0 Kristy Hart ISBN-10: 0-321-67335-2 Library of Congress Cataloging-in-Publication Data Project Editor Betsy Harris Darcey, Lauren, 1977- Copy Editor Sams teach yourself Android application development in 24 hours / Lauren Darcey, Shane Kitty Wilson Conder. p. cm. — (Sams teach yourself in 24 Hours) Indexer Includes index. Erika Millen ISBN 978-0-321-67335-0 (pbk.) Proofreader 1. Application software—Development. 2. Android (Electronic resource) 3. Mobile computing. Sheri Cain I. Conder, Shane, 1975- II. Title. Technical Editor QA76.76.A65D26 2010 Jonathan Jackson 005.1—dc22 2010011663 Publishing Printed in the United States of America Coordinator First Printing June 2010 Olivia Basegio Trademarks Book Designer All terms mentioned in this book that are known to be trademarks or service marks have been Gary Adair ptg appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service Senior Compositor mark. Gloria Schurick Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or from the use of the programs accompanying it. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk pur- chases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 [email protected] For sales outside of the U.S., please contact International Sales [email protected] Contents at a Glance Introduction.............................................................................................................................................. 1 Part I: Android Fundamentals HOUR 1 Getting Started with Android .................................................................................................... 7 2 Mastering the Android Development Tools . ............................................................... 27 3 Building Android Applications ............................................................................................. 43 4 Managing Application Resources........................................................................................ 59 5 Configuring the Android Manifest File........................................................................... 77 6 Designing an Application Framework............................................................................. 95 Part II: Building an Application Framework HOUR 7 Implementing an Animated Splash Screen ............................................................. 113 ptg 8 Implementing the Main Menu Screen . ........................................................................ 127 9 Developing the Help and Scores Screens .................................................................... 143 10 Building Forms to Collect User Input............................................................................. 161 11 Using Dialogs to Collect User Input ............................................................................... 181 12 Adding Application Logic .................................................................................................... 197 Part III: Enhancing Your Application with Powerful Android Features HOUR 13 Working with Images and the Camera........................................................................ 217 14 Adding Support for Location-Based Services ........................................................... 233 15 Adding Network Support ........................................................................................................ 255 16 Adding More Network Support .......................................................................................... 277 17 Adding Social Features ............................................................................................................. 291 18 Creating a Home Screen App Widget ........................................................................... 305 Part IV: Adding Polish to Your Android Application HOUR 19 Internationalizing Your Application ............................................................................. 321 20 Developing for Different Devices ...................................................................................... 333 21 Diving Deeper into Android ................................................................................................. 347 22 Testing Android Applications............................................................................................... 367 iv Teach Yourself Android Application Development in 24 Hours Part V: Publishing Your Application HOUR 23 Getting Ready to Publish ........................................................................................................ 383 24 Publishing on the Android Market ................................................................................. 395 Part VI: Appendixes A Configuring Your Android Development Environment .................................. 409 B Eclipse IDE Tips and Tricks .................................................................................................... 415 C Supplementary Materials........................................................................................................ 423 ptg v Table of Contents Introduction 1 Part I: Android Fundamentals HOUR 1: Getting Started with Android 7 Introducing Android .......................................................................................................................................... 7 Familiarizing Yourself with Eclipse.......................................................................................................... 9 Running and Debugging Applications . .......................................................................................... 17 Summary ................................................................................................................................................................ 23 Q&A.............................................................................................................................................................................. 23 Workshop ................................................................................................................................................................ 24 HOUR 2: Mastering the Android Development Tools ptg 27 Using the Android Documentation ............................................................................................... 27 Debugging Applications with DDMS . .......................................................................................... 29 Working with the Android Emulator ............................................................................................... 35 Using Other Android Tools ........................................................................................................................ 38 Summary ................................................................................................................................................................ 39 Q&A.............................................................................................................................................................................. 40 Workshop ................................................................................................................................................................ 40 HOUR 3: Building Android Applications 43 Designing a Typical Android Application .................................................................................... 43 Using the Application Context . ............................................................................................................ 46 Working with Activities ............................................................................................................................... 47 Working with Intents....................................................................................................................................... 51 Working with Dialogs ................................................................................................................................... 53 Logging Application Information ...................................................................................................... 54 Summary ................................................................................................................................................................ 55 Q&A.............................................................................................................................................................................. 55 Workshop ................................................................................................................................................................ 56 vi Teach Yourself Android Application Development in 24 Hours HOUR 4: Managing Application Resources 59 Using Application and System Resources .................................................................................... 59 Working with Simple Resource Values ........................................................................................... 63 Working with Drawable Resources ...................................................................................................... 66 Working with Layouts ................................................................................................................................... 67 Working with Files .......................................................................................................................................... 71 Working with Other Types of Resources ........................................................................................ 73 Summary ................................................................................................................................................................ 73 Q&A.............................................................................................................................................................................. 74 Workshop ................................................................................................................................................................ 75 HOUR 5: Configuring the Android Manifest File 77 Exploring the Android Manifest File................................................................................................... 77 Configuring Basic Application Settings........................................................................................... 81 Defining Activities .......................................................................................................................................... 86 ptg Managing Application Permissions ................................................................................................... 88 Managing Other Application Settings ........................................................................................... 91 Summary ................................................................................................................................................................ 91 Q&A.............................................................................................................................................................................. 92 Workshop ................................................................................................................................................................ 93 HOUR 6: Designing an Application Framework 95 Designing an Android Trivia Game ................................................................................................... 95 Implementing an Application Prototype .................................................................................... 102 Running the Game Prototype . ............................................................................................................ 107 Summary................................................................................................................................................................ 109 Q&A ........................................................................................................................................................................... 110 Workshop ............................................................................................................................................................ 110 vii Contents Part II: Building an Application Framework HOUR 7: Implementing an Animated Splash Screen 113 Designing the Splash Screen ................................................................................................................. 113 Implementing the Splash Screen Layout .................................................................................... 114 Working with Animation ........................................................................................................................ 119 Summary................................................................................................................................................................ 123 Q&A ........................................................................................................................................................................... 124 Workshop ............................................................................................................................................................ 124 HOUR 8: Implementing the Main Menu Screen 127 Designing the Main Menu Screen ................................................................................................... 127 Implementing the Main Menu Screen Layout ...................................................................... 131 Working with the ListView Control................................................................................................... 134 Working with Other Menu Types ...................................................................................................... 138 ptg Summary................................................................................................................................................................ 141 Q&A ........................................................................................................................................................................... 141 Workshop ............................................................................................................................................................ 141 HOUR 9: Developing the Help and Scores Screens 143 Designing the Help Screen ..................................................................................................................... 144 Implementing the Help Screen Layout ........................................................................................ 145 Working with Files.......................................................................................................................................... 147 Designing the Scores Screen ................................................................................................................. 149 Implementing the Scores Screen Layout .................................................................................... 151 Designing a Screen with Tabs ............................................................................................................. 154 Working with XML ....................................................................................................................................... 156 Summary................................................................................................................................................................ 158 Q&A ........................................................................................................................................................................... 158 Workshop ............................................................................................................................................................ 159 viii Teach Yourself Android Application Development in 24 Hours HOUR 10: Building Forms to Collect User Input 161 Designing the Settings Screen . ............................................................................................................ 161 Implementing the Settings Screen Layout ................................................................................. 165 Using Common Form Controls .......................................................................................................... 167 Saving Form Data with SharedPreferences................................................................................. 175 Summary................................................................................................................................................................ 178 Q&A ........................................................................................................................................................................... 178 Workshop ............................................................................................................................................................ 179 HOUR 11: Using Dialogs to Collect User Input 181 Working with Activity Dialogs. ............................................................................................................ 181 Using DatePickerDialog ............................................................................................................................ 184 Working with Custom Dialogs. ............................................................................................................ 187 Summary................................................................................................................................................................ 194 Q&A ........................................................................................................................................................................... 194 ptg Workshop ............................................................................................................................................................ 194 HOUR 12: Adding Application Logic 197 Designing the Game Screen ................................................................................................................. 197 Implementing the Game Screen Layout .................................................................................... 200 Working with ViewSwitcher Controls . .......................................................................................... 203 Wiring Up Game Logic............................................................................................................................... 208 Summary................................................................................................................................................................ 214 Q&A ........................................................................................................................................................................... 215 Workshop ............................................................................................................................................................ 215 Part III: Enhancing Your Application with Powerful Android Features HOUR 13: Working with Images and the Camera 217 Designing the Avatar Feature . ............................................................................................................ 217 Adding an Avatar to the Settings Screen Layout .................................................................. 219 Working with ImageButton Controls . .......................................................................................... 221 Working with Image Media ................................................................................................................. 223 ix Contents Working with Bitmaps ............................................................................................................................... 228 Summary................................................................................................................................................................ 230 Q&A ........................................................................................................................................................................... 230 Workshop ............................................................................................................................................................ 231 HOUR 14: Adding Support for Location-Based Services 233 Designing the Favorite Place Feature ........................................................................................... 233 Implementing the Framework for the Favorite Place Feature .................................. 237 Using Location-Based Services ............................................................................................................. 240 Using Geocoding Services ........................................................................................................................ 246 Working with Maps ....................................................................................................................................... 248 Summary................................................................................................................................................................ 251 Q&A ........................................................................................................................................................................... 251 Workshop ............................................................................................................................................................ 252 HOUR 15: Adding Network Support 255 ptg Designing Network Applications ...................................................................................................... 255 Developing Network Applications ................................................................................................... 257 Accessing Network Services ..................................................................................................................... 260 Indicating Network Activity with Progress Bars .................................................................. 262 Running Tasks Asynchronously .......................................................................................................... 265 Downloading and Displaying Scores ........................................................................................... 267 Downloading and Parsing Question Batches ......................................................................... 271 Summary................................................................................................................................................................ 274 Q&A ........................................................................................................................................................................... 274 Workshop ............................................................................................................................................................ 274 HOUR 16: Adding More Network Support 277 Determining Data to Send to the Server .................................................................................... 277 Accessing Phone Status Information............................................................................................... 278 Uploading Data to a Remote Application Server ............................................................... 281 Summary................................................................................................................................................................ 289 Q&A ........................................................................................................................................................................... 289 Workshop ............................................................................................................................................................ 289