The Busy Coder's Guide to Android Development by Mark L. Murphy Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © 2008-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 3.7 ISBN: 978-0-9816780-0-9 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!........................................................xxiii Preface.................................................................................................xxv Welcome to the Book!..............................................................................xxv Warescription............................................................................................xxv Getting Help.............................................................................................xxvi Book Bug Bounty....................................................................................xxvii Source Code And Its License................................................................xxviii Creative Commons and the Four-to-Free (42F) Guarantee...............xxviii Acknowledgments...................................................................................xxix The Big Picture.........................................................................................1 What Androids Are Made Of.......................................................................3 Activities.................................................................................................3 Services...................................................................................................4 Content Providers..................................................................................4 Intents.....................................................................................................4 Stuff At Your Disposal..................................................................................5 Storage.....................................................................................................5 Network...................................................................................................5 Multimedia.............................................................................................5 iii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition GPS..........................................................................................................5 Phone Services........................................................................................6 The Big Picture...Of This Book....................................................................6 How To Get Started.................................................................................7 Hardware Requirements..............................................................................7 Java.................................................................................................................8 Install the JDK........................................................................................8 Learn Java...............................................................................................9 Install the Android SDK...............................................................................9 Install the Base Tools............................................................................10 Install the SDKs and Add-Ons.............................................................10 Install the ADT for Eclipse..........................................................................11 Install Apache Ant.......................................................................................13 Set Up the Emulator....................................................................................14 Set Up the Device........................................................................................21 Windows...............................................................................................22 OS X and Linux.....................................................................................23 Your First Android Project....................................................................25 Step #1: Create the New Project.................................................................25 Eclipse...................................................................................................25 Command Line.....................................................................................30 Step #2: Build, Install, and Run the Application in Your Emulator or Device...........................................................................................................31 Eclipse....................................................................................................31 Command Line.....................................................................................32 Examining Your First Project................................................................37 Project Structure.........................................................................................37 iv Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Root Contents.......................................................................................37 The Sweat Off Your Brow....................................................................38 And Now, The Rest of the Story..........................................................39 What You Get Out Of It......................................................................40 Inside Your Manifest..................................................................................40 In The Beginning, There Was the Root, And It Was Good...............41 An Application For Your Application.................................................42 A Bit About Eclipse................................................................................45 What the ADT Gives You...........................................................................45 Coping with Eclipse....................................................................................46 How to Import a Non-Eclipse Project................................................46 How to Get To DDMS..........................................................................50 How to Create an Emulator.................................................................52 How to Run a Project...........................................................................53 How Not to Run Your Project.............................................................54 Alternative IDEs..........................................................................................55 More on the Tools.......................................................................................55 IDEs...And This Book.................................................................................56 Enhancing Your First Project................................................................57 Supporting Multiple Screens.....................................................................57 Specifying Versions....................................................................................58 Rewriting Your First Project.................................................................63 The Activity.................................................................................................63 Dissecting the Activity...............................................................................64 Building and Running the Activity...........................................................66 About the Remaining Examples................................................................67 v Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Using XML-Based Layouts....................................................................69 What Is an XML-Based Layout?................................................................69 Why Use XML-Based Layouts?.................................................................70 OK, So What Does It Look Like?...............................................................71 What's With the @ Signs?..........................................................................72 And We Attach These to the Java...How?.................................................72 The Rest of the Story..................................................................................73 Employing Basic Widgets......................................................................77 Assigning Labels.........................................................................................77 Button, Button, Who's Got the Button?...................................................78 Fleeting Images...........................................................................................79 Fields of Green. Or Other Colors...............................................................81 Just Another Box to Check.........................................................................83 Don't Like Checkboxes? How About Toggles?........................................86 Turn the Radio Up......................................................................................87 It's Quite a View.........................................................................................90 Padding.................................................................................................90 Other Useful Properties......................................................................90 Useful Methods.....................................................................................91 Colors.....................................................................................................91 Working with Containers......................................................................93 Thinking Linearly.......................................................................................94 Concepts and Properties.....................................................................94 Example................................................................................................99 The Box Model....................................................................................104 All Things Are Relative.............................................................................106 Concepts and Properties....................................................................106 vi Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Example...............................................................................................109 Overlap..................................................................................................111 Tabula Rasa.................................................................................................113 Concepts and Properties.....................................................................114 Example................................................................................................116 Scrollwork...................................................................................................117 The Input Method Framework.............................................................121 Keyboards, Hard and Soft..........................................................................121 Tailored To Your Needs............................................................................122 Tell Android Where It Can Go.................................................................126 Fitting In....................................................................................................129 Jane, Stop This Crazy Thing!....................................................................130 Using Selection Widgets......................................................................133 Clicks versus Selections.............................................................................133 Adapting to the Circumstances................................................................134 Using ArrayAdapter............................................................................135 Lists of Naughty and Nice.........................................................................136 Selection Modes..................................................................................138 Clicks versus Selections, Revisited....................................................140 Spin Control...............................................................................................141 Grid Your Lions (Or Something Like That...).........................................144 Fields: Now With 35% Less Typing!........................................................148 Galleries, Give Or Take The Art...............................................................152 Getting Fancy With Lists......................................................................155 Getting To First Base.................................................................................155 A Dynamic Presentation...........................................................................158 Inflating Rows Ourselves..........................................................................160 vii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition A Sidebar About Inflation..................................................................160 And Now, Back To Our Story............................................................162 Better. Stronger. Faster.............................................................................163 Using convertView..............................................................................163 Using the Holder Pattern...................................................................165 Interactive Rows........................................................................................167 Still More Widgets and Containers.....................................................175 Pick and Choose........................................................................................175 Time Keeps Flowing Like a River.............................................................180 Seeking Resolution....................................................................................182 Putting It On My Tab................................................................................183 The Pieces............................................................................................184 Wiring It Together..............................................................................185 Adding Them Up................................................................................188 Flipping Them Off.....................................................................................194 Getting In Somebody's Drawer...............................................................200 Other Good Stuff......................................................................................204 Embedding the WebKit Browser........................................................207 A Browser, Writ Small..............................................................................207 Loading It Up.............................................................................................210 Navigating the Waters...............................................................................211 Entertaining the Client.............................................................................212 Settings, Preferences, and Options (Oh, My!)........................................214 Applying Menus....................................................................................217 Flavors of Menu.........................................................................................217 Menus of Options......................................................................................218 Menus in Context.....................................................................................220 viii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition Taking a Peek.............................................................................................221 Yet More Inflation....................................................................................226 Menu XML Structure.........................................................................227 Menu Options and XML....................................................................228 Inflating the Menu.............................................................................229 In the Land of Menus and Honey............................................................231 Showing Pop-Up Messages..................................................................233 Raising Toasts............................................................................................233 Alert! Alert!................................................................................................234 Checking Them Out.................................................................................235 Handling Activity Lifecycle Events.....................................................239 Schroedinger's Activity.............................................................................239 Life, Death, and Your Activity.................................................................240 onCreate() and onDestroy()..............................................................240 onStart(), onRestart(), and onStop().................................................241 onPause() and onResume()...............................................................242 The Grace of State.....................................................................................242 When Activities Die.................................................................................244 Handling Rotation...............................................................................245 A Philosophy of Destruction....................................................................245 It's All The Same, Just Different..............................................................246 Picking and Viewing a Contact.........................................................248 Saving Your State...............................................................................250 Now With More Savings!.........................................................................253 DIY Rotation..............................................................................................255 ...But Google Does Not Recommend This........................................259 Forcing the Issue.......................................................................................259 ix Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 3.0 License Edition
Description: