The Busy Coder's Guide to Android Development by Mark L. Murphy Subscribe to updates at https://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-2014 CommonsWare, LLC. All Rights Reserved. Printed in the United States of America. Printing History: November 2014: Version 6.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 https://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! ..................................................................... xxxv ◦ The Book’s Structure ....................................................................... xxxv ◦ The Trails ............................................................................. xxxvi ◦ About the Updates .............................................................................. xli ◦ Warescription ..................................................................................... xli ◦ Book Bug Bounty ............................................................................... xlii ◦ Source Code and Its License ............................................................ xliii ◦ Creative Commons and the Four-to-Free (42F) Guarantee .... xliv ◦ Acknowledgments ............................................................................. xliv • Key Android Concepts ◦ Android Applications ............................................................................ 1 ◦ Android Devices .......................................................................... 7 ◦ Don’t Be Scared .................................................................................... 10 • Choosing Your Development Toolchain ◦ Android Studio ..................................................................................... 11 ◦ Eclipse .................................................................................................... 11 ◦ IntelliJ IDEA ......................................................................................... 12 ◦ Command-Line Builds via Gradle for Android .................................. 12 ◦ Yet Other Alternatives ......................................................................... 12 ◦ IDEs… And This Book ........................................................................... 13 ◦ What We Are Not Covering ................................................................. 13 • Tutorial #1 - Installing the Tools ◦ Step #1 - Checking Your Hardware Requirements ............................. 15 ◦ Step #2 - Setting Up Java ........................................................... 16 ◦ Step #3 - Install the Developer Tools ........................................ 16 ◦ Step #4 - Install the SDKs and Add-Ons.................................... 20 ◦ In Our Next Episode… ......................................................................... 26 • Android and Projects ◦ Common Concepts .................................................................... 27 ◦ Projects and Android Studio ............................................................... 28 ◦ Projects and Eclipse ............................................................................. 35 ◦ Starter Project Generators ................................................................... 41 • Tutorial #2 - Creating a Stub Project ◦ About Our Tutorial Project ................................................................. 43 i Subscribe to updates at https://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition ◦ About the Rest of the Tutorials .......................................................... 44 ◦ About Our Tools........................................................................ 44 ◦ Step #1: Creating the Project .................................................... 45 ◦ Step #2 - Set Up the Emulator .................................................... 51 ◦ Step #3 - Set Up the Device ................................................................. 62 ◦ Step #4: Running the Project .............................................................. 65 ◦ In Our Next Episode… ........................................................................ 68 • Getting Around Android Studio ◦ Navigating The Project Explorer ........................................................ 69 ◦ Running Projects ................................................................................. 70 ◦ Viewing Output ................................................................................... 71 ◦ Accessing Android Tools ..................................................................... 72 • Contents of Android Projects ◦ What You Get, In General ................................................................... 77 ◦ The Contents of an Android Studio Project ...................................... 79 ◦ The Contents of an Eclipse Project .................................................... 81 ◦ What You Get Out Of It ...................................................................... 82 • Inside the Manifest ◦ An Application For Your Application ................................................. 85 ◦ Specifying Versions ............................................................................. 86 ◦ Supporting Multiple Screens ............................................................. 86 ◦ Other Stuff ........................................................................................... 87 • Introducing Gradle ◦ The Big Questions .............................................................................. 89 ◦ Obtaining Gradle ...................................................................... 92 ◦ Versions of Gradle and Gradle for Android ...................................... 94 ◦ Gradle Environment Variables ............................................................ 95 ◦ Examining the Gradle File .................................................................. 95 ◦ Learning More About Gradle ............................................................. 99 ◦ Visit the Trails! .................................................................................... 99 • Tutorial #3 - Changing Our Manifest (and Gradle File) ◦ Some Notes About Relative Paths ..................................................... 101 ◦ Step #1: Supporting Screens .............................................................. 102 ◦ Step #2: Adding our Minimum and Target SDK Versions .............. 105 ◦ In Our Next Episode… ....................................................................... 107 • Some Words About Resources ◦ String Theory ...................................................................................... 110 ◦ Got the Picture? .................................................................................. 114 ◦ Dimensions ......................................................................................... 118 ◦ The Resource That Shall Not Be Named… Yet ................................. 120 • Tutorial #4 - Adjusting Our Resources ii Subscribe to updates at https://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition ◦ Step #1: Changing the Name .............................................................. 121 ◦ Step #2: Changing the Icon ................................................................ 123 ◦ Step #3: Running the Result ............................................................... 132 ◦ In Our Next Episode… ........................................................................ 134 • The Theory of Widgets ◦ What Are Widgets? ............................................................................ 135 ◦ Size, Margins, and Padding ................................................................ 137 ◦ What Are Containers? ........................................................................ 137 ◦ The Absolute Positioning Anti-Pattern ............................................ 138 • The Android User Interface ◦ The Activity ......................................................................................... 141 ◦ Dissecting the Activity ...................................................................... 142 ◦ Using XML-Based Layouts ................................................................. 143 • Basic Widgets ◦ Common Concepts ............................................................................ 149 ◦ Assigning Labels ................................................................................. 152 ◦ A Commanding Button ..................................................................... 162 ◦ Fleeting Images .......................................................................166 ◦ Fields of Green. Or Other Colors. ..................................................... 173 ◦ More Common Concepts .................................................................. 178 ◦ Visit the Trails! ................................................................................... 180 • Debugging Your App ◦ Get Thee To a Stack Trace ................................................................. 182 ◦ The Case of the Confounding Class Cast ......................................... 186 ◦ Point Break ......................................................................................... 187 • LinearLayout and the Box Model ◦ Concepts and Properties ................................................................... 189 ◦ Android Studio Graphical Layout Editor ......................................... 193 ◦ Eclipse Graphical Layout Editor ....................................................... 194 • Other Common Widgets and Containers ◦ Just a Box to Check ............................................................................ 197 ◦ Don’t Like Checkboxes? How About Toggles or Switches? ............ 201 ◦ Turn the Radio Up ............................................................................. 205 ◦ All Things Are Relative ..................................................................... 208 ◦ Tabula Rasa ......................................................................................... 215 ◦ Scrollwork ........................................................................................... 222 ◦ Making Progress with ProgressBars ................................................. 225 ◦ Visit the Trails! ................................................................................... 226 • Tutorial #5 - Making Progress ◦ Step #1: Removing The “Hello, World” ............................................. 227 ◦ Step #2: Adding a ProgressBar .......................................................... 229 iii Subscribe to updates at https://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition ◦ Step #3: Seeing the Results ............................................................... 232 ◦ In Our Next Episode… ....................................................................... 232 • GUI Building, Continued ◦ Making Your Selection ...................................................................... 233 ◦ Including Includes ................................................................... 234 ◦ Wrap It Up (In a Container) ..................................................... 235 ◦ Morphing Widgets ............................................................................. 236 ◦ Preview of Coming Attractions ......................................................... 237 • AdapterViews and Adapters ◦ Adapting to the Circumstances ........................................................ 239 ◦ Lists of Naughty and Nice ................................................................. 241 ◦ Clicks versus Selections ..................................................................... 243 ◦ Spin Control ....................................................................................... 247 ◦ Grid Your Lions (Or Something Like That…) .................................. 250 ◦ Fields: Now With 35% Less Typing! ................................................. 253 ◦ Customizing the Adapter .................................................................. 257 ◦ Visit the Trails! ................................................................................... 265 • The WebView Widget ◦ Role of WebView ............................................................................... 267 ◦ WebView and WebKit ...................................................................... 268 ◦ Adding the Widget ........................................................................... 268 ◦ Loading Content Via a URL ............................................................. 269 ◦ Supporting JavaScript ........................................................................ 270 ◦ Alternatives for Loading Content ...................................................... 271 ◦ Listening for Events ........................................................................... 272 ◦ WebView and Android 4.4 ................................................................ 275 ◦ Visit the Trails! ................................................................................... 276 • Defining and Using Styles ◦ Styles: DIY DRY .................................................................................. 277 ◦ Elements of Style ............................................................................... 279 ◦ Themes: Would a Style By Any Other Name… ................................ 282 • JARs and Library Projects ◦ The Dalvik VM, and a Bit of ART ..................................................... 284 ◦ Getting the Library ............................................................................ 284 ◦ The Outer Limits .............................................................................. 286 ◦ JAR Dependency Management ......................................................... 287 ◦ OK, So What is a Library Project? .................................................... 287 ◦ Using a Library Project ..................................................................... 288 ◦ Library Projects: What You Get ....................................................... 289 ◦ The Android Support Package ......................................................... 290 • Tutorial #6 - Adding a Library iv Subscribe to updates at https://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition ◦ Step #1: Attaching the Android Support Package ........................... 293 ◦ Step #2: Downloading the Third-Party JARs ................................... 294 ◦ In Our Next Episode… ...................................................................... 296 • The Action Bar ◦ Bar Hopping ....................................................................................... 297 ◦ Yet Another History Lesson .............................................................. 302 ◦ Your Action Bar Options ................................................................... 303 ◦ Setting the Target .............................................................................. 304 ◦ Splitting the Bar ................................................................................. 306 ◦ Defining the Resource ....................................................................... 307 ◦ Applying the Resource ...................................................................... 310 ◦ Responding to Events ......................................................................... 311 ◦ Attaching to Action Layouts .............................................................. 311 ◦ The Rest of the Sample Activity ........................................................ 312 ◦ MENU Key, We Hardly Knew Ye ...................................................... 319 ◦ Visit the Trails! ................................................................................... 320 • Tutorial #7 - Setting Up the Action Bar ◦ Step #1: Splitting the Bar ......................................................... 321 ◦ Step #2: Acquiring Some Icons ......................................................... 323 ◦ Step #3: Defining Some Options ...................................................... 324 ◦ Step #4: Loading and Responding to Our Options ......................... 327 ◦ In Our Next Episode… ........................................................................ 331 • Android’s Process Model ◦ When Processes Are Created ............................................................ 333 ◦ BACK, HOME, and Your Process ...................................................... 334 ◦ Termination ........................................................................................ 335 ◦ Foreground Means “I Love You” ....................................................... 336 ◦ You and Your Heap ............................................................................ 336 • Activities and Their Lifecycles ◦ Creating Your Second (and Third and…) Activity ........................... 338 ◦ Warning! Contains Explicit Intents! ................................................. 344 ◦ Using Implicit Intents ............................................................ 346 ◦ Extra! Extra! ......................................................................................... 351 ◦ Pondering Parcelable ......................................................................... 353 ◦ Asynchronicity and Results .............................................................. 354 ◦ Schroedinger’s Activity ...................................................................... 354 ◦ Life, Death, and Your Activity ........................................................... 355 ◦ When Activities Die .......................................................................... 357 ◦ Walking Through the Lifecycle ........................................................ 358 ◦ Recycling Activities ............................................................................ 361 ◦ Application: Transcending the Activity ........................................... 362 v Subscribe to updates at https://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition • Tutorial #8 - Setting Up An Activity ◦ Step #1: Creating the Stub Activity Class and Manifest Entry ........ 365 ◦ Step #2: Launching Our Activity ...................................................... 368 ◦ In Our Next Episode… ....................................................................... 369 • The Tactics of Fragments ◦ The Six Questions ............................................................................... 371 ◦ Where You Get Your Fragments From ............................................. 374 ◦ Your First Fragment ........................................................................... 374 ◦ The Fragment Lifecycle Methods ..................................................... 377 ◦ Your First Dynamic Fragment ........................................................... 378 ◦ Fragments and the Action Bar .......................................................... 381 ◦ Fragments Within Fragments: Just Say “Maybe” ............................. 383 ◦ Fragments and Multiple Activities ................................................... 383 • Tutorial #9 - Starting Our Fragments ◦ Step #1: Create a SimpleContentFragment ...................................... 385 ◦ Step #2: Examining SimpleContentFragment ................................. 388 ◦ In Our Next Episode… ....................................................................... 388 • Swiping with ViewPager ◦ Swiping Design Patterns ................................................................... 389 ◦ Pieces of a Pager ................................................................................. 390 ◦ Paging Fragments .............................................................................. 390 ◦ Paging Other Stuff ............................................................................. 395 ◦ Indicators ........................................................................................... 396 • Tutorial #10 - Rigging Up a ViewPager ◦ Step #1: Add a ViewPager to the Layout ........................................... 401 ◦ Step #2: Obtaining Our ViewPager .................................................. 402 ◦ Step #3: Creating a ContentsAdapter ............................................... 403 ◦ Step #4: Setting Up the ViewPager .................................................. 404 ◦ In Our Next Episode… ....................................................................... 405 • Resource Sets and Configurations ◦ What’s a Configuration? And How Do They Change? ................... 407 ◦ Configurations and Resource Sets ......................................... 408 ◦ Screen Size and Orientation ............................................................ 409 ◦ Coping with Complexity ................................................................... 412 ◦ Choosing The Right Resource ............................................................ 413 ◦ Default Change Behavior .................................................................. 417 ◦ State Saving Scenarios ....................................................................... 419 ◦ Your Options for Configuration Changes ....................................... 420 ◦ Blocking Rotations ............................................................................ 431 • Dealing with Threads ◦ The Main Application Thread .......................................................... 433 vi Subscribe to updates at https://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition ◦ Getting to the Background ................................................................ 435 ◦ Asyncing Feeling ...................................................................... 435 ◦ Alternatives to AsyncTask ................................................................. 445 ◦ And Now, The Caveats ...................................................................... 446 ◦ Event Buses ........................................................................................ 447 ◦ Visit the Trails! ................................................................................... 454 • Requesting Permissions ◦ Mother, May I? ................................................................................... 456 ◦ New Permissions in Old Applications .............................................. 458 ◦ Permissions: Up Front Or Not At All ............................................... 458 ◦ Signature Permissions ....................................................................... 459 ◦ Requiring Permissions ...................................................................... 460 • Assets, Files, and Data Parsing ◦ Packaging Files with Your App ......................................................... 461 ◦ Files and Android .............................................................................. 463 ◦ Working with Internal Storage ........................................................ 464 ◦ Working with External Storage ........................................................ 467 ◦ Multiple User Accounts ..................................................................... 471 ◦ Linux Filesystems: You Sync, You Win ............................................. 472 ◦ StrictMode: Avoiding Janky Code ..................................................... 473 ◦ XML Parsing Options ........................................................................ 476 ◦ JSON Parsing Options ....................................................................... 477 ◦ Visit the Trails! ................................................................................... 477 • Tutorial #11 - Adding Simple Content ◦ Step #1: Adding Some Content ................................................ 479 ◦ Step #2: Using SimpleContentFragment ......................................... 480 ◦ Step #3: Launching Our Activities, For Real This Time .................. 481 ◦ In Our Next Episode… ....................................................................... 483 • Tutorial #12 - Displaying the Book ◦ Step #1: Adding a Book ...................................................................... 485 ◦ Step #2: Creating a ModelFragment ................................................ 486 ◦ Step #3: Attaching our ModelFragment ........................................... 487 ◦ Step #4: Defining Our Model ........................................................... 488 ◦ Step #5: Examining Our Model ....................................................... 490 ◦ Step #6: Defining Our Event ............................................................ 490 ◦ Step #7: Loading Our Model ............................................................. 491 ◦ Step #8: Registering for Events ........................................................ 494 ◦ Step #9: Adapting the Content ........................................................ 494 ◦ Step #10: Showing the Content When Loaded ................................ 495 ◦ Step #11: Showing the Content After a Configuration Change 497 ◦ Step #12: Going Home, Again .......................................................... 498 vii Subscribe to updates at https://commonsware.com Special Creative Commons BY-NC-SA 4.0 License Edition
Description: