The Busy Coder's Guide to Android Development by Mark L. Murphy The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © 2008-2016 CommonsWare, LLC. All Rights Reserved. Printed in the United States of America. Printing History: June 2016: Version 7.4 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. Table of Contents Headings formatted in bold-italic have changed since the last version. • Preface ◦ Welcome to the Book! .................................................................... xxxix ◦ The Book’s Structure ...................................................................... xxxix ◦ The Trails ................................................................................... xl ◦ About the Updates .............................................................................. xlv ◦ What’s New in Version 7.4? ..................................................... xlvi ◦ Warescription .................................................................................... xlvi ◦ About the APK Edition ..................................................................... xlvii ◦ Extending Your Warescription ...................................................... xlviii ◦ Source Code and Its License ................................................................. l ◦ Creative Commons and the Four-to-Free (42F) Guarantee ........ l ◦ Acknowledgments ................................................................................ li • 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 .......................................................................... 13 ◦ 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 and 32-Bit Linux Support .......................... 16 ◦ Step #3 - Install the Developer Tools .................................................. 16 ◦ Step #4 - Install the SDKs and Add-Ons ............................................. 17 ◦ In Our Next Episode… ......................................................................... 27 • Android and Projects ◦ Common Concepts .............................................................................. 29 ◦ Projects and Android Studio .................................................... 30 ◦ Starter Project Generators ................................................................... 37 • Tutorial #2 - Creating a Stub Project i ◦ About Our Tutorial Project ................................................................. 39 ◦ About the Rest of the Tutorials .......................................................... 40 ◦ About Our Tools ................................................................................. 40 ◦ Step #1: Creating the Project .............................................................. 40 ◦ Step #2 - Set Up the Emulator ............................................................ 43 ◦ Step #3 - Set Up the Device ................................................................. 52 ◦ Step #4: Running the Project .............................................................. 55 ◦ In Our Next Episode… ......................................................................... 57 • Getting Around Android Studio ◦ Navigating The Project Explorer .............................................. 59 ◦ Running Projects ................................................................................. 62 ◦ Viewing Output .................................................................................. 64 ◦ Accessing Android Tools .................................................................... 66 ◦ Android Studio and Release Channels .............................................. 69 ◦ Visit the Trails! ..................................................................................... 70 • Contents of Android Projects ◦ What You Get, In General .......................................................... 71 ◦ The Contents of an Android Studio Project ............................. 73 ◦ The Contents of an Eclipse-Style Project .................................. 75 ◦ What You Get Out Of It ............................................................ 76 • Introducing Gradle and the Manifest ◦ Gradle: The Big Questions ........................................................ 77 ◦ Obtaining Gradle ................................................................................ 80 ◦ Versions of Gradle and the Android Plugin for Gradle ............ 82 ◦ Gradle Environment Variables ............................................................ 83 ◦ Examining the Gradle Files ................................................................. 83 ◦ Introducing the Manifest ......................................................... 86 ◦ Things In Common Between the Manifest and Gradle ............ 86 ◦ Other Gradle Items of Note ...................................................... 90 ◦ Where’s the GUI? ................................................................................. 91 ◦ The Rest of the Manifest ........................................................... 91 ◦ Learning More About Gradle ............................................................. 94 ◦ Visit the Trails! ......................................................................... 94 • Tutorial #3 - Changing Our Manifest (and Gradle File) ◦ Some Notes About Relative Paths ...................................................... 95 ◦ Step #1: Supporting Screens ............................................................... 96 ◦ Step #2: Adding our Minimum and Target SDK Versions ......... 96 ◦ In Our Next Episode… ............................................................... 97 • Some Words About Resources ◦ String Theory .......................................................................... 100 ◦ Got the Picture? ....................................................................... 102 ii ◦ Dimensions ........................................................................................ 107 ◦ The Resource That Shall Not Be Named… Yet ................................. 108 • Tutorial #4 - Adjusting Our Resources ◦ Step #1: Changing the Name ............................................................. 109 ◦ Step #2: Changing the Icon ................................................................ 110 ◦ Step #3: Running the Result ............................................................... 113 ◦ In Our Next Episode… ........................................................................ 114 • The Theory of Widgets ◦ What Are Widgets? ............................................................................ 115 ◦ Size, Margins, and Padding ................................................................ 117 ◦ What Are Containers? ........................................................................ 117 ◦ The Absolute Positioning Anti-Pattern ............................................. 118 ◦ The Theme of This Section: Themes ................................................. 119 • The Android User Interface ◦ The Activity .............................................................................. 123 ◦ Dissecting the Activity ...................................................................... 124 ◦ Using XML-Based Layouts ....................................................... 125 • Basic Widgets ◦ Common Concepts ............................................................................ 129 ◦ Assigning Labels ................................................................................. 132 ◦ A Commanding Button ..................................................................... 138 ◦ Fleeting Images .................................................................................. 142 ◦ Fields of Green. Or Other Colors. .................................................... 147 ◦ More Common Concepts ................................................................... 151 ◦ Visit the Trails! ................................................................................... 154 • Debugging Your App ◦ Get Thee To a Stack Trace ........................................................ 156 ◦ The Case of the Confounding Class Cast ................................. 157 ◦ Point Break .............................................................................. 158 • LinearLayout and the Box Model ◦ Concepts and Properties ................................................................... 159 ◦ Android Studio Graphical Layout Editor ......................................... 163 • Other Common Widgets and Containers ◦ Just a Box to Check ............................................................................ 165 ◦ Don’t Like Checkboxes? How About Toggles or Switches? ............ 170 ◦ Turn the Radio Up .............................................................................. 175 ◦ All Things Are Relative ............................................................ 178 ◦ Tabula Rasa ........................................................................................ 186 ◦ Scrollwork ........................................................................................... 192 ◦ Making Progress with ProgressBars ................................................. 195 ◦ Visit the Trails! ................................................................................... 196 iii • Tutorial #5 - Making Progress ◦ Step #1: Removing The “Hello, World” ............................................. 197 ◦ Step #2: Adding a ProgressBar ................................................ 200 ◦ Step #3: Seeing the Results ............................................................... 201 ◦ In Our Next Episode… .............................................................. 201 • GUI Building, Continued ◦ Making Your Selection ............................................................ 203 ◦ Including Includes ............................................................................. 204 ◦ Morphing Widgets ............................................................................ 206 ◦ Preview of Coming Attractions ........................................................ 206 • AdapterViews and Adapters ◦ Adapting to the Circumstances ........................................................ 207 ◦ Lists of Naughty and Nice ................................................................ 209 ◦ Clicks versus Selections ...................................................................... 211 ◦ Spin Control ........................................................................................ 215 ◦ Grid Your Lions (Or Something Like That…) .................................. 218 ◦ Fields: Now With 35% Less Typing! .................................................. 221 ◦ Customizing the Adapter .................................................................. 225 ◦ Visit the Trails! ................................................................................... 233 • The WebView Widget ◦ Role of WebView ............................................................................... 235 ◦ WebView and WebKit ....................................................................... 236 ◦ The Android System WebView ......................................................... 236 ◦ Adding the Widget ................................................................... 237 ◦ Loading Content Via a URL .............................................................. 237 ◦ Links and Redirects ........................................................................... 239 ◦ Supporting JavaScript ....................................................................... 240 ◦ Alternatives for Loading Content .................................................... 240 ◦ Listening for Events ........................................................................... 242 ◦ Addressing the Link/Redirect Behavior ........................................... 245 ◦ Visit the Trails! .................................................................................. 246 • Defining and Using Styles ◦ Styles: DIY DRY .................................................................................. 247 ◦ Elements of Style ............................................................................... 249 ◦ Themes: Would a Style By Any Other Name… ................................ 252 ◦ What Happens If You Have No Theme ............................................ 252 ◦ Android Studio’s Theme Editor ........................................................ 253 • JARs and Library Projects ◦ The Dalvik VM, and a Bit of ART .................................................... 260 ◦ Getting the Library ................................................................. 260 ◦ The Outer Limits ............................................................................... 262 iv ◦ JAR Dependency Management ............................................... 262 ◦ OK, So What is a Library Project? .................................................... 263 ◦ Using a Library Project ........................................................... 263 ◦ Library Projects: What You Get .............................................. 264 ◦ The Android Support Package .......................................................... 264 • Tutorial #6 - Adding a Library ◦ Step #1: Attaching the Android Support Package .......................... 269 ◦ Step #2: Attaching the Third-Party Dependencies ......................... 270 ◦ In Our Next Episode… ........................................................................ 271 • The Action Bar ◦ Bar Hopping ....................................................................................... 273 ◦ Yet Another History Lesson .............................................................. 279 ◦ Your Action Bar Options .................................................................. 280 ◦ Setting the Target ................................................................... 282 ◦ Defining the Resource .............................................................283 ◦ Applying the Resource ...................................................................... 285 ◦ Responding to Events ........................................................................ 285 ◦ The Rest of the Sample Activity ...................................................... 286 ◦ MENU Key, We Hardly Knew Ye ...................................................... 292 ◦ Action Bars, Live in Living Color! ........................................... 293 ◦ Visit the Trails! ................................................................................... 302 • Tutorial #7 - Setting Up the Action Bar ◦ Step #1: Acquiring Some Icons .......................................................... 303 ◦ Step #2: Defining Some Options ...................................................... 305 ◦ Step #3: Loading and Responding to Our Options ......................... 307 ◦ Step #4: Trying It Out ........................................................................ 308 ◦ In Our Next Episode… ....................................................................... 310 • Android’s Process Model ◦ When Processes Are Created ............................................................. 311 ◦ BACK, HOME, and Your Process ....................................................... 312 ◦ Termination ......................................................................................... 313 ◦ Foreground Means “I Love You” ....................................................... 314 ◦ You and Your Heap ............................................................................ 314 • Activities and Their Lifecycles ◦ Creating Your Second (and Third and…) Activity.................... 316 ◦ Warning! Contains Explicit Intents! ................................................. 319 ◦ Using Implicit Intents ....................................................................... 322 ◦ Extra! Extra! ........................................................................................ 327 ◦ Pondering Parcelable ......................................................................... 329 ◦ Asynchronicity and Results .............................................................. 330 ◦ Schroedinger’s Activity ...................................................................... 330 v ◦ Life, Death, and Your Activity ............................................................ 331 ◦ When Activities Die .......................................................................... 333 ◦ Walking Through the Lifecycle ........................................................ 334 ◦ Recycling Activities ............................................................................ 337 ◦ Application: Transcending the Activity ........................................... 338 ◦ The Case of the Invisible Activity ..................................................... 339 • Tutorial #8 - Setting Up An Activity ◦ Step #1: Creating the Stub Activity Class and Manifest Entry ........ 341 ◦ Step #2: Launching Our Activity ...................................................... 343 ◦ In Our Next Episode… ....................................................................... 343 • The Tactics of Fragments ◦ The Six Questions .............................................................................. 345 ◦ Where You Get Your Fragments From ............................................. 348 ◦ Your First Fragment ................................................................ 348 ◦ The Fragment Lifecycle Methods ..................................................... 352 ◦ Your First Dynamic Fragment ........................................................... 353 ◦ Fragments and the Action Bar .......................................................... 356 ◦ Fragments Within Fragments: Just Say “Maybe” ............................. 357 ◦ Fragments and Multiple Activities ................................................... 358 • Tutorial #9 - Starting Our Fragments ◦ Step #1: Create a SimpleContentFragment ...................................... 361 ◦ Step #2: Examining SimpleContentFragment ................................. 363 ◦ In Our Next Episode… ....................................................................... 364 • Swiping with ViewPager ◦ Swiping Design Patterns ................................................................... 365 ◦ Pieces of a Pager ................................................................................. 366 ◦ Paging Fragments .............................................................................. 366 ◦ Paging Other Stuff .............................................................................. 371 ◦ Indicators ........................................................................................... 372 • Tutorial #10 - Rigging Up a ViewPager ◦ Step #1: Add a ViewPager to the Layout ........................................... 375 ◦ Step #2: Obtaining Our ViewPager .................................................. 376 ◦ Step #3: Creating a ContentsAdapter ............................................... 377 ◦ Step #4: Setting Up the ViewPager ................................................... 377 ◦ In Our Next Episode… ....................................................................... 380 • Resource Sets and Configurations ◦ What’s a Configuration? And How Do They Change? .................... 381 ◦ Configurations and Resource Sets .................................................... 382 ◦ Screen Size and Orientation ............................................................. 383 ◦ Coping with Complexity ................................................................... 386 ◦ Choosing The Right Resource ........................................................... 387 vi ◦ API-Versioned Resources .................................................................. 391 ◦ Default Change Behavior .................................................................. 393 ◦ State Saving Scenarios ....................................................................... 395 ◦ Your Options for Configuration Changes ........................................ 396 ◦ Blocking Rotations ................................................................. 407 ◦ And Now, a Word From the Android Project View ........................ 408 • Material Design Basics ◦ Your App, in Technicolor! .................................................................. 411 • Dealing with Threads ◦ The Main Application Thread .......................................................... 419 ◦ Getting to the Background ................................................................ 421 ◦ Asyncing Feeling ................................................................................ 421 ◦ Alternatives to AsyncTask ................................................................. 430 ◦ And Now, The Caveats ....................................................................... 431 ◦ Event Buses ..............................................................................432 ◦ Visit the Trails! ................................................................................... 441 • Requesting Permissions ◦ Frequently-Asked Questions About Permissions ........................... 444 ◦ Characteristics of Permissions ......................................................... 449 ◦ New Permissions in Old Applications .............................................. 452 ◦ Android 6.0+ Runtime Permission System ...................................... 453 • Tutorial: Runtime Permission Support ◦ Step #0: Install the Android 6.0 SDK .............................................. 468 ◦ Step #1: Import and Review the Starter Project .............................. 469 ◦ Step #2: Update Gradle for Android 6.0 ........................................... 475 ◦ Step #3: Review the Planned UX ...................................................... 478 ◦ Step #4: Detect the First Run ............................................................ 479 ◦ Step #5: On First Run, Ask For Permissions ................................... 480 ◦ Step #6: Check for Permissions Before Taking a Picture ............... 484 ◦ Step #7: Detect If We Should Show Some Rationale ...................... 485 ◦ Step #8: Add a Rationale UI and Re-Request Permissions ............. 487 ◦ Step #9: Check for Permissions Before Recording a Video ............. 493 ◦ Step #10: Detect If We Should Show Some Rationale (Again) ...... 494 ◦ Step #11: Support Configuration Changes ....................................... 496 • Assets, Files, and Data Parsing ◦ Packaging Files with Your App ............................................... 505 ◦ Files and Android .............................................................................. 507 ◦ Working with Internal Storage ......................................................... 508 ◦ Working with External Storage ............................................... 516 ◦ Multiple User Accounts ..................................................................... 522 ◦ Linux Filesystems: You Sync, You Win ............................................. 523 vii
Description: