Table of Contents
Introduction 1
 How to Use This Book 1
 Conventions Used in This Book 2
 Foolish Assumptions 2
 Icons Used in This Book 3
 Beyond the Book 4
 Where to Go from Here 4
 Book 1: Getting Started with Android Application Development 5
 Chapter 1: All about Android 7
 The Consumer Perspective 8
 The Versions of Android 9
 The Developer Perspective 12
 Java and Kotlin 12
 XML 14
 Linux 16
 The Business Perspective 18
 Chapter 2: Installing the Software Tools 19
 Setting Up the Software 20
 Considering the requirements 20
 Downloading the software 21
 Installing Android Studio 23
 Installing offline tools 25
 Launching the Android Studio IDE 28
 In Windows 29
 On a Mac 29
 In Linux 30
 In Chrome OS 30
 Using the Android Studio Setup Wizard 30
 Fattening Up the Android SDK 32
 The more things stay the same, the more they change 32
 Installing new versions (and older versions) of Android 33
 Creating an Android virtual device 35
 A third-party emulator 39
 Chapter 3: Creating an Android App 43
 Creating Your First App 43
 Starting the IDE and creating your first app 45
 Launching your first app 50
 Running Your App 52
 You Can Download All the Code 55
 Troubleshooting Common IDE Errors 58
 Error message: Failed to find target 58
 Error running ‘app’: No target device found 58
 Error message: Android Virtual Device may be incompatible with your configuration 58
 You lose contact with the Android Debug Bridge (ADB) 59
 You don’t like whatever AVD opens automatically 59
 The emulator stalls during startup 60
 Error message: The user data image is used by another emulator 61
 Error message: Unknown virtual device name 63
 The emulator displays a “process isn’t responding” dialog box 63
 Changes to your app don’t appear in the emulator 64
 Testing Apps on a Real Device 64
 Chapter 4: Examining a Basic Android App 67
 A Project’s Files 68
 The MainActivity.kt file 71
 The onCreate() method 72
 Using other templates 73
 The res Branch 76
 The res/drawable branch 77
 The res/layout branch 77
 The res/menu branch 78
 The res/mipmap branch 79
 The res/values branch 80
 Other Files in an Android Project 82
 The build.gradle file 82
 The AndroidManifest.xml file 85
 The R.java file 87
 The assets folder 89
 The android.jar archive 90
 The APK file 91
 What Did I Agree To? 93
 What’s in a name? 93
 Choosing a language 95
 Your app’s API levels 95
 Chapter 5: Conjuring and Embellishing an Android App 101
 Dragging, Dropping, and Otherwise Tweaking an App 102
 Creating the “look” 102
 Coding the behavior 112
 A Bit of Debugging 118
 Try it! 118
 Discovering the secrets of Logcat 123
 Using the debugger 126
 Chapter 6: Improving Your App 131
 Improving the Layout 131
 Changing the layout 132
 Creating a reusable layout 139
 Reusing a layout 142
 Starting Another Activity 145
 Localizing Your App 151
 Responding to Check Box Events 155
 Displaying Images 157
 Sending in Your Order 162
 Book 2: Android Background Material 167
 Chapter 1: Using Android Studio 169
 Good to Know versus Need to Know 170
 Getting a Feel for the Big Picture 171
 The main window 173
 Viewing modes 179
 The Designer tool 181
 Discovering What You Can Do 184
 Finding things 185
 Fixing code 190
 Refactoring 199
 Chapter 2: Kotlin for Java Programmers 203
 Using Kotlin or Java for Development 204
 Defining the Java Issues That Kotlin Fixes 207
 Improving control over null references 207
 Removing raw data types 210
 Using invariant arrays 210
 Working with proper function types 212
 Getting rid of the checked exceptions 213
 Nothing’s Perfect: Kotlin is Missing Features, Too 214
 Considering primitive types that are not classes 214
 Losing static members 214
 Eliminating non-private fields 215
 Reducing confusion by eliminating wildcard-types 216
 Abandoning the ternary-operator a ? b : c 217
 Looking at What Kotlin Adds to the Picture 218
 Considering higher order functions and lambdas 218
 Refining object orientation using extension functions 218
 Relying on smart casts 219
 Employing string templates 220
 Understanding primary constructors 221
 Implementing first-class delegation 221
 Using ranges of values 223
 Creating data classes 224
 Overloading operators 224
 Developing asynchronous code using coroutines 225
 Chapter 3: Kotlin for Everyone 227
 Moving from Development to Execution with Kotlin 228
 What is a compiler? 228
 Understanding native code compiler or interpreter issues 230
 Considering the Android Runtime (ART) 231
 Grasping Kotlin Code 235
 Nearly everything begins with an expression 236
 The Kotlin class 238
 Classes and objects 239
 Kotlin types 240
 Performing casts 245
 The Kotlin function 249
 Objects and their constructors 252
 Classes grow on trees 254
 The Kotlin package 255
 Considering Kotlin visibility rules 257
 Defying your parent 258
 Kotlin comments 259
 Chapter 4: What Kotlin Does (and When) 261
 Making Decisions (Kotlin if Statements) 261
 Testing for equality 264
 Choosing among many alternatives (Kotlin when statements) 266
 Repeating Instructions Over and Over Again 269
 Kotlin while statements 269
 Kotlin do statements 271
 Arrays in Kotlin 273
 Kotlin’s for statements 277
 Looping using Kotlin recursion 281
 Working with break and continue 283
 Jumping Away from Trouble 284
 Working with Kotlin Collections 286
 Considering the collection types 287
 Differentiating between read-only and mutable collections 289
 Chapter 5: Object-Oriented Programming in Kotlin 291
 Static Fields and Methods 291
 Interfaces and Callbacks 294
 Event handling and callbacks 299
 An object remembers who created it 302
 A less wordy way to implement an interface 303
 Classes That Must (and Must Not) Be Extended 305
 The need to override 306
 Java’s final classes 306
 Kotlin’s open classes 307
 Kotlin extensions 307
 Abstract classes 308
 Inner Classes 310
 Named inner classes 310
 Anonymous inner classes 312
 Chapter 6: Functional Programming in Kotlin 315
 Defining Functional Programming 316
 Differences from other programming paradigms 316
 Understanding its goals 317
 Understanding Pure and Impure Languages 318
 Using the pure approach 318
 Using the impure approach 320
 Comparing the Functional Paradigm 320
 Using Kotlin for Functional Programming Needs 322
 Defining the Role of State 323
 Using Recursion to Perform Calculations 324
 Relying on standard recursion 324
 Relying on tail recursion 326
 Using Function Types 327
 Understanding Function Literals 329
 Lambda expressions 329
 Anonymous functions 330
 Defining the Function Types 331
 Comprehensions 331
 Receivers 332
 Inline 334
 Utility 335
 Using Functional Programming for Android Apps 336
 Chapter 7: A  Look at XML 339
 XML Isn’t Ordinary Text 340
 Of tags and elements 340
 Other things you find in an XML document 348
 What’s in a Namespace? 350
 The package attribute 353
 The style attribute 354
 Book 3: The Building Blocks 357
 Chapter 1: Getting an Overview of Jetpack 359
 Understanding the Benefits of Jetpack 360
 Eliminating boilerplate code 360
 Managing background tasks 361
 Navigating between activities and fragments 362
 Managing memory 364
 Performing configuration changes 365
 Considering the Jetpack Components 366
 Foundation 367
 Architecture 368
 Behavior 370
 UI 372
 Getting an Overview of the AndroidX Package 373
 Working with Lifecycle-Aware Components 374
 Focusing on activities 375
 Understanding events and states 376
 Chapter 2: Building a Foundation for Your App 377
 Working with Android KTX 378
 Getting a feel for KTX features 378
 Using KTX in your project 381
 Considering the modules 382
 Addressing Security Issues 389
 Benchmarking Your Application 392
 Removing barriers to correct results 393
 Creating a test app 394
 Profiling your app 397
 Tracing your app 398
 Checking for benchmarking module support 400
 Benchmarking the app 401
 Testing Application Functionality 403
 Chapter 3: Creating an Architecture 405
 Managing Application Activities 405
 Defining an activity 406
 Getting an overview of intent filters 407
 Considering the activity lifecycle 407
 Understanding the backstack 409
 Working with fragments 412
 Considering the fragment lifecycle 416
 Seeing activities and fragments in action 417
 Providing for Navigational Needs 433
 Creating the navigational graph 434
 Adding a NavHostFragment to your activity 437
 Adding destinations 438
 Creating links between destinations 440
 Creating the required linkages 442
 Performing Background Tasks Using WorkManager 446
 Chapter 4: Defining an App’s Behavior 451
 Working with Notifications 452
 Understanding what notifications do 452
 Anatomy of a notification 454
 Assigning a channel to your notification 456
 Setting the notification importance 457
 Considering the notification types 458
 Relying on notification updates 459
 Do Not Disturb mode 460
 Creating a notification 460
 Getting Permission 466
 Considering permission use 467
 Configuring permissions in AndroidManifest.xml 468
 Complying with User Preferences 469
 Deciding on a preference set 470
 Setting preferences using the Preference Library 472
 Working with MediaPlayer 481
 Adding Camera Support Using CameraX 484
 Sharing with Others 487
 Performing simple share actions with other apps 487
 Using Slices 488
 Chapter 5: Interacting with the Users 491
 Creating a Great Layout 492
 Defining the View and ViewGroup elements 492
 Creating a layout using XML 493
 Modifying a layout at runtime 497
 Considering the common layouts 498
 Working with adapters 499
 Debugging your layout 500
 Employing Color and Texture 502
 Working with styles and themes 503
 Creating a palette 509
 Using swatches to create color schemes 510
 Using Animations and Transitions 510
 Understanding the need for animations 511
 Animating graphics 511
 Communicating with Emoji 514
 Keyboard emoji support 515
 Using the cut-and-paste method on standard controls 516
 Using the AndroidX approach 517
 Book 4: Programming Cool Phone Features 521
 Chapter 1: Hungry Burds: A Simple Android Game 523
 Introducing the Hungry Burds Game 523
 The Hungry Burds Project’s Files 526
 The Main Activity 528
 The Code, All the Code, and Nothing But the Code 530
 Setting Up the Game 535
 Declaring properties 535
 The onCreate Method 537
 Displaying a Burd 538
 Creating random values 538
 Creating a Burd 539
 Placing a Burd on the constraint layout 540
 Animating a Burd 542
 Handling a Touch Event 544
 Finishing Up 546
 Chapter 2: An Android Social Media App 547
 Setting Things Up on Facebook’s Developer Site 548
 A Minimal Facebook App 549
 The build.gradle file 550
 The manifest file 550
 A Bare-Bones Main Activity 551
 Enriching the Minimal App 555
 Working with a radio group 559
 Controlling the web view 562
 Who tests your Facebook app? 563
 Chapter 3: Going Native 567
 The Native Development Kit 567
 Understanding why you need the NDK 568
 Knowing what you get 569
 Getting the NDK 570
 Creating an Application 573
 Starting with the template 573
 Seeing the essential project differences 575
 Considering the build.gradle (Module: app) differences 577
 Understanding the default template differences 580
 Getting an overview of the C++ file 582
 Seeing the result 583
 Book 5: Apps for Tablets, Watches, TV Sets, and Cars 585
 Chapter 1: Apps for Tablets 587
 Gaining Perspective 588
 Creating the right devices 589
 Running code on multiple devices 593
 Copying the project 594
 Seeing presentation differences 596
 Developing a Nested Navigational Graph 603
 Understanding the uses for nested navigational graphs 603
 Developing an app design 604
 Considering the content needs 608
 Creating a Responsive App 612
 Chapter 2: Developing for Android Wear 615
 Seeing Where Wearables Are Used 615
 Setting Up Your Testing Environment 617
 Creating the project 617
 Configuring a wearable device emulator 620
 Other testing configurations 624
 Wearable Apps: What’s the Big Deal? 625
 Case Study: A Watch Face 626
 Defining the watch face project 627
 Testing the watch face app 628
 Dissecting the skeletal watch face project 631
 Enhancing the skeletal watch face project 634
 Chapter 3: Developing for Android TV 637
 Getting Started 638
 Running the Skeletal App 641
 Dissecting the TV App 644
 Adding to the standard AndroidManifest.xml 644
 Looking into build.gradle (Module: app) 645
 Defining a layout 646
 The adapter and the presenter 647
 Using the Adapter class 648
 Using the Presenter class 650
 Chapter 4: Developing for Android Auto 653
 Checking Auto Compatibility 654
 Choosing the Google Play Services 656
 Considering Notification Limits 658
 Creating an Emulator 660
 Configuring your car for development 661
 Defining an emulator 662
 Developing an Android Auto App 670
 Creating the project 670
 Viewing the project configuration 672
 Performing required configuration tasks 674
 Touring the Media Service app 675
 Book 6: The Job Isn’t Done Until 679
 Chapter 1: Publishing Your App to the Google Play Store 681
 Creating a Google Play Developer Account 681
 Preparing Your Code 682
 Un-testing the app 683
 Choosing Android versions 683
 Setting your app’s own version code and version name 684
 Choosing a package name 685
 Preparing Graphic Assets for the Play Store 685
 Creating an icon 686
 Creating screenshots 688
 Providing other visual assets 690
 Creating a Publishable File 691
 Differences among builds 692
 Creating the release build 697
 Running a new APK file 702
 Running the app in a new AAB file 703
 Another way to build and run an AAB file 705
 Publishing Your App 708
 The App Releases page 708
 The Store Listing page 710
 The App Signing page 711
 Other pages 711
 Leave No Stone Unturned 714
 Publishing Elsewhere 714
 The Amazon Appstore 714
 Other venues 715
 Chapter 2: Monetizing and Marketing Your App 717
 Choosing a Revenue Model 718
 Charging for your app 719
 Offering an extended free trial 723
 Freemium apps 724
 Selling things with your app 726
 Subscription pricing 729
 Earning revenue from advertising 729
 Variations on in-app advertising 731
 Donationware 732
 Offering your app for free 732
 Getting paid to develop apps for others 732
 Marketing Your Application 733
 Brick Breaker Master: An App Marketing Case Study 734
 Chapter 3: Creating Public Support for Your App 739
 Obtaining Support through Patreon 740
 Discovering that patronage isn’t new 740
 Considering crowdfunding 741
 Defining why you should use crowdfunding 741
 Understanding the development angle 742
 Determining the trade-offs 744
 Developing Your Own Distribution Stream 744
 Creating podcasts 744
 Developing YouTube videos 746
 Employing social media 748
 Answering questions 750
 Taking the Personal Approach 750
 Creating a blog 751
 Answering your email 752
 Considering App Store Alternatives 754
 Getting Awards 756
 Looking for awards in all the right places 757
 Strutting your stuff 757
 Index 759