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 4.0 License Edition The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © 2008-2012 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: September 2012: Version 4.2 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 4.0 License Edition Table of Contents Headings formatted in bold-italic have changed since the last version. • Preface ◦ Welcome to the Book! ....................................................................... xxv ◦ The Book’s Structure ......................................................................... xxv ◦ The Trails ............................................................................... xxvi ◦ Warescription .................................................................................... xxx ◦ Getting Help ...................................................................................... xxxi ◦ Book Bug Bounty .............................................................................. xxxi ◦ Source Code And Its License ........................................................... xxxii ◦ Creative Commons and the Four-to-Free (42F) Guarantee .......... xxxii ◦ Acknowledgments .......................................................................... xxxiii • Key Android Concepts ◦ Android Applications ............................................................................ 1 ◦ Android Devices .................................................................................... 7 ◦ Don’t Be Scared .................................................................................... 10 • Choosing Your IDE ◦ Eclipse .................................................................................................... 11 ◦ Alternative IDEs ................................................................................... 12 ◦ IDEs… And This Book ........................................................................... 13 ◦ About App Inventor .............................................................................. 13 • Tutorial #1 - Installing the Tools ◦ Step #1 - Checking Your Hardware Requirements ............................. 15 ◦ Step #2 - Setting Up Java ..................................................................... 16 ◦ Step #3 - Install the Android SDK ...................................................... 16 ◦ Step #4 - Install the ADT for Eclipse .................................................. 18 ◦ Step #5 - Install Apache Ant ............................................................... 20 ◦ Step #6 - Set Up the Emulator ............................................................ 21 ◦ Step #7 - Set Up the Device ................................................................. 28 ◦ In Our Next Episode… .......................................................................... 31 • Tutorial #2 - Creating a Stub Project ◦ About Our Tutorial Project ...................................................... 33 ◦ About the Rest of the Tutorials ........................................................... 34 ◦ About the Eclipse Instructions ........................................................... 34 ◦ Step #1: Creating the Project ............................................................... 35 ◦ Step #2: Running the Project ............................................................. 40 ◦ Step #3: Perform Some Minor Cleanup ............................................. 44 i Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition ◦ In Our Next Episode… ........................................................................ 44 • Contents of Android Projects ◦ Root Contents ........................................................................... 45 ◦ The Sweat Off Your Brow ................................................................... 46 ◦ Resources ............................................................................................. 46 ◦ What You Get Out Of It ............................................................ 47 • Inside the Manifest ◦ An Application For Your Application ................................................. 51 ◦ Specifying Versions .............................................................................. 51 ◦ Supporting Multiple Screens .............................................................. 52 ◦ Other Stuff ........................................................................................... 53 • Tutorial #3 - Changing Our Manifest ◦ Step #1: Supporting Screens ................................................................ 55 ◦ Step #2: Validating our Minimum SDK Version ....................... 59 ◦ In Our Next Episode… ........................................................................ 60 • Some Words About Resources ◦ String Theory ....................................................................................... 61 ◦ Got the Picture? ........................................................................ 64 ◦ Dimensions ......................................................................................... 66 ◦ The Resource That Shall Not Be Named… Yet ................................... 67 • Tutorial #4 - Adjusting Our Resources ◦ Step #1: Changing the Name ..................................................... 69 ◦ Step #2: Changing the Icon ................................................................. 72 ◦ In Our Next Episode… ........................................................................ 80 • The Theory of Widgets ◦ What Are Widgets? ............................................................................. 81 ◦ Size, Margins, and Padding ................................................................. 83 ◦ What Are Containers? ......................................................................... 83 ◦ The Absolute Positioning Anti-Pattern ............................................. 84 • The Android User Interface ◦ The Activity .......................................................................................... 87 ◦ Dissecting the Activity ....................................................................... 88 ◦ Using XML-Based Layouts ................................................................. 89 • Basic Widgets ◦ Common Concepts .............................................................................. 95 ◦ Assigning Labels .................................................................................. 97 ◦ A Commanding Button ..................................................................... 102 ◦ Fleeting Images ...................................................................... 106 ◦ Fields of Green. Or Other Colors. ...................................................... 111 ◦ More Common Concepts ................................................................... 114 ◦ Visit the Trails! .................................................................................... 116 ii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition • Debugging Crashes ◦ Get Thee To a Stack Trace ................................................................. 120 ◦ The Case of the Confounding Class Cast .......................................... 123 ◦ Point Break .......................................................................................... 123 • LinearLayout and the Box Model ◦ Concepts and Properties .................................................................... 125 ◦ Eclipse Graphical Layout Editor ....................................................... 129 • Other Common Widgets and Containers ◦ Just a Box to Check ............................................................................. 131 ◦ Don’t Like Checkboxes? How About Toggles? .................................. 134 ◦ Turn the Radio Up ............................................................................. 136 ◦ All Things Are Relative ...................................................................... 138 ◦ Tabula Rasa ......................................................................................... 145 ◦ Scrollwork ........................................................................................... 149 ◦ Making Progress with ProgressBars .................................................. 152 ◦ Visit the Trails! .................................................................................... 153 • Tutorial #5 - Making Progress ◦ Step #1: Removing The “Hello, World” .............................................. 155 ◦ Step #2: Adding a ProgressBar ........................................................... 157 ◦ Step #3: Seeing the Results ............................................................... 159 ◦ In Our Next Episode… ....................................................................... 160 • GUI Building, Continued ◦ Making Your Selection ....................................................................... 161 ◦ Including Includes .............................................................................. 161 ◦ Wrap It Up (In a Container) ............................................................. 163 ◦ Morphing Widgets ............................................................................. 163 ◦ Preview of Coming Attractions ......................................................... 164 • AdapterViews and Adapters ◦ Adapting to the Circumstances ........................................................ 165 ◦ Lists of Naughty and Nice ................................................................. 167 ◦ Clicks versus Selections ..................................................................... 169 ◦ Spin Control ........................................................................................ 173 ◦ Grid Your Lions (Or Something Like That…) .................................. 176 ◦ Fields: Now With 35% Less Typing! ................................................. 180 ◦ Galleries, Give Or Take The Art ........................................................ 185 ◦ Customizing the Adapter .................................................................. 186 ◦ Visit the Trails! ................................................................................... 194 • The WebView Widget ◦ Role of WebView ............................................................................... 195 ◦ WebView and WebKit ....................................................................... 196 ◦ Adding the Widget ............................................................................ 196 iii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition ◦ Loading Content Via a URL .............................................................. 197 ◦ Supporting JavaScript ........................................................................ 199 ◦ Alternatives for Loading Content .................................................... 200 ◦ Listening for Events ........................................................................... 201 ◦ Visit the Trails! ................................................................................... 205 • Defining and Using Styles ◦ Styles: DIY DRY .................................................................................. 207 ◦ Elements of Style .............................................................................. 209 ◦ Themes: Would a Style By Any Other Name… ................................. 212 • JARs and Library Projects ◦ The Dalvik VM ................................................................................... 214 ◦ The Easy Part ..................................................................................... 214 ◦ The Outer Limits ................................................................................ 215 ◦ OK, So What is a Library Project? .................................................... 216 ◦ Creating a Library Project ................................................................. 216 ◦ Using a Library Project ....................................................................... 217 ◦ Limitations of Library Projects ......................................................... 218 ◦ The Android Support Package .......................................................... 218 ◦ JAR Dependency Management .......................................................... 221 • Tutorial #6 - Adding a Library ◦ Step #1: Downloading and Unpacking ActionBarSherlock ............. 223 ◦ Step #2: Adding the Library to Your Project .................................... 224 ◦ In Our Next Episode… ....................................................................... 227 • Options Menus and the Action Bar ◦ Terminology ....................................................................................... 229 ◦ A Wee Spot O’ History ....................................................................... 231 ◦ Your Action Bar Options ................................................................... 232 ◦ Setting the Target .............................................................................. 235 ◦ Minding Narrow ................................................................................ 236 ◦ Defining the Resource ............................................................ 236 ◦ Applying the Resource ...................................................................... 241 ◦ Responding to Events ........................................................................ 241 ◦ Attaching to Action Layouts .................................................. 242 ◦ The Rest of the Sample Activity ....................................................... 242 ◦ Visit the Trails! ................................................................................... 250 • Tutorial #7 - Adding the Action Bar ◦ Step #1: Setting Up the Target SDK Version ...................................... 251 ◦ Step #2: Setting the Theme and Splitting the Bar ........................... 253 ◦ Step #3: Changing to SherlockFragmentActivity ............................ 255 ◦ Step #4: Defining Some Options ...................................................... 256 ◦ Step #5: Loading and Responding to Our Options ......................... 259 iv Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition ◦ In Our Next Episode… ....................................................................... 262 • Android’s Process Model ◦ When Processes Are Created ............................................................ 263 ◦ BACK, HOME, and Your Process ...................................................... 264 ◦ Termination ........................................................................................ 265 ◦ Foreground Means “I Love You” ....................................................... 265 ◦ You and Your Heap ........................................................................... 266 • Activities and Their Lifecycles ◦ Creating Your Second (and Third and…) Activity .......................... 268 ◦ Warning! Contains Explicit Intents! ................................................. 273 ◦ Using Implicit Intents ....................................................................... 275 ◦ Extra! Extra! ....................................................................................... 280 ◦ Asynchronicity and Results .............................................................. 282 ◦ Schroedinger’s Activity ........................................................... 282 ◦ Life, Death, and Your Activity ........................................................... 283 ◦ When Activities Die .......................................................................... 285 ◦ Walking Through the Lifecycle ....................................................... 286 ◦ Recycling Activities ........................................................................... 289 • Tutorial #8 - Setting Up An Activity ◦ Step #1: Creating the Stub Activity Class ......................................... 291 ◦ Step #2: Adding the Activity to the Manifest .................................. 293 ◦ Step #3: Launching Our Activity ...................................................... 295 ◦ In Our Next Episode… ...................................................................... 296 • The Tactics of Fragments ◦ The Six Questions .............................................................................. 297 ◦ Your First Fragment .......................................................................... 299 ◦ The Fragment Lifecycle Methods ..................................................... 304 ◦ Your First Dynamic Fragment ........................................................... 305 ◦ Fragments and the Action Bar .......................................................... 309 ◦ Fragments Within Fragments: Just Say No ...................................... 310 ◦ Fragments and Multiple Activities ................................................... 310 • Tutorial #9 - Starting Our Fragments ◦ Step #1: Copy In WebViewFragment ................................................. 313 ◦ Step #2: Examining WebViewFragment ............................................ 317 ◦ Step #3: Creating AbstractContentFragment ................................... 317 ◦ Step #4: Examining AbstractContentFragment ............................... 319 ◦ In Our Next Episode… ....................................................................... 319 • Swiping with ViewPager ◦ Swiping Design Patterns .................................................................... 321 ◦ Paging Fragments .............................................................................. 322 ◦ Paging Other Stuff ............................................................................. 327 v Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition ◦ Indicators ........................................................................................... 327 ◦ Fragment-Free Paging ........................................................................ 331 • Tutorial #10 - Rigging Up a ViewPager ◦ Step #1: Add a ViewPager to the Layout ........................................... 333 ◦ Step #2: Obtaining Our ViewPager .................................................. 334 ◦ Step #3: Creating a ContentsAdapter ............................................... 335 ◦ Step #4: Setting Up the ViewPager ................................................... 336 ◦ In Our Next Episode… ....................................................................... 337 • Resource Sets and Configurations ◦ What’s a Configuration? And How Do They Change? .................... 339 ◦ Configurations and Resource Sets .................................................... 340 ◦ Coping with Complexity ................................................................... 341 ◦ Default Change Behavior .................................................................. 343 ◦ Your Options for Configuration Changes ............................... 345 ◦ Blocking Rotations ............................................................................ 357 • Dealing with Threads ◦ The Main Application Thread .......................................................... 359 ◦ Getting to the Background ................................................................ 360 ◦ Asyncing Feeling ................................................................................ 361 ◦ Alternatives to AsyncTask ................................................................. 369 ◦ And Now, The Caveats ....................................................................... 370 • Requesting Permissions ◦ Mother, May I? ................................................................................... 372 ◦ New Permissions in Old Applications .............................................. 373 ◦ Permissions: Up Front Or Not At All ............................................... 374 ◦ Signature Permissions ....................................................................... 375 ◦ Requiring Permissions ....................................................................... 375 • Assets, Files, and Data Parsing ◦ Packaging Files with Your App ......................................................... 377 ◦ Files and Android .............................................................................. 379 ◦ Working with Internal Storage ......................................................... 380 ◦ Working with External Storage ......................................................... 382 ◦ Linux Filesystems: You Sync, You Win ............................................. 386 ◦ StrictMode: Avoiding Janky Code ..................................................... 387 ◦ XML Parsing Options ........................................................................ 394 ◦ JSON Parsing Options ....................................................................... 395 • Tutorial #11 - Adding Simple Content ◦ Step #1: Adding Some Content ......................................................... 397 ◦ Step #2: Create a SimpleContentFragment ...................................... 398 ◦ Step #3: Examining SimpleContentFragment ................................. 399 ◦ Step #4: Using SimpleContentFragment .......................................... 399 vi Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition ◦ Step #5: Launching Our Activities, For Real This Time ................. 400 ◦ In Our Next Episode… ....................................................................... 403 • Tutorial #12 - Displaying the Book ◦ Step #1: Adding a Book ...................................................................... 405 ◦ Step #2: Defining Our Model ........................................................... 406 ◦ Step #3: Examining Our Model ....................................................... 408 ◦ Step #4: Creating a ModelFragment ................................................ 408 ◦ Step #5: Examining the ModelFragment ........................................... 411 ◦ Step #6: Supplying the Content ........................................................ 412 ◦ Step #7: Adapting the Content .......................................................... 413 ◦ Step #8: Going Home, Again ............................................................ 415 ◦ In Our Next Episode… ....................................................................... 416 • Using Preferences ◦ Getting What You Want .................................................................... 417 ◦ Stating Your Preference ..................................................................... 418 ◦ Introducing PreferenceActivity ........................................................ 419 ◦ Types of Preferences .......................................................................... 431 ◦ Intents for Headers or Preferences ................................................... 434 • Tutorial #13 - Using Some Preferences ◦ Step #1: Adding a StockPreferenceFragment ................................... 438 ◦ Step #2: Defining the Preference XML Files .................................... 439 ◦ Step #3: Creating Our PreferenceActivity ........................................ 441 ◦ Step #4: Adding To Our Action Bar .................................................. 442 ◦ Step #5: Launching the PreferenceActivity ..................................... 444 ◦ Step #6: Loading Our Preferences .................................................... 447 ◦ Step #7: Saving the Last-Read Position ........................................... 449 ◦ Step #8: Restoring the Last-Read Position ....................................... 450 ◦ Step #9: Keeping the Screen On ....................................................... 450 ◦ In Our Next Episode… ....................................................................... 451 • SQLite Databases ◦ Introducting SQLite .......................................................................... 453 ◦ Thinking About Schemas .................................................................. 454 ◦ Start with a Helper ............................................................................ 454 ◦ Getting Data Out .............................................................................. 460 ◦ The Rest of the CRUD ....................................................................... 465 ◦ Hey, What About Hibernate? ........................................................... 470 ◦ Visit the Trails! ................................................................................... 470 • Tutorial #14 - Saving Notes ◦ Step #1: Adding a DatabaseHelper ................................................... 471 ◦ Step #2: Examining DatabaseHelper ................................................ 473 ◦ Step #3: Creating a NoteFragment ................................................... 474 vii Subscribe to updates at http://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition
Description: