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-2017 CommonsWare, LLC. All Rights Reserved. Printed in the United States of America. Printing History: November 2017: Version 8.8 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! ........................................................................ xli ◦ The Book’s Structure .......................................................................... xli ◦ The Trails ................................................................................ xlii ◦ About the Updates .......................................................................... xlviii ◦ What’s New in Version 8.8? .................................................. xlviii ◦ Warescription .................................................................................... xlix ◦ About the APK Edition .......................................................................... l ◦ Book Bug Bounty ................................................................................... l ◦ Source Code and Its License ................................................................ li ◦ Creative Commons and the Four-to-Free (42F) Guarantee ...... lii ◦ Acknowledgments ............................................................................... lii • Key Android Concepts ◦ Android Applications .................................................................. 1 ◦ Android Devices .......................................................................... 6 ◦ Don’t Be Scared .................................................................................... 10 • Choosing Your Development Toolchain ◦ Android Studio ........................................................................... 11 ◦ Eclipse .................................................................................................... 11 ◦ IntelliJ IDEA ......................................................................................... 12 ◦ Command-Line Builds via Gradle .............................................. 12 ◦ Yet Other Alternatives ............................................................... 12 ◦ IDEs… And This Book ........................................................................... 13 ◦ What We Are Not Covering ................................................................. 13 • Tutorial #1 - Installing the Tools ◦ But First, Some Notes About Android’s Emulator ..................... 15 ◦ Step #1: Checking Your Hardware ............................................. 16 ◦ Step #2: Setting Up Java and 32-Bit Linux Support ............................ 17 ◦ Step #3: Install Android Studio .................................................. 17 ◦ Step #4: Install the SDKs and Add-Ons .................................... 19 ◦ In Our Next Episode… ......................................................................... 27 • Android and Projects ◦ Projects and Android Studio .................................................... 29 ◦ Starter Project Generators ................................................................... 35 • Tutorial #2 - Creating a Stub Project ◦ About Our Tutorial Project ...................................................... 37 ◦ About the Rest of the Tutorials ........................................................... 38 ◦ About Our Tools........................................................................ 38 ◦ Step #1: Importing the Project .................................................. 38 ◦ Step #2: Get Ready for the x86 Emulator ........................................... 42 ◦ Step #3: Set Up the AVD ............................................................. 43 ◦ Step #4: Set Up the Device......................................................... 50 ◦ Step #5: Running the Project .............................................................. 55 ◦ In Our Next Episode… ......................................................................... 56 • Getting Around Android Studio ◦ Navigating The Project Explorer .............................................. 57 ◦ Running Projects ................................................................................ 60 ◦ Viewing Output ........................................................................ 61 ◦ Accessing Android Tools ..................................................................... 62 ◦ Android Studio and Release Channels .............................................. 64 ◦ Visit the Trails! ..................................................................................... 65 • Contents of Android Projects ◦ What You Get, In General ......................................................... 67 ◦ More About the Directory Structure ........................................ 70 ◦ What You Get Out Of It ............................................................ 72 • Introducing Gradle and the Manifest ◦ Gradle: The Big Questions ........................................................ 73 ◦ Obtaining Gradle ...................................................................... 75 ◦ Versions of Gradle and the Android Gradle Plugin .................. 77 ◦ Gradle Environment Variables ................................................. 78 ◦ Examining the Gradle Files ...................................................... 78 ◦ Introducing the Manifest ......................................................... 81 ◦ Things In Common Between the Manifest and Gradle ............ 82 ◦ Other Gradle Items of Note ...................................................... 84 ◦ Where’s the GUI? ................................................................................. 85 ◦ The Rest of the Manifest ........................................................... 85 ◦ Learning More About Gradle .................................................... 88 ◦ Visit the Trails! ......................................................................... 88 • Tutorial #3 - Manifest Changes ◦ Some Notes About Relative Paths ............................................ 89 ◦ Step #1: Supporting Screens ............................................................... 89 ◦ Step #2: Blocking Backups ................................................................. 90 ◦ Step #3: Ignoring Lint ............................................................... 90 ◦ In Our Next Episode… ......................................................................... 92 • Some Words About Resources ◦ String Theory ....................................................................................... 93 ii ◦ Got the Picture? ........................................................................ 99 ◦ Dimensions ........................................................................................ 103 ◦ The Resource That Shall Not Be Named… Yet ................................. 105 • Icons ◦ App Icons… And Everything Else.............................................. 107 ◦ Creating an App Icon with the Asset Studio ........................... 108 ◦ Creating Other Icons with the Asset Studio ............................ 113 • Tutorial #4 - Adjusting Our Resources ◦ Step #1: Changing the Name .............................................................. 115 ◦ Step #2: Changing the Icon ...................................................... 116 ◦ Step #3: Running the Result .............................................................. 120 ◦ In Our Next Episode… ........................................................................ 121 • The Theory of Widgets ◦ What Are Widgets? ............................................................................ 123 ◦ Size, Margins, and Padding ................................................................ 125 ◦ What Are Containers? ........................................................................ 125 ◦ The Absolute Positioning Anti-Pattern ............................................ 126 ◦ The Theme of This Section: Themes ................................................. 127 • The Android User Interface ◦ The Activity ......................................................................................... 131 ◦ Dissecting the Activity ....................................................................... 132 ◦ Using XML-Based Layouts ................................................................. 133 • Basic Widgets ◦ Common Concepts ............................................................................. 137 ◦ Introducing the Graphical Layout Editor ............................... 139 ◦ Assigning Labels ...................................................................... 149 ◦ A Commanding Button ............................................................ 154 ◦ Fleeting Images ....................................................................... 157 ◦ Fields of Green. Or Other Colors. ............................................ 163 ◦ More Common Concepts .................................................................. 166 ◦ Visit the Trails! ................................................................................... 169 • Debugging Your App ◦ Get Thee To a Stack Trace ........................................................ 172 ◦ The Case of the Confounding Class Cast .......................................... 173 ◦ Point Break .......................................................................................... 174 • The Classic Container Classes ◦ Introducing the Sampler App ............................................................ 175 ◦ RTL and Your Layouts ....................................................................... 176 ◦ LinearLayout and the Box Model ............................................ 177 ◦ All Things Are Relative ............................................................ 201 ◦ Tabula Rasa ......................................................................................... 212 iii ◦ Hey, What About ConstraintLayout? ............................................... 218 ◦ Turning Back to RTL .......................................................................... 219 • Other Common Widgets and Containers ◦ Just a Box to Check .................................................................. 221 ◦ Don’t Like Checkboxes? How About Toggles or Switches? ..... 224 ◦ Turn the Radio Up ............................................................................. 228 ◦ Scrollwork ............................................................................................ 231 ◦ Making Progress with ProgressBars ................................................. 234 ◦ Framing the Scene ............................................................................. 235 ◦ Visit the Trails! ................................................................................... 236 • Tutorial #5 - Creating a Layout ◦ Step #1: Creating a New Layout Resource ........................................ 237 ◦ Step #2: Defining the UI ...........................................................238 ◦ In Our Next Episode… ....................................................................... 243 • GUI Building, Continued ◦ Making Your Selection ...................................................................... 245 ◦ Including Includes .................................................................. 246 ◦ Preview of Coming Attractions ......................................................... 248 • AdapterViews and Adapters ◦ Adapting to the Circumstances ........................................................ 249 ◦ Lists of Naughty and Nice .................................................................. 251 ◦ Clicks versus Selections ..................................................................... 253 ◦ Spin Control ....................................................................................... 257 ◦ Grid Your Lions (Or Something Like That…) ................................. 260 ◦ Fields: Now With 35% Less Typing! ................................................. 264 ◦ Customizing the Adapter ................................................................. 268 ◦ Visit the Trails! ................................................................................... 277 • The WebView Widget ◦ Role of WebView ............................................................................... 279 ◦ Daddy, Where Do WebViews Come From? .................................... 280 ◦ Adding the Widget ........................................................................... 280 ◦ Loading Content Via a URL .............................................................. 281 ◦ Links and Redirects ........................................................................... 283 ◦ Supporting JavaScript ........................................................................ 283 ◦ Alternatives for Loading Content ..................................................... 284 ◦ Listening for Events ........................................................................... 285 ◦ Addressing the Link/Redirect Behavior .......................................... 288 ◦ Visit the Trails! .................................................................................. 290 • Defining and Using Styles ◦ Styles: DIY DRY .................................................................................. 291 ◦ Elements of Style ............................................................................... 293 iv ◦ Themes: Would a Style By Any Other Name… ............................... 296 ◦ What Happens If You Have No Theme ............................................ 297 ◦ Android Studio’s Theme Editor ........................................................ 297 • Dependencies ◦ What’s a Dependency? ............................................................ 303 ◦ Dependency Scopes ................................................................ 304 ◦ Depending on a Local JAR ...................................................... 305 ◦ What’s an Artifact? ................................................................. 306 ◦ Artifacts and Repositories ...................................................... 306 ◦ Major Library Families from Google ...................................... 306 ◦ Requesting Dependencies ...................................................... 307 ◦ The Android Support Library .................................................. 310 • Tutorial #6 - Adding a Library ◦ Step #1: Getting Rid of Existing Cruft ...................................... 315 ◦ Step #2: Requesting New Dependencies .......................................... 316 ◦ In Our Next Episode… ....................................................................... 318 • Introducing ConstraintLayout ◦ Why Another Container? .................................................................. 319 ◦ Comparing with the Classics ............................................................. 321 ◦ Getting ConstraintLayout ....................................................... 322 ◦ Using Widgets and Containers from Libraries ................................ 323 ◦ Using a ConstraintLayout ....................................................... 323 ◦ Converting Existing Layouts ............................................................. 339 ◦ Visit the Trails! ................................................................................... 341 • The Action Bar ◦ Bar Hopping ....................................................................................... 343 ◦ Yet Another History Lesson .............................................................. 349 ◦ Your Action Bar Options ................................................................... 350 ◦ Setting the Target .............................................................................. 352 ◦ Defining the Resource ....................................................................... 353 ◦ Applying the Resource ...................................................................... 356 ◦ Responding to Events ........................................................................ 356 ◦ The Rest of the Sample Activity ....................................................... 357 ◦ MENU Key, We Hardly Knew Ye ...................................................... 363 ◦ Action Bars, Live in Living Color! ........................................... 364 ◦ Visit the Trails! ................................................................................... 374 • Vector Drawables ◦ Getting the Artwork................................................................. 375 ◦ VectorDrawableCompat ......................................................... 380 ◦ Other VectorDrawable Backports ..................................................... 388 • Tutorial #7 - Setting Up the Action Bar v ◦ Step #1: Adding Some Icons .............................................................. 389 ◦ Step #2: Defining Some Options ...................................................... 391 ◦ Step #3: Loading and Responding to Our Options ................. 394 ◦ Step #4: Supporting Older Devices .................................................. 395 ◦ Step #5: Trying It Out .............................................................. 396 ◦ In Our Next Episode… ....................................................................... 398 • Android’s Process Model ◦ When Processes Are Created ............................................................ 399 ◦ BACK, HOME, and Your Process ..................................................... 400 ◦ Termination ........................................................................................ 401 ◦ Foreground Means “I Love You” ....................................................... 402 ◦ You and Your Heap ............................................................................ 402 • Activities and Their Lifecycles ◦ Creating Your Second (and Third and…) Activity .......................... 404 ◦ Warning! Contains Explicit Intents! ................................................ 408 ◦ Using Implicit Intents ....................................................................... 410 ◦ Extra! Extra! ........................................................................................ 416 ◦ Pondering Parcelable ......................................................................... 417 ◦ Asynchronicity and Results .............................................................. 419 ◦ Schroedinger’s Activity ...................................................................... 419 ◦ Life, Death, and Your Activity .......................................................... 420 ◦ When Activities Die .......................................................................... 422 ◦ Walking Through the Lifecycle ................................................423 ◦ Recycling Activities ........................................................................... 426 ◦ Application: Transcending the Activity ........................................... 427 ◦ The Case of the Invisible Activity ..................................................... 428 • Tutorial #8 - Setting Up An Activity ◦ Step #1: Creating the Stub Activity Class and Manifest Entry 431 ◦ Step #2: Launching Our Activity ...................................................... 433 ◦ In Our Next Episode… ....................................................................... 434 • The Tactics of Fragments ◦ The Six Questions .............................................................................. 435 ◦ Where You Get Your Fragments From .................................... 438 ◦ Your First Fragment ........................................................................... 438 ◦ The Fragment Lifecycle Methods ..................................................... 442 ◦ Your First Dynamic Fragment .......................................................... 444 ◦ Fragments and the Action Bar ......................................................... 446 ◦ Fragments Within Fragments: Just Say “Maybe” ............................ 448 ◦ Fragments and Multiple Activities .................................................. 449 • Tutorial #9 - Starting Our Fragments ◦ Step #1: Create a SimpleContentFragment ...................................... 451 vi ◦ Step #2: Examining SimpleContentFragment ................................. 453 ◦ In Our Next Episode… ....................................................................... 454 • Swiping with ViewPager ◦ Pieces of a Pager ................................................................................. 455 ◦ Paging Fragments ................................................................... 456 ◦ Paging Other Stuff ............................................................................. 461 ◦ Indicators ........................................................................................... 462 ◦ Revisiting the Containers Sampler ................................................... 465 • Tutorial #10 - Rigging Up a ViewPager ◦ Step #1: Add a ViewPager to the Layout ........................................... 473 ◦ Step #2: Creating a ContentsAdapter ............................................... 474 ◦ Step #3: Setting Up the ViewPager ................................................... 475 ◦ In Our Next Episode… ....................................................................... 478 • Resource Sets and Configurations ◦ What’s a Configuration? And How Do They Change? .................... 479 ◦ Configurations and Resource Sets ................................................... 480 ◦ Screen Size and Orientation ............................................................. 481 ◦ Coping with Complexity .................................................................. 484 ◦ Choosing The Right Resource ........................................................... 485 ◦ API-Versioned Resources ................................................................. 489 ◦ Default Change Behavior .................................................................. 491 ◦ State Saving Scenarios ....................................................................... 493 ◦ Your Options for Configuration Changes ....................................... 494 ◦ Blocking Rotations ............................................................................ 508 ◦ And Now, a Word From the Android Project View ........................ 509 ◦ Configuration Challenges .................................................................. 510 • Material Design Basics ◦ Your App, in Technicolor! .................................................................. 515 • Dealing with Threads ◦ The Main Application Thread ........................................................... 521 ◦ Getting to the Background ................................................................ 523 ◦ Asyncing Feeling ................................................................................ 523 ◦ Alternatives to AsyncTask ................................................................. 532 ◦ And Now, The Caveats ....................................................................... 534 ◦ Event Buses .............................................................................. 535 ◦ Visit the Trails! ................................................................................... 543 • Requesting Permissions ◦ Frequently-Asked Questions About Permissions ........................... 546 ◦ Characteristics of Permissions ........................................................... 551 ◦ New Permissions in Old Applications .............................................. 554 ◦ Android 6.0+ Runtime Permission System ...................................... 555 vii