ebook img

The Busy Coder’s Guide to Android Development, Version 7.5 PDF

3859 Pages·2016·64.88 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The Busy Coder’s Guide to Android Development, Version 7.5

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: July 2016: Version 7.5 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.5? ...................................................... 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 ..................................................................................... 11 • Choosing Your Development Toolchain ◦ Android Studio ........................................................................... 13 ◦ Eclipse .................................................................................................... 13 ◦ IntelliJ IDEA ......................................................................................... 14 ◦ Command-Line Builds via Gradle for Android ..........................14 ◦ Yet Other Alternatives ...............................................................14 ◦ IDEs… And This Book ........................................................................... 15 ◦ What We Are Not Covering ................................................................. 15 • Tutorial #1 - Installing the Tools ◦ Step #1 - Checking Your Hardware Requirements ............................. 17 ◦ Step #2 - Setting Up Java and 32-Bit Linux Support .................. 18 ◦ Step #3 - Install the Developer Tools ........................................ 18 ◦ Step #4 - Install the SDKs and Add-Ons ............................................ 19 ◦ In Our Next Episode… ......................................................................... 29 • Android and Projects ◦ Common Concepts ............................................................................... 31 ◦ Projects and Android Studio ............................................................... 32 ◦ Starter Project Generators ................................................................... 39 • Tutorial #2 - Creating a Stub Project i ◦ About Our Tutorial Project ................................................................. 41 ◦ About the Rest of the Tutorials ........................................................... 42 ◦ About Our Tools .................................................................................. 42 ◦ Step #1: Creating the Project ............................................................... 42 ◦ Step #2 - Set Up the Emulator ............................................................ 45 ◦ Step #3 - Set Up the Device ................................................................. 54 ◦ Step #4: Running the Project .............................................................. 57 ◦ In Our Next Episode… ......................................................................... 59 • Getting Around Android Studio ◦ Navigating The Project Explorer ......................................................... 61 ◦ Running Projects ................................................................................ 64 ◦ Viewing Output .................................................................................. 66 ◦ Accessing Android Tools .................................................................... 68 ◦ Android Studio and Release Channels ............................................... 71 ◦ Visit the Trails! ..................................................................................... 72 • Contents of Android Projects ◦ What You Get, In General ......................................................... 73 ◦ The Contents of an Android Studio Project ............................. 75 ◦ The Contents of an Eclipse-Style Project ........................................... 77 ◦ What You Get Out Of It ...................................................................... 78 • Introducing Gradle and the Manifest ◦ Gradle: The Big Questions .................................................................. 79 ◦ Obtaining Gradle ................................................................................. 82 ◦ Versions of Gradle and the Android Plugin for Gradle .................... 84 ◦ Gradle Environment Variables ............................................................ 85 ◦ Examining the Gradle Files ...................................................... 85 ◦ Introducing the Manifest ......................................................... 88 ◦ Things In Common Between the Manifest and Gradle ................... 88 ◦ Other Gradle Items of Note ................................................................ 92 ◦ Where’s the GUI? ................................................................................. 93 ◦ The Rest of the Manifest ..................................................................... 93 ◦ Learning More About Gradle ............................................................. 96 ◦ Visit the Trails! .................................................................................... 96 • Tutorial #3 - Changing Our Manifest (and Gradle File) ◦ Some Notes About Relative Paths ...................................................... 97 ◦ Step #1: Supporting Screens ............................................................... 98 ◦ Step #2: Adding our Minimum and Target SDK Versions ............... 98 ◦ In Our Next Episode… ........................................................................ 99 • Some Words About Resources ◦ String Theory ..................................................................................... 102 ◦ Got the Picture? .......................................................................104 ii ◦ Dimensions ........................................................................................ 108 ◦ The Resource That Shall Not Be Named… Yet ................................. 109 • Tutorial #4 - Adjusting Our Resources ◦ Step #1: Changing the Name ............................................................... 111 ◦ Step #2: Changing the Icon ................................................................ 112 ◦ Step #3: Running the Result ............................................................... 115 ◦ In Our Next Episode… ........................................................................ 116 • The Theory of Widgets ◦ What Are Widgets? ............................................................................ 117 ◦ Size, Margins, and Padding ................................................................ 119 ◦ What Are Containers? ........................................................................ 119 ◦ The Absolute Positioning Anti-Pattern ............................................ 120 ◦ The Theme of This Section: Themes ................................................. 121 • The Android User Interface ◦ The Activity ......................................................................................... 125 ◦ Dissecting the Activity ...................................................................... 126 ◦ Using XML-Based Layouts ....................................................... 127 • Basic Widgets ◦ Common Concepts ............................................................................. 131 ◦ Assigning Labels ...................................................................... 134 ◦ A Commanding Button ........................................................... 140 ◦ Fleeting Images ....................................................................... 144 ◦ Fields of Green. Or Other Colors. .................................................... 149 ◦ More Common Concepts ................................................................... 153 ◦ Visit the Trails! ................................................................................... 156 • Debugging Your App ◦ Get Thee To a Stack Trace ................................................................. 158 ◦ The Case of the Confounding Class Cast ......................................... 159 ◦ Point Break ......................................................................................... 160 • LinearLayout and the Box Model ◦ Concepts and Properties .................................................................... 161 ◦ Android Studio Graphical Layout Editor ......................................... 165 • Other Common Widgets and Containers ◦ Just a Box to Check ............................................................................ 167 ◦ Don’t Like Checkboxes? How About Toggles or Switches? ............. 172 ◦ Turn the Radio Up .............................................................................. 177 ◦ All Things Are Relative ...................................................................... 180 ◦ Tabula Rasa ........................................................................................ 188 ◦ Scrollwork ........................................................................................... 194 ◦ Making Progress with ProgressBars ................................................. 197 ◦ Visit the Trails! ................................................................................... 198 iii • Tutorial #5 - Making Progress ◦ Step #1: Removing The “Hello, World” ............................................. 199 ◦ Step #2: Adding a ProgressBar .......................................................... 202 ◦ Step #3: Seeing the Results ............................................................... 203 ◦ In Our Next Episode… ....................................................................... 203 • GUI Building, Continued ◦ Making Your Selection ...................................................................... 205 ◦ Including Includes ............................................................................ 206 ◦ Morphing Widgets ............................................................................ 208 ◦ Preview of Coming Attractions ........................................................ 208 • AdapterViews and Adapters ◦ Adapting to the Circumstances ....................................................... 209 ◦ Lists of Naughty and Nice .................................................................. 211 ◦ Clicks versus Selections ...................................................................... 213 ◦ Spin Control ........................................................................................ 217 ◦ Grid Your Lions (Or Something Like That…) .................................. 220 ◦ Fields: Now With 35% Less Typing! ................................................. 223 ◦ Customizing the Adapter .................................................................. 227 ◦ Visit the Trails! ................................................................................... 235 • The WebView Widget ◦ Role of WebView ............................................................................... 237 ◦ Daddy, Where Do WebViews Come From? ...............................238 ◦ Adding the Widget ............................................................................ 238 ◦ Loading Content Via a URL .............................................................. 239 ◦ Links and Redirects ........................................................................... 241 ◦ Supporting JavaScript ........................................................................ 241 ◦ Alternatives for Loading Content ..................................................... 242 ◦ Listening for Events ........................................................................... 243 ◦ Addressing the Link/Redirect Behavior ................................. 246 ◦ Visit the Trails! ................................................................................... 247 • Defining and Using Styles ◦ Styles: DIY DRY .................................................................................. 249 ◦ Elements of Style ................................................................................ 251 ◦ Themes: Would a Style By Any Other Name… ................................ 254 ◦ What Happens If You Have No Theme ............................................ 254 ◦ Android Studio’s Theme Editor ........................................................ 255 • JARs and Library Projects ◦ The Dalvik VM, and a Bit of ART ..................................................... 262 ◦ Getting the Library ............................................................................ 262 ◦ The Outer Limits ............................................................................... 264 ◦ JAR Dependency Management ......................................................... 264 iv ◦ OK, So What is a Library Project? .................................................... 265 ◦ Using a Library Project ...................................................................... 265 ◦ Library Projects: What You Get ....................................................... 266 ◦ The Android Support Package ................................................ 266 • 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 ◦ Life, Death, and Your Activity ............................................................ 331 v ◦ 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 ◦ API-Versioned Resources .................................................................. 391 vi ◦ 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 .......................................................... 417 ◦ Getting to the Background ................................................................ 419 ◦ Asyncing Feeling ................................................................................ 419 ◦ Alternatives to AsyncTask ................................................................. 428 ◦ And Now, The Caveats ....................................................................... 429 ◦ Event Buses ............................................................................. 430 ◦ Visit the Trails! ................................................................................... 439 • Requesting Permissions ◦ Frequently-Asked Questions About Permissions ............................ 442 ◦ Characteristics of Permissions .......................................................... 447 ◦ New Permissions in Old Applications .............................................. 450 ◦ Android 6.0+ Runtime Permission System ...................................... 451 • Tutorial: Runtime Permission Support ◦ Step #0: Install the Android 6.0 SDK .............................................. 466 ◦ Step #1: Import and Review the Starter Project .............................. 467 ◦ Step #2: Update Gradle for Android 6.0 ........................................... 473 ◦ Step #3: Review the Planned UX ...................................................... 476 ◦ Step #4: Detect the First Run ............................................................ 477 ◦ Step #5: On First Run, Ask For Permissions .................................... 478 ◦ Step #6: Check for Permissions Before Taking a Picture ................ 482 ◦ Step #7: Detect If We Should Show Some Rationale ...................... 483 ◦ Step #8: Add a Rationale UI and Re-Request Permissions ............. 485 ◦ Step #9: Check for Permissions Before Recording a Video ............. 491 ◦ Step #10: Detect If We Should Show Some Rationale (Again) ....... 492 ◦ Step #11: Support Configuration Changes ....................................... 494 • Assets, Files, and Data Parsing ◦ Packaging Files with Your App ......................................................... 503 ◦ Files and Android .............................................................................. 505 ◦ Working with Internal Storage ........................................................ 506 ◦ Working with External Storage ......................................................... 514 ◦ Multiple User Accounts ..................................................................... 520 ◦ Linux Filesystems: You Sync, You Win .............................................. 521 ◦ StrictMode: Avoiding Janky Code ..................................................... 523 vii

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.