ebook img

The Busy Coder’s Guide To Android Development (Final Version) PDF

4293 Pages·2019·76.17 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 (Final Version)

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-2019 CommonsWare, LLC. All Rights Reserved. Printed in the United States of America. Printing History: February 2019 FINAL Version 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! ...................................................................... xliii ◦ First-Generation Book ...................................................................... xliii ◦ The Book’s Structure ......................................................................... xliv ◦ The Trails ............................................................................................. xlv ◦ About the Updates ................................................................................. l ◦ About the APK Edition .......................................................................... l ◦ Source Code and Its License ................................................................ li ◦ Creative Commons and the Four-to-Free (42F) Guarantee ............... li ◦ 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 ........................................................... 18 ◦ Step #4: Install the SDKs and Add-Ons ............................................. 19 ◦ In Our Next Episode… ......................................................................... 27 • Android and Projects ◦ Projects and Android Studio ............................................................... 29 • Tutorial #2 - Creating a Stub Project ◦ About Our Tutorial Project ................................................................. 37 ◦ About the Rest of the Tutorials ........................................................... 38 ◦ About Our Tools .................................................................................. 38 i ◦ Step #1: Importing the Project ............................................................ 38 ◦ Step #2: Get Ready for the x86 Emulator ........................................... 43 ◦ Step #3: Set Up the AVD ..................................................................... 44 ◦ Step #4: Set Up the Device .................................................................. 51 ◦ Step #5: Running the Project .............................................................. 56 ◦ In Our Next Episode… ......................................................................... 58 • Getting Around Android Studio ◦ Navigating The Project Explorer ......................................................... 59 ◦ Running Projects ................................................................................. 62 ◦ Viewing Output ................................................................................... 63 ◦ Accessing Android Tools ..................................................................... 65 ◦ Android Studio and Release Channels .............................................. 66 ◦ Visit the Trails! .................................................................................... 68 • Contents of Android Projects ◦ What You Get, In General .................................................................. 69 ◦ More About the Directory Structure .................................................. 72 ◦ What You Get Out Of It ...................................................................... 74 • Introducing Gradle and the Manifest ◦ Gradle: The Big Questions .................................................................. 75 ◦ Obtaining Gradle ................................................................................. 77 ◦ Versions of Gradle and the Android Gradle Plugin ........................... 79 ◦ Gradle Environment Variables ........................................................... 80 ◦ Examining the Gradle Files ................................................................ 80 ◦ Introducing the Manifest .................................................................... 83 ◦ Things In Common Between the Manifest and Gradle ................... 84 ◦ Other Gradle Items of Note ............................................................... 86 ◦ Where’s the GUI? ................................................................................. 87 ◦ The Rest of the Manifest ..................................................................... 87 ◦ Learning More About Gradle ............................................................. 89 ◦ Visit the Trails! .................................................................................... 90 • Tutorial #3 - Manifest Changes ◦ Some Notes About Relative Paths ...................................................... 91 ◦ Step #1: Supporting Screens ................................................................ 91 ◦ Step #2: Blocking Backups .................................................................. 92 ◦ Step #3: Ignoring Lint .......................................................................... 92 ◦ In Our Next Episode… ........................................................................ 94 • Some Words About Resources ◦ String Theory ....................................................................................... 95 ◦ Got the Picture? ................................................................................. 102 ◦ Dimensions ........................................................................................ 105 ◦ The Resource That Shall Not Be Named… Yet ................................. 107 ii • Icons ◦ App Icons… And Everything Else ...................................................... 109 ◦ Creating an App Icon with the Asset Studio .................................... 110 ◦ Creating Other Icons with the Asset Studio ..................................... 116 • Tutorial #4 - Adjusting Our Resources ◦ Step #1: Changing the Name .............................................................. 117 ◦ Step #2: Changing the Icon ................................................................ 118 ◦ Step #3: Running the Result ............................................................... 123 ◦ In Our Next Episode… ........................................................................ 123 • The Theory of Widgets ◦ What Are Widgets? ............................................................................ 125 ◦ Size, Margins, and Padding ................................................................ 127 ◦ What Are Containers? ........................................................................ 127 ◦ The Absolute Positioning Anti-Pattern ............................................ 128 ◦ The Theme of This Section: Themes ................................................ 129 • The Android User Interface ◦ The Activity ......................................................................................... 135 ◦ Dissecting the Activity ...................................................................... 136 ◦ Using XML-Based Layouts ................................................................. 137 • Basic Widgets ◦ Common Concepts ............................................................................. 141 ◦ Introducing the Graphical Layout Editor ........................................ 144 ◦ And Now, Some Notes About the Book’s Sample Projects .............. 153 ◦ Assigning Labels ................................................................................ 154 ◦ A Commanding Button ..................................................................... 159 ◦ Fleeting Images .................................................................................. 162 ◦ Fields of Green. Or Other Colors. .................................................... 168 ◦ More Common Concepts ................................................................... 171 ◦ Visit the Trails! .................................................................................... 174 • Debugging Your App ◦ Get Thee To a Stack Trace ................................................................. 176 ◦ The Case of the Confounding Class Cast .......................................... 177 ◦ Point Break ......................................................................................... 178 • The Classic Container Classes ◦ Introducing the Sampler App ........................................................... 179 ◦ RTL and Your Layouts ....................................................................... 180 ◦ LinearLayout and the Box Model ...................................................... 181 ◦ All Things Are Relative ...................................................................... 207 ◦ Tabula Rasa ......................................................................................... 217 ◦ Hey, What About ConstraintLayout? ............................................... 223 ◦ Turning Back to RTL .......................................................................... 224 iii • Other Common Widgets and Containers ◦ Just a Box to Check ............................................................................ 227 ◦ Don’t Like Checkboxes? How About Toggles or Switches? ............. 231 ◦ Turn the Radio Up ............................................................................. 235 ◦ Scrollwork ........................................................................................... 238 ◦ Making Progress with ProgressBars ................................................. 242 ◦ Framing the Scene ............................................................................. 242 ◦ Visit the Trails! ................................................................................... 243 • Tutorial #5 - Creating a Layout ◦ Step #1: Creating a New Layout Resource ........................................ 245 ◦ Step #2: Defining the UI ................................................................... 246 ◦ In Our Next Episode… ....................................................................... 250 • GUI Building, Continued ◦ Making Your Selection ...................................................................... 252 ◦ Including Includes ............................................................................. 252 ◦ Preview of Coming Attractions ......................................................... 254 • AdapterViews and Adapters ◦ Adapting to the Circumstances ........................................................ 256 ◦ Lists of Naughty and Nice ................................................................. 257 ◦ Clicks versus Selections .................................................................... 260 ◦ Spin Control ....................................................................................... 263 ◦ Grid Your Lions (Or Something Like That…) .................................. 267 ◦ Fields: Now With 35% Less Typing! ................................................. 272 ◦ Customizing the Adapter .................................................................. 277 ◦ Visit the Trails! ................................................................................... 285 • The WebView Widget ◦ Role of WebView ............................................................................... 287 ◦ Daddy, Where Do WebViews Come From? .................................... 288 ◦ Adding the Widget ........................................................................... 288 ◦ Loading Content Via a URL ............................................................. 289 ◦ Links and Redirects ........................................................................... 291 ◦ Supporting JavaScript ........................................................................ 291 ◦ Alternatives for Loading Content ..................................................... 292 ◦ Listening for Events ........................................................................... 293 ◦ Addressing the Link/Redirect Behavior .......................................... 296 ◦ Opting Out of Google Monitoring .................................................. 298 ◦ Visit the Trails! .................................................................................. 299 • Defining and Using Styles ◦ Styles: DIY DRY .................................................................................. 301 ◦ Elements of Style ............................................................................... 303 ◦ Themes: Would a Style By Any Other Name… ................................ 306 iv ◦ What Happens If You Have No Theme ............................................ 306 ◦ Android Studio’s Theme Editor ........................................................ 307 • Dependencies ◦ What’s a Dependency? ....................................................................... 313 ◦ Dependency Scopes ........................................................................... 314 ◦ Depending on a Local JAR ................................................................. 315 ◦ What’s an Artifact? ............................................................................ 316 ◦ Artifacts and Repositories ................................................................. 316 ◦ Major Library Families from Google ................................................ 316 ◦ Requesting Dependencies .................................................................. 317 ◦ The Android Support Library ........................................................... 320 • Tutorial #6 - Adding a Library ◦ Step #1: Getting Rid of Existing Cruft ............................................... 325 ◦ Step #2: Requesting New Dependencies .......................................... 326 ◦ In Our Next Episode… ....................................................................... 328 • Introducing ConstraintLayout ◦ Why Another Container? .................................................................. 329 ◦ Comparing with the Classics ............................................................. 331 ◦ Getting ConstraintLayout .................................................................. 331 ◦ Using Widgets and Containers from Libraries ................................ 332 ◦ Using a ConstraintLayout ................................................................. 332 ◦ Converting Existing Layouts ............................................................. 346 ◦ Visit the Trails! ................................................................................... 348 • RecyclerView ◦ AdapterView and its Discontents ..................................................... 349 ◦ Enter RecyclerView ............................................................................ 350 ◦ A Trivial List ........................................................................................ 351 ◦ Divider Options ................................................................................. 359 ◦ Handling Click Events ....................................................................... 366 ◦ Visit the Trails! ................................................................................... 373 • The Action Bar ◦ Bar Hopping ....................................................................................... 375 ◦ Yet Another History Lesson .............................................................. 382 ◦ Your Action Bar Options ................................................................... 382 ◦ Setting the Target .............................................................................. 384 ◦ Defining the Resource ....................................................................... 386 ◦ Applying the Resource ...................................................................... 388 ◦ Responding to Events ........................................................................ 388 ◦ The Rest of the Sample Activity ....................................................... 389 ◦ MENU Key, We Hardly Knew Ye ...................................................... 396 ◦ Action Bars, Live in Living Color! ..................................................... 397 v ◦ Visit the Trails! .................................................................................. 407 • Vector Drawables ◦ Getting the Artwork ......................................................................... 409 ◦ VectorDrawableCompat .................................................................... 414 • Tutorial #7 - Setting Up the Action Bar ◦ Step #1: Adding Some Icons .............................................................. 421 ◦ Step #2: Defining Some Options ...................................................... 423 ◦ Step #3: Loading and Responding to Our Options ........................ 426 ◦ Step #4: Supporting Older Devices .................................................. 427 ◦ Step #5: Trying It Out ........................................................................ 429 ◦ In Our Next Episode… ....................................................................... 430 • Android’s Process Model ◦ When Processes Are Created ............................................................ 431 ◦ BACK, HOME, and Your Process ...................................................... 432 ◦ Termination ........................................................................................ 433 ◦ Foreground Means “I Love You” ....................................................... 434 ◦ You and Your Heap ............................................................................ 434 • Activities and Their Lifecycles ◦ Creating Your Second (and Third and…) Activity ........................... 436 ◦ Warning! Contains Explicit Intents! ................................................ 440 ◦ Using Implicit Intents ....................................................................... 442 ◦ Extra! Extra! ....................................................................................... 448 ◦ Pondering Parcelable ......................................................................... 450 ◦ Asynchronicity and Results .............................................................. 451 ◦ Schroedinger’s Activity ...................................................................... 451 ◦ Life, Death, and Your Activity ........................................................... 452 ◦ When Activities Die .......................................................................... 454 ◦ Walking Through the Lifecycle ........................................................ 455 ◦ Recycling Activities ............................................................................ 458 ◦ Application: Transcending the Activity ........................................... 459 ◦ The Case of the Invisible Activity .................................................... 460 • Tutorial #8 - Setting Up An Activity ◦ Step #1: Creating the Stub Activity Class and Manifest Entry ........ 463 ◦ Step #2: Launching Our Activity ...................................................... 465 ◦ In Our Next Episode… ...................................................................... 466 • The Tactics of Fragments ◦ The Six Questions ............................................................................. 467 ◦ Where You Get Your Fragments From ............................................. 470 ◦ Your First Fragment ........................................................................... 471 ◦ The Fragment Lifecycle Methods ..................................................... 475 ◦ Your First Dynamic Fragment ........................................................... 477 vi ◦ Fragments and the Action Bar ......................................................... 480 • Tutorial #9 - Starting Our Fragments ◦ Step #1: Create a SimpleContentFragment ...................................... 483 ◦ Step #2: Examining SimpleContentFragment ................................. 485 ◦ In Our Next Episode… ...................................................................... 486 • Swiping with ViewPager ◦ Pieces of a Pager ................................................................................. 487 ◦ Paging Fragments ............................................................................. 488 ◦ Paging Other Stuff ............................................................................. 493 ◦ Indicators .......................................................................................... 494 ◦ Revisiting the Containers Sampler ................................................... 497 • Tutorial #10 - Rigging Up a ViewPager ◦ Step #1: Add a ViewPager to the Layout ........................................... 505 ◦ Step #2: Creating a ContentsAdapter .............................................. 506 ◦ Step #3: Setting Up the ViewPager ................................................... 507 ◦ In Our Next Episode… ....................................................................... 510 • Resource Sets and Configurations ◦ What’s a Configuration? And How Do They Change? ..................... 511 ◦ Configurations and Resource Sets ..................................................... 512 ◦ Screen Size and Orientation .............................................................. 513 ◦ Coping with Complexity ................................................................... 516 ◦ Choosing The Right Resource ............................................................ 517 ◦ API-Versioned Resources ................................................................... 521 ◦ Default Change Behavior .................................................................. 523 ◦ State Saving Scenarios ....................................................................... 525 ◦ Your Options for Configuration Changes ........................................ 526 ◦ Blocking Rotations ............................................................................ 540 ◦ And Now, a Word From the Android Project View ......................... 541 ◦ Configuration Challenges .................................................................. 543 • Material Design Basics ◦ Your App, in Technicolor! ................................................................. 549 • Dealing with Threads ◦ The Main Application Thread .......................................................... 555 ◦ Getting to the Background ................................................................ 557 ◦ Asyncing Feeling ................................................................................ 557 ◦ Alternatives to AsyncTask ................................................................. 567 ◦ And Now, The Caveats ...................................................................... 569 ◦ Event Buses ....................................................................................... 569 ◦ Visit the Trails! ................................................................................... 578 • Requesting Permissions ◦ Frequently-Asked Questions About Permissions ............................ 580 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.