1. Introduction....28
1.1 Downloading the Code Samples....28
1.2 Feedback....28
1.3 Errata....29
2. Setting up an Android Studio Development Environment....30
2.1 System requirements....30
2.2 Downloading the Android Studio package....30
2.3 Installing Android Studio....31
2.3.1 Installation on Windows....31
2.3.2 Installation on macOS....31
2.3.3 Installation on Linux....32
2.4 The Android Studio setup wizard....32
2.5 Installing additional Android SDK packages....33
2.6 Installing the Android SDK Command-line Tools....36
2.6.1 Windows 8.1....37
2.6.2 Windows 10....38
2.6.3 Windows 11....38
2.6.4 Linux....38
2.6.5 macOS....38
2.7 Android Studio memory management....38
2.8 Updating Android Studio and the SDK....39
2.9 Summary....40
3. Creating an Example Android App in Android Studio....42
3.1 About the Project....42
3.2 Creating a New Android Project....42
3.3 Creating an Activity....43
3.4 Defining the Project and SDK Settings....43
3.5 Enabling the New Android Studio UI....44
3.6 Modifying the Example Application....45
3.7 Modifying the User Interface ....46
3.8 Reviewing the Layout and Resource Files....52
3.9 Adding Interaction....55
3.10 Summary....56
4. Creating an Android Virtual Device (AVD) in Android Studio....58
4.1 About Android Virtual Devices....58
4.2 Starting the Emulator....60
4.3 Running the Application in the AVD....61
4.4 Running on Multiple Devices....62
4.5 Stopping a Running Application....63
4.6 Supporting Dark Theme....63
4.7 Running the Emulator in a Separate Window....64
4.8 Enabling the Device Frame....67
4.9 Summary....68
5. Using and Configuring the Android Studio AVD Emulator ....70
5.1 The Emulator Environment....70
5.2 Emulator Toolbar Options....70
5.3 Working in Zoom Mode....72
5.4 Resizing the Emulator Window....72
5.5 Extended Control Options....72
5.5.1 Location....73
5.5.2 Displays....73
5.5.3 Cellular....73
5.5.4 Battery....73
5.5.5 Camera....73
5.5.6 Phone....73
5.5.7 Directional Pad....73
5.5.8 Microphone....73
5.5.9 Fingerprint....73
5.5.10 Virtual Sensors....74
5.5.11 Snapshots....74
5.5.12 Record and Playback....74
5.5.13 Google Play....74
5.5.14 Settings....74
5.5.15 Help....74
5.6 Working with Snapshots....74
5.7 Configuring Fingerprint Emulation....75
5.8 The Emulator in Tool Window Mode....77
5.9 Creating a Resizable Emulator....77
5.10 Summary....78
6. A Tour of the Android Studio User Interface....80
6.1 The Welcome Screen....80
6.2 The Menu Bar....81
6.3 The Main Window....81
6.4 The Tool Windows....83
6.5 The Tool Window Menus....86
6.6 Android Studio Keyboard Shortcuts....86
6.7 Switcher and Recent Files Navigation....87
6.8 Changing the Android Studio Theme....88
6.9 Summary....89
7. Testing Android Studio Apps on a Physical Android Device....90
7.1 An Overview of the Android Debug Bridge (ADB)....90
7.2 Enabling USB Debugging ADB on Android Devices....90
7.2.1 macOS ADB Configuration....91
7.2.2 Windows ADB Configuration....92
7.2.3 Linux adb Configuration....93
7.3 Resolving USB Connection Issues....93
7.4 Enabling Wireless Debugging on Android Devices....94
7.5 Testing the adb Connection....96
7.6 Device Mirroring....96
7.7 Summary....96
8. The Basics of the Android Studio Code Editor....98
8.1 The Android Studio Editor....98
8.2 Splitting the Editor Window....101
8.3 Code Completion....101
8.4 Statement Completion....103
8.5 Parameter Information....103
8.6 Parameter Name Hints....103
8.7 Code Generation....103
8.8 Code Folding....105
8.9 Quick Documentation Lookup....106
8.10 Code Reformatting....106
8.11 Finding Sample Code....107
8.12 Live Templates....107
8.13 Summary....108
9. An Overview of the Android Architecture....110
9.1 The Android Software Stack....110
9.2 The Linux Kernel....111
9.3 Android Runtime – ART....111
9.4 Android Libraries....111
9.4.1 CC Libraries....112
9.5 Application Framework....112
9.6 Applications....113
9.7 Summary....113
10. The Anatomy of an Android App....114
10.1 Android Activities....114
10.2 Android Fragments....114
10.3 Android Intents....115
10.4 Broadcast Intents....115
10.5 Broadcast Receivers....115
10.6 Android Services....115
10.7 Content Providers....116
10.8 The Application Manifest....116
10.9 Application Resources....116
10.10 Application Context....116
10.11 Summary....116
11. An Introduction to Kotlin....118
11.1 What is Kotlin?....118
11.2 Kotlin and Java....118
11.3 Converting from Java to Kotlin....118
11.4 Kotlin and Android Studio....119
11.5 Experimenting with Kotlin....119
11.6 Semi-colons in Kotlin....120
11.7 Summary....120
12. Kotlin Data Types, Variables, and Nullability....122
12.1 Kotlin Data Types....122
12.1.1 Integer Data Types....123
12.1.2 Floating-Point Data Types....123
12.1.3 Boolean Data Type....123
12.1.4 Character Data Type....123
12.1.5 String Data Type....123
12.1.6 Escape Sequences....124
12.2 Mutable Variables....125
12.3 Immutable Variables....125
12.4 Declaring Mutable and Immutable Variables....125
12.5 Data Types are Objects....125
12.6 Type Annotations and Type Inference....126
12.7 Nullable Type....127
12.8 The Safe Call Operator....127
12.9 Not-Null Assertion....128
12.10 Nullable Types and the let Function....128
12.11 Late Initialization (lateinit)....129
12.12 The Elvis Operator....130
12.13 Type Casting and Type Checking....130
12.14 Summary....131
13. Kotlin Operators and Expressions....132
13.1 Expression Syntax in Kotlin....132
13.2 The Basic Assignment Operator....132
13.3 Kotlin Arithmetic Operators....132
13.4 Augmented Assignment Operators....133
13.5 Increment and Decrement Operators....133
13.6 Equality Operators....134
13.7 Boolean Logical Operators....134
13.8 Range Operator....135
13.9 Bitwise Operators....135
13.9.1 Bitwise Inversion....135
13.9.2 Bitwise AND....136
13.9.3 Bitwise OR....136
13.9.4 Bitwise XOR....136
13.9.5 Bitwise Left Shift....137
13.9.6 Bitwise Right Shift....137
13.10 Summary....138
14. Kotlin Control Flow....140
14.1 Looping Control flow....140
14.1.1 The Kotlin for-in Statement....140
14.1.2 The while Loop ....141
14.1.3 The do ... while loop ....142
14.1.4 Breaking from Loops....142
14.1.5 The continue Statement ....143
14.1.6 Break and Continue Labels....143
14.2 Conditional Control Flow....144
14.2.1 Using the if Expressions ....144
14.2.2 Using if ... else Expressions ....145
14.2.3 Using if ... else if ... Expressions ....145
14.2.4 Using the when Statement....145
14.3 Summary....146
15. An Overview of Kotlin Functions and Lambdas....148
15.1 What is a Function?....148
15.2 How to Declare a Kotlin Function....148
15.3 Calling a Kotlin Function....149
15.4 Single Expression Functions....149
15.5 Local Functions....149
15.6 Handling Return Values....150
15.7 Declaring Default Function Parameters....150
15.8 Variable Number of Function Parameters ....150
15.9 Lambda Expressions....151
15.10 Higher-order Functions....152
15.11 Summary....153
16. The Basics of Object Oriented Programming in Kotlin....154
16.1 What is an Object?....154
16.2 What is a Class?....154
16.3 Declaring a Kotlin Class....154
16.4 Adding Properties to a Class....155
16.5 Defining Methods....155
16.6 Declaring and Initializing a Class Instance....155
16.7 Primary and Secondary Constructors....155
16.8 Initializer Blocks....158
16.9 Calling Methods and Accessing Properties....158
16.10 Custom Accessors....158
16.11 Nested and Inner Classes....159
16.12 Companion Objects....160
16.13 Summary....162
17. An Introduction to Kotlin Inheritance and Subclassing....164
17.1 Inheritance, Classes and Subclasses....164
17.2 Subclassing Syntax....164
17.3 A Kotlin Inheritance Example....165
17.4 Extending the Functionality of a Subclass....166
17.5 Overriding Inherited Methods....167
17.6 Adding a Custom Secondary Constructor....168
17.7 Using the SavingsAccount Class....168
17.8 Summary....168
18. An Overview of Android View Binding....170
18.1 Find View by Id....170
18.2 View Binding ....170
18.3 Converting the AndroidSample project....171
18.4 Enabling View Binding....171
18.5 Using View Binding....171
18.6 Choosing an Option....172
18.7 View Binding in the Book Examples....172
18.8 Migrating a Project to View Binding....173
18.9 Summary....173
19. Understanding Android Application and Activity Lifecycles....174
19.1 Android Applications and Resource Management....174
19.2 Android Process States....174
19.2.1 Foreground Process....175
19.2.2 Visible Process....175
19.2.3 Service Process....175
19.2.4 Background Process....175
19.2.5 Empty Process....176
19.3 Inter-Process Dependencies ....176
19.4 The Activity Lifecycle....176
19.5 The Activity Stack....176
19.6 Activity States....177
19.7 Configuration Changes....177
19.8 Handling State Change....178
19.9 Summary....178
20. Handling Android Activity State Changes....180
20.1 New vs. Old Lifecycle Techniques....180
20.2 The Activity and Fragment Classes....180
20.3 Dynamic State vs. Persistent State....182
20.4 The Android Lifecycle Methods....182
20.5 Lifetimes....184
20.6 Foldable Devices and Multi-Resume....185
20.7 Disabling Configuration Change Restarts....185
20.8 Lifecycle Method Limitations....185
20.9 Summary....186
21. Android Activity State Changes by Example....188
21.1 Creating the State Change Example Project....188
21.2 Designing the User Interface....189
21.3 Overriding the Activity Lifecycle Methods....190
21.4 Filtering the Logcat Panel....192
21.5 Running the Application....193
21.6 Experimenting with the Activity....194
21.7 Summary....195
22. Saving and Restoring the State of an Android Activity....196
22.1 Saving Dynamic State....196
22.2 Default Saving of User Interface State....196
22.3 The Bundle Class....197
22.4 Saving the State....198
22.5 Restoring the State....199
22.6 Testing the Application....199
22.7 Summary....199
23. Understanding Android Views, View Groups and Layouts....200
23.1 Designing for Different Android Devices....200
23.2 Views and View Groups....200
23.3 Android Layout Managers....200
23.4 The View Hierarchy....202
23.5 Creating User Interfaces....203
23.6 Summary....203
24. A Guide to the Android Studio Layout Editor Tool....204
24.1 Basic vs. Empty Views Activity Templates....204
24.2 The Android Studio Layout Editor....208
24.3 Design Mode....208
24.4 The Palette....209
24.5 Design Mode and Layout Views....210
24.6 Night Mode....211
24.7 Code Mode....211
24.8 Split Mode....212
24.9 Setting Attributes....213
24.10 Transforms....214
24.11 Tools Visibility Toggles....215
24.12 Converting Views....216
24.13 Displaying Sample Data....217
24.14 Creating a Custom Device Definition....218
24.15 Changing the Current Device....218
24.16 Layout Validation....219
24.17 Summary....220
25. A Guide to the Android ConstraintLayout....222
25.1 How ConstraintLayout Works....222
25.1.1 Constraints....222
25.1.2 Margins....223
25.1.3 Opposing Constraints....223
25.1.4 Constraint Bias....224
25.1.5 Chains....225
25.1.6 Chain Styles....225
25.2 Baseline Alignment....226
25.3 Configuring Widget Dimensions....226
25.4 Guideline Helper....227
25.5 Group Helper....227
25.6 Barrier Helper....227
25.7 Flow Helper....229
25.8 Ratios....230
25.9 ConstraintLayout Advantages....230
25.10 ConstraintLayout Availability....231
25.11 Summary....231
26. A Guide to Using ConstraintLayout in Android Studio....232
26.1 Design and Layout Views....232
26.2 Autoconnect Mode....234
26.3 Inference Mode....234
26.4 Manipulating Constraints Manually....234
26.5 Adding Constraints in the Inspector....236
26.6 Viewing Constraints in the Attributes Window....236
26.7 Deleting Constraints....237
26.8 Adjusting Constraint Bias....238
26.9 Understanding ConstraintLayout Margins....238
26.10 The Importance of Opposing Constraints and Bias....240
26.11 Configuring Widget Dimensions....242
26.12 Design Time Tools Positioning....243
26.13 Adding Guidelines....244
26.14 Adding Barriers....246
26.15 Adding a Group....247
26.16 Working with the Flow Helper....248
26.17 Widget Group Alignment and Distribution....248
26.18 Converting other Layouts to ConstraintLayout....250
26.19 Summary ....250
27. Working with ConstraintLayout Chains and Ratios in Android Studio....252
27.1 Creating a Chain....252
27.2 Changing the Chain Style....254
27.3 Spread Inside Chain Style....255
27.4 Packed Chain Style....255
27.5 Packed Chain Style with Bias....255
27.6 Weighted Chain....255
27.7 Working with Ratios....256
27.8 Summary....258
28. An Android Studio Layout Editor ConstraintLayout Tutorial....260
28.1 An Android Studio Layout Editor Tool Example....260
28.2 Preparing the Layout Editor Environment....260
28.3 Adding the Widgets to the User Interface....261
28.4 Adding the Constraints....264
28.5 Testing the Layout....266
28.6 Using the Layout Inspector....266
28.7 Summary....267
29. Manual XML Layout Design in Android Studio....268
29.1 Manually Creating an XML Layout....268
29.2 Manual XML vs. Visual Layout Design....271
29.3 Summary....271
30. Managing Constraints using Constraint Sets....272
30.1 Kotlin Code vs. XML Layout Files....272
30.2 Creating Views....272
30.3 View Attributes....273
30.4 Constraint Sets....273
30.4.1 Establishing Connections....273
30.4.2 Applying Constraints to a Layout....273
30.4.3 Parent Constraint Connections....273
30.4.4 Sizing Constraints....274
30.4.5 Constraint Bias....274
30.4.6 Alignment Constraints....274
30.4.7 Copying and Applying Constraint Sets....274
30.4.8 ConstraintLayout Chains....274
30.4.9 Guidelines....275
30.4.10 Removing Constraints....275
30.4.11 Scaling....275
30.4.12 Rotation....276
30.5 Summary....276
31. An Android ConstraintSet Tutorial....278
31.1 Creating the Example Project in Android Studio....278
31.2 Adding Views to an Activity....278
31.3 Setting View Attributes....279
31.4 Creating View IDs....280
31.5 Configuring the Constraint Set....281
31.6 Adding the EditText View....282
31.7 Converting Density Independent Pixels (dp) to Pixels (px)....283
31.8 Summary....284
32. A Guide to Using Apply Changes in Android Studio....286
32.1 Introducing Apply Changes....286
32.2 Understanding Apply Changes Options....286
32.3 Using Apply Changes....287
32.4 Configuring Apply Changes Fallback Settings....288
32.5 An Apply Changes Tutorial....288
32.6 Using Apply Code Changes....288
32.7 Using Apply Changes and Restart Activity....289
32.8 Using Run App....289
32.9 Summary....289
33. An Overview and Example of Android Event Handling....290
33.1 Understanding Android Events....290
33.2 Using the android:onClick Resource....290
33.3 Event Listeners and Callback Methods....291
33.4 An Event Handling Example....291
33.5 Designing the User Interface....292
33.6 The Event Listener and Callback Method....292
33.7 Consuming Events....294
33.8 Summary....295
34. Android Touch and Multi-touch Event Handling....296
34.1 Intercepting Touch Events....296
34.2 The MotionEvent Object....297
34.3 Understanding Touch Actions....297
34.4 Handling Multiple Touches....297
34.5 An Example Multi-Touch Application....298
34.6 Designing the Activity User Interface....298
34.7 Implementing the Touch Event Listener....298
34.8 Running the Example Application....301
34.9 Summary....301
35. Detecting Common Gestures Using the Android Gesture Detector Class....302
35.1 Implementing Common Gesture Detection....302
35.2 Creating an Example Gesture Detection Project....303
35.3 Implementing the Listener Class....303
35.4 Creating the GestureDetectorCompat Instance....305
35.5 Implementing the onTouchEvent() Method....305
35.6 Testing the Application....306
35.7 Summary....306
36. Implementing Custom Gesture and Pinch Recognition on Android....308
36.1 The Android Gesture Builder Application....308
36.2 The GestureOverlayView Class....308
36.3 Detecting Gestures....308
36.4 Identifying Specific Gestures....308
36.5 Installing and Running the Gesture Builder Application....308
36.6 Creating a Gestures File....309
36.7 Creating the Example Project....309
36.8 Extracting the Gestures File from the SD Card....309
36.9 Adding the Gestures File to the Project....310
36.10 Designing the User Interface....310
36.11 Loading the Gestures File....311
36.12 Registering the Event Listener....312
36.13 Implementing the onGesturePerformed Method....312
36.14 Testing the Application....313
36.15 Configuring the GestureOverlayView....313
36.16 Intercepting Gestures....314
36.17 Detecting Pinch Gestures....314
36.18 A Pinch Gesture Example Project....314
36.19 Summary....316
37. An Introduction to Android Fragments....318
37.1 What is a Fragment?....318
37.2 Creating a Fragment....318
37.3 Adding a Fragment to an Activity using the Layout XML File....319
37.4 Adding and Managing Fragments in Code....321
37.5 Handling Fragment Events....322
37.6 Implementing Fragment Communication....322
37.7 Summary ....324
38. Using Fragments in Android Studio - An Example....326
38.1 About the Example Fragment Application....326
38.2 Creating the Example Project....326
38.3 Creating the First Fragment Layout....326
38.4 Migrating a Fragment to View Binding....328
38.5 Adding the Second Fragment....329
38.6 Adding the Fragments to the Activity....330
38.7 Making the Toolbar Fragment Talk to the Activity....331
38.8 Making the Activity Talk to the Text Fragment....334
38.9 Testing the Application....335
38.10 Summary....335
39. Modern Android App Architecture with Jetpack....336
39.1 What is Android Jetpack?....336
39.2 The Old Architecture....336
39.3 Modern Android Architecture....336
39.4 The ViewModel Component....337
39.5 The LiveData Component....337
39.6 ViewModel Saved State....338
39.7 LiveData and Data Binding....338
39.8 Android Lifecycles....339
39.9 Repository Modules....339
39.10 Summary....340
40. An Android ViewModel Tutorial....342
40.1 About the Project....342
40.2 Creating the ViewModel Example Project....342
40.3 Removing Unwanted Project Elements....342
40.4 Designing the Fragment Layout....343
40.5 Implementing the View Model....344
40.6 Associating the Fragment with the View Model....345
40.7 Modifying the Fragment....346
40.8 Accessing the ViewModel Data....346
40.9 Testing the Project....347
40.10 Summary....347
41. An Android Jetpack LiveData Tutorial....348
41.1 LiveData - A Recap....348
41.2 Adding LiveData to the ViewModel....348
41.3 Implementing the Observer....350
41.4 Summary....351
42. An Overview of Android Jetpack Data Binding....352
42.1 An Overview of Data Binding....352
42.2 The Key Components of Data Binding....352
42.2.1 The Project Build Configuration....352
42.2.2 The Data Binding Layout File....353
42.2.3 The Layout File Data Element....354
42.2.4 The Binding Classes....355
42.2.5 Data Binding Variable Configuration....355
42.2.6 Binding Expressions (One-Way)....356
42.2.7 Binding Expressions (Two-Way)....357
42.2.8 Event and Listener Bindings....357
42.3 Summary....358
43. An Android Jetpack Data Binding Tutorial....360
43.1 Removing the Redundant Code....360
43.2 Enabling Data Binding....361
43.3 Adding the Layout Element....362
43.4 Adding the Data Element to Layout File....363
43.5 Working with the Binding Class....363
43.6 Assigning the ViewModel Instance to the Data Binding Variable....364
43.7 Adding Binding Expressions....365
43.8 Adding the Conversion Method....366
43.9 Adding a Listener Binding....366
43.10 Testing the App....367
43.11 Summary....367
44. An Android ViewModel Saved State Tutorial....368
44.1 Understanding ViewModel State Saving....368
44.2 Implementing ViewModel State Saving....368
44.3 Saving and Restoring State....369
44.4 Adding Saved State Support to the ViewModelDemo Project....370
44.5 Summary....371
45. Working with Android Lifecycle-Aware Components....372
45.1 Lifecycle Awareness....372
45.2 Lifecycle Owners....372
45.3 Lifecycle Observers....373
45.4 Lifecycle States and Events....373
45.5 Summary....374
46. An Android Jetpack Lifecycle Awareness Tutorial....376
46.1 Creating the Example Lifecycle Project....376
46.2 Creating a Lifecycle Observer....376
46.3 Adding the Observer....377
46.4 Testing the Observer....378
46.5 Creating a Lifecycle Owner....378
46.6 Testing the Custom Lifecycle Owner....380
46.7 Summary....380
47. An Overview of the Navigation Architecture Component....382
47.1 Understanding Navigation....382
47.2 Declaring a Navigation Host....383
47.3 The Navigation Graph....385
47.4 Accessing the Navigation Controller....386
47.5 Triggering a Navigation Action....386
47.6 Passing Arguments....387
47.7 Summary....387
48. An Android Jetpack Navigation Component Tutorial....388
48.1 Creating the NavigationDemo Project....388
48.2 Adding Navigation to the Build Configuration....388
48.3 Creating the Navigation Graph Resource File....389
48.4 Declaring a Navigation Host....390
48.5 Adding Navigation Destinations....391
48.6 Designing the Destination Fragment Layouts....393
48.7 Adding an Action to the Navigation Graph....394
48.8 Implement the OnFragmentInteractionListener....396
48.9 Adding View Binding Support to the Destination Fragments....397
48.10 Triggering the Action....397
48.11 Passing Data Using Safeargs....398
48.12 Summary....401
49. An Introduction to MotionLayout....402
49.1 An Overview of MotionLayout....402
49.2 MotionLayout....402
49.3 MotionScene....402
49.4 Configuring ConstraintSets....403
49.5 Custom Attributes....404
49.6 Triggering an Animation....406
49.7 Arc Motion....407
49.8 Keyframes....407
49.8.1 Attribute Keyframes....407
49.8.2 Position Keyframes....408
49.9 Time Linearity....411
49.10 KeyTrigger....411
49.11 Cycle and Time Cycle Keyframes....412
49.12 Starting an Animation from Code....412
49.13 Summary....413
50. An Android MotionLayout Editor Tutorial....414
50.1 Creating the MotionLayoutDemo Project....414
50.2 ConstraintLayout to MotionLayout Conversion....414
50.3 Configuring Start and End Constraints....416
50.4 Previewing the MotionLayout Animation....419
50.5 Adding an OnClick Gesture....419
50.6 Adding an Attribute Keyframe to the Transition....421
50.7 Adding a CustomAttribute to a Transition....423
50.8 Adding Position Keyframes....425
50.9 Summary....427
51. A MotionLayout KeyCycle Tutorial....428
51.1 An Overview of Cycle Keyframes....428
51.2 Using the Cycle Editor....432
51.3 Creating the KeyCycleDemo Project....433
51.4 Configuring the Start and End Constraints....433
51.5 Creating the Cycles....435
51.6 Previewing the Animation....437
51.7 Adding the KeyFrameSet to the MotionScene....437
51.8 Summary....439
52. Working with the Floating Action Button and Snackbar....440
52.1 The Material Design....440
52.2 The Design Library....440
52.3 The Floating Action Button (FAB) ....440
52.4 The Snackbar....441
52.5 Creating the Example Project....442
52.6 Reviewing the Project....442
52.7 Removing Navigation Features....443
52.8 Changing the Floating Action Button....443
52.9 Adding an Action to the Snackbar....445
52.10 Summary....445
53. Creating a Tabbed Interface using the TabLayout Component....446
53.1 An Introduction to the ViewPager2....446
53.2 An Overview of the TabLayout Component....446
53.3 Creating the TabLayoutDemo Project....447
53.4 Creating the First Fragment....447
53.5 Duplicating the Fragments....449
53.6 Adding the TabLayout and ViewPager2....450
53.7 Performing the Initialization Tasks....452
53.8 Testing the Application....454
53.9 Customizing the TabLayout....454
53.10 Summary....456
54. Working with the RecyclerView and CardView Widgets....458
54.1 An Overview of the RecyclerView....458
54.2 An Overview of the CardView....460
54.3 Summary....461
55. An Android RecyclerView and CardView Tutorial....462
55.1 Creating the CardDemo Project....462
55.2 Modifying the Basic Views Activity Project....462
55.3 Designing the CardView Layout....463
55.4 Adding the RecyclerView....464
55.5 Adding the Image Files....464
55.6 Creating the RecyclerView Adapter....464
55.7 Initializing the RecyclerView Component....466
55.8 Testing the Application....467
55.9 Responding to Card Selections....468
55.10 Summary....470
56. Working with the AppBar and Collapsing Toolbar Layouts....472
56.1 The Anatomy of an AppBar....472
56.2 The Example Project....473
56.3 Coordinating the RecyclerView and Toolbar....473
56.4 Introducing the Collapsing Toolbar Layout....475
56.5 Changing the Title and Scrim Color....478
56.6 Summary....479
57. An Overview of Android Intents....480
57.1 An Overview of Intents....480
57.2 Explicit Intents....480
57.3 Returning Data from an Activity....481
57.4 Implicit Intents....482
57.5 Using Intent Filters....483
57.6 Automatic Link Verification....483
57.7 Manually Enabling Links....486
57.8 Checking Intent Availability....487
57.9 Summary....488
58. Android Explicit Intents – A Worked Example....490
58.1 Creating the Explicit Intent Example Application....490
58.2 Designing the User Interface Layout for MainActivity....490
58.3 Creating the Second Activity Class....491
58.4 Designing the User Interface Layout for SecondActivity....492
58.5 Reviewing the Application Manifest File....492
58.6 Creating the Intent....493
58.7 Extracting Intent Data....494
58.8 Launching SecondActivity as a Sub-Activity....495
58.9 Returning Data from a Sub-Activity....496
58.10 Testing the Application....496
58.11 Summary....496
59. Android Implicit Intents – A Worked Example....498
59.1 Creating the Android Studio Implicit Intent Example Project....498
59.2 Designing the User Interface....498
59.3 Creating the Implicit Intent....499
59.4 Adding a Second Matching Activity....500
59.5 Adding the Web View to the UI....500
59.6 Obtaining the Intent URL....501
59.7 Modifying the MyWebView Project Manifest File....502
59.8 Installing the MyWebView Package on a Device....503
59.9 Testing the Application....504
59.10 Manually Enabling the Link....504
59.11 Automatic Link Verification....506
59.12 Summary....508
60. Android Broadcast Intents and Broadcast Receivers....510
60.1 An Overview of Broadcast Intents....510
60.2 An Overview of Broadcast Receivers....511
60.3 Obtaining Results from a Broadcast....512
60.4 Sticky Broadcast Intents....512
60.5 The Broadcast Intent Example....512
60.6 Creating the Example Application....513
60.7 Creating and Sending the Broadcast Intent....513
60.8 Creating the Broadcast Receiver....514
60.9 Registering the Broadcast Receiver....515
60.10 Testing the Broadcast Example....516
60.11 Listening for System Broadcasts....516
60.12 Summary....516
61. An Introduction to Kotlin Coroutines....518
61.1 What are Coroutines?....518
61.2 Threads vs. Coroutines....518
61.3 Coroutine Scope....519
61.4 Suspend Functions....519
61.5 Coroutine Dispatchers....519
61.6 Coroutine Builders....520
61.7 Jobs....520
61.8 Coroutines – Suspending and Resuming....521
61.9 Returning Results from a Coroutine....522
61.10 Using withContext....522
61.11 Coroutine Channel Communication....524
61.12 Summary....525
62. An Android Kotlin Coroutines Tutorial....526
62.1 Creating the Coroutine Example Application....526
62.2 Adding Coroutine Support to the Project....526
62.3 Designing the User Interface....526
62.4 Implementing the SeekBar....528
62.5 Adding the Suspend Function....528
62.6 Implementing the launchCoroutines Method....529
62.7 Testing the App....530
62.8 Summary....530
63. An Overview of Android Services....532
63.1 Intent Service....532
63.2 Bound Service....532
63.3 The Anatomy of a Service....533
63.4 Controlling Destroyed Service Restart Options....533
63.5 Declaring a Service in the Manifest File....533
63.6 Starting a Service Running on System Startup....534
63.7 Summary....535
64. Android Local Bound Services – A Worked Example....536
64.1 Understanding Bound Services....536
64.2 Bound Service Interaction Options....536
64.3 A Local Bound Service Example....536
64.4 Adding a Bound Service to the Project....537
64.5 Implementing the Binder....537
64.6 Binding the Client to the Service....539
64.7 Completing the Example....540
64.8 Testing the Application....541
64.9 Summary....542
65. Android Remote Bound Services – A Worked Example....544
65.1 Client to Remote Service Communication....544
65.2 Creating the Example Application....544
65.3 Designing the User Interface....544
65.4 Implementing the Remote Bound Service....544
65.5 Configuring a Remote Service in the Manifest File....546
65.6 Launching and Binding to the Remote Service....546
65.7 Sending a Message to the Remote Service....548
65.8 Summary....548
66. An Introduction to Kotlin Flow....550
66.1 Understanding Flows....550
66.2 Creating the Sample Project....550
66.3 Adding the Kotlin Lifecycle Library....551
66.4 Declaring a Flow....551
66.5 Emitting Flow Data....552
66.6 Collecting Flow Data....552
66.7 Adding a Flow Buffer....553
66.8 Transforming Data with Intermediaries....555
66.9 Terminal Flow Operators....556
66.10 Flow Flattening....557
66.11 Combining Multiple Flows....558
66.12 Hot and Cold Flows....559
66.13 StateFlow....560
66.14 SharedFlow....561
66.15 Summary....562
67. An Android SharedFlow Tutorial....564
67.1 About the Project....564
67.2 Creating the SharedFlowDemo Project....564
67.3 Designing the User Interface Layout....564
67.4 Adding the List Row Layout....564
67.5 Adding the RecyclerView Adapter....565
67.6 Adding the ViewModel....566
67.7 Configuring the ViewModelProvider....567
67.8 Collecting the Flow Values....568
67.9 Testing the SharedFlowDemo App....569
67.10 Handling Flows in the Background....569
67.11 Summary....572
68. An Overview of Android SQLite Databases....574
68.1 Understanding Database Tables....574
68.2 Introducing Database Schema ....574
68.3 Columns and Data Types ....574
68.4 Database Rows ....575
68.5 Introducing Primary Keys ....575
68.6 What is SQLite?....575
68.7 Structured Query Language (SQL)....575
68.8 Trying SQLite on an Android Virtual Device (AVD)....576
68.9 The Android Room Persistence Library....577
68.10 Summary....578
69. The Android Room Persistence Library....580
69.1 Revisiting Modern App Architecture....580
69.2 Key Elements of Room Database Persistence....580
69.2.1 Repository....581
69.2.2 Room Database....581
69.2.3 Data Access Object (DAO)....581
69.2.4 Entities....581
69.2.5 SQLite Database....581
69.3 Understanding Entities....582
69.4 Data Access Objects....584
69.5 The Room Database....585
69.6 The Repository....586
69.7 In-Memory Databases....587
69.8 Database Inspector....587
69.9 Summary....588
70. An Android TableLayout and TableRow Tutorial....590
70.1 The TableLayout and TableRow Layout Views....590
70.2 Creating the Room Database Project....591
70.3 Converting to a LinearLayout....591
70.4 Adding the TableLayout to the User Interface....592
70.5 Configuring the TableRows....593
70.6 Adding the Button Bar to the Layout....594
70.7 Adding the RecyclerView....595
70.8 Adjusting the Layout Margins....596
70.9 Summary....596
71. An Android Room Database and Repository Tutorial....598
71.1 About the RoomDemo Project....598
71.2 Modifying the Build Configuration....598
71.3 Building the Entity....599
71.4 Creating the Data Access Object....600
71.5 Adding the Room Database....602
71.6 Adding the Repository....602
71.7 Adding the ViewModel....605
71.8 Creating the Product Item Layout....606
71.9 Adding the RecyclerView Adapter....607
71.10 Preparing the Main Activity....608
71.11 Adding the Button Listeners....609
71.12 Adding LiveData Observers....610
71.13 Initializing the RecyclerView....610
71.14 Testing the RoomDemo App....611
71.15 Using the Database Inspector....611
71.16 Summary....612
72. Video Playback on Android using the VideoView and MediaController Classes....614
72.1 Introducing the Android VideoView Class....614
72.2 Introducing the Android MediaController Class....615
72.3 Creating the Video Playback Example....615
72.4 Designing the VideoPlayer Layout....615
72.5 Downloading the Video File....616
72.6 Configuring the VideoView....616
72.7 Adding the MediaController to the Video View....618
72.8 Setting up the onPreparedListener....618
72.9 Summary....619
73. Android Picture-in-Picture Mode....620
73.1 Picture-in-Picture Features....620
73.2 Enabling Picture-in-Picture Mode....621
73.3 Configuring Picture-in-Picture Parameters....621
73.4 Entering Picture-in-Picture Mode....622
73.5 Detecting Picture-in-Picture Mode Changes....622
73.6 Adding Picture-in-Picture Actions....622
73.7 Summary....623
74. An Android Picture-in-Picture Tutorial....624
74.1 Adding Picture-in-Picture Support to the Manifest....624
74.2 Adding a Picture-in-Picture Button....624
74.3 Entering Picture-in-Picture Mode....625
74.4 Detecting Picture-in-Picture Mode Changes....626
74.5 Adding a Broadcast Receiver....626
74.6 Adding the PiP Action....627
74.7 Testing the Picture-in-Picture Action....630
74.8 Summary....630
75. Making Runtime Permission Requests in Android....632
75.1 Understanding Normal and Dangerous Permissions....632
75.2 Creating the Permissions Example Project....634
75.3 Checking for a Permission....634
75.4 Requesting Permission at Runtime....636
75.5 Providing a Rationale for the Permission Request....637
75.6 Testing the Permissions App....638
75.7 Summary....639
76. Android Audio Recording and Playback using MediaPlayer and MediaRecorder....640
76.1 Playing Audio....640
76.2 Recording Audio and Video using the MediaRecorder Class....641
76.3 About the Example Project....642
76.4 Creating the AudioApp Project....642
76.5 Designing the User Interface....642
76.6 Checking for Microphone Availability....643
76.7 Initializing the Activity....644
76.8 Implementing the recordAudio() Method....645
76.9 Implementing the stopAudio() Method....645
76.10 Implementing the playAudio() method....646
76.11 Configuring and Requesting Permissions....646
76.12 Testing the Application....648
76.13 Summary....648
77. An Android Notifications Tutorial....650
77.1 An Overview of Notifications....650
77.2 Creating the NotifyDemo Project....652
77.3 Designing the User Interface....652
77.4 Creating the Second Activity....652
77.5 Creating a Notification Channel....653
77.6 Requesting Notification Permission....654
77.7 Creating and Issuing a Notification....657
77.8 Launching an Activity from a Notification....659
77.9 Adding Actions to a Notification....661
77.10 Bundled Notifications....661
77.11 Summary....663
78. An Android Direct Reply Notification Tutorial....664
78.1 Creating the DirectReply Project....664
78.2 Designing the User Interface....664
78.3 Requesting Notification Permission....665
78.4 Creating the Notification Channel....666
78.5 Building the RemoteInput Object....667
78.6 Creating the PendingIntent....668
78.7 Creating the Reply Action....669
78.8 Receiving Direct Reply Input....670
78.9 Updating the Notification....671
78.10 Summary....672
79. Working with the Google Maps Android API in Android Studio....674
79.1 The Elements of the Google Maps Android API....674
79.2 Creating the Google Maps Project....675
79.3 Creating a Google Cloud Billing Account....675
79.4 Creating a New Google Cloud Project....676
79.5 Enabling the Google Maps SDK....677
79.6 Generating a Google Maps API Key....678
79.7 Adding the API Key to the Android Studio Project....679
79.8 Testing the Application....679
79.9 Understanding Geocoding and Reverse Geocoding....679
79.10 Adding a Map to an Application....681
79.11 Requesting Current Location Permission....681
79.12 Displaying the Users Current Location....683
79.13 Changing the Map Type....684
79.14 Displaying Map Controls to the User....685
79.15 Handling Map Gesture Interaction....685
79.15.1 Map Zooming Gestures....685
79.15.2 Map ScrollingPanning Gestures....686
79.15.3 Map Tilt Gestures....686
79.15.4 Map Rotation Gestures....686
79.16 Creating Map Markers....686
79.17 Controlling the Map Camera....687
79.18 Summary....688
80. Printing with the Android Printing Framework....690
80.1 The Android Printing Architecture....690
80.2 The Print Service Plugins....690
80.3 Google Cloud Print....691
80.4 Printing to Google Drive....691
80.5 Save as PDF....692
80.6 Printing from Android Devices....692
80.7 Options for Building Print Support into Android Apps....693
80.7.1 Image Printing....693
80.7.2 Creating and Printing HTML Content....694
80.7.3 Printing a Web Page....695
80.7.4 Printing a Custom Document....696
80.8 Summary....696
81. An Android HTML and Web Content Printing Example....698
81.1 Creating the HTML Printing Example Application....698
81.2 Printing Dynamic HTML Content....698
81.3 Creating the Web Page Printing Example....701
81.4 Removing the Floating Action Button....701
81.5 Removing Navigation Features....701
81.6 Designing the User Interface Layout....702
81.7 Accessing the WebView from the Main Activity....703
81.8 Loading the Web Page into the WebView....703
81.9 Adding the Print Menu Option....704
81.10 Summary....706
82. A Guide to Android Custom Document Printing....708
82.1 An Overview of Android Custom Document Printing....708
82.1.1 Custom Print Adapters....708
82.2 Preparing the Custom Document Printing Project....709
82.3 Creating the Custom Print Adapter....710
82.4 Implementing the onLayout() Callback Method....711
82.5 Implementing the onWrite() Callback Method....714
82.6 Checking a Page is in Range....716
82.7 Drawing the Content on the Page Canvas....717
82.8 Starting the Print Job....719
82.9 Testing the Application....720
82.10 Summary....720
83. An Introduction to Android App Links....722
83.1 An Overview of Android App Links....722
83.2 App Link Intent Filters....722
83.3 Handling App Link Intents....723
83.4 Associating the App with a Website....723
83.5 Summary....724
84. An Android Studio App Links Tutorial....726
84.1 About the Example App....726
84.2 The Database Schema....726
84.3 Loading and Running the Project....726
84.4 Adding the URL Mapping....728
84.5 Adding the Intent Filter....731
84.6 Adding Intent Handling Code....731
84.7 Testing the App....734
84.8 Creating the Digital Asset Links File....734
84.9 Testing the App Link....735
84.10 Summary....735
85. An Android Biometric Authentication Tutorial....736
85.1 An Overview of Biometric Authentication....736
85.2 Creating the Biometric Authentication Project....736
85.3 Configuring Device Fingerprint Authentication....737
85.4 Adding the Biometric Permission to the Manifest File....737
85.5 Designing the User Interface....738
85.6 Adding a Toast Convenience Method....738
85.7 Checking the Security Settings....739
85.8 Configuring the Authentication Callbacks....740
85.9 Adding the CancellationSignal....741
85.10 Starting the Biometric Prompt....741
85.11 Testing the Project....742
85.12 Summary....743
86. Creating, Testing, and Uploading an Android App Bundle....744
86.1 The Release Preparation Process....744
86.2 Android App Bundles....744
86.3 Register for a Google Play Developer Console Account....745
86.4 Configuring the App in the Console....746
86.5 Enabling Google Play App Signing....747
86.6 Creating a Keystore File....747
86.7 Creating the Android App Bundle....748
86.8 Generating Test APK Files....750
86.9 Uploading the App Bundle to the Google Play Developer Console....751
86.10 Exploring the App Bundle....752
86.11 Managing Testers....753
86.12 Rolling the App Out for Testing....753
86.13 Uploading New App Bundle Revisions....754
86.14 Analyzing the App Bundle File....755
86.15 Summary....756
87. An Overview of Android In-App Billing....758
87.1 Preparing a Project for In-App Purchasing....758
87.2 Creating In-App Products and Subscriptions....758
87.3 Billing Client Initialization....759
87.4 Connecting to the Google Play Billing Library....760
87.5 Querying Available Products....760
87.6 Starting the Purchase Process....761
87.7 Completing the Purchase....761
87.8 Querying Previous Purchases....762
87.9 Summary....763
88. An Android In-App Purchasing Tutorial....764
88.1 About the In-App Purchasing Example Project....764
88.2 Creating the InAppPurchase Project....764
88.3 Adding Libraries to the Project....764
88.4 Designing the User Interface....765
88.5 Adding the App to the Google Play Store....765
88.6 Creating an In-App Product....766
88.7 Enabling License Testers....766
88.8 Initializing the Billing Client....767
88.9 Querying the Product....769
88.10 Launching the Purchase Flow....770
88.11 Handling Purchase Updates....770
88.12 Consuming the Product....771
88.13 Restoring a Previous Purchase....772
88.14 Testing the App....773
88.15 Troubleshooting....774
88.16 Summary....775
89. Working with Material Design 3 Theming....776
89.1 Material Design 2 vs. Material Design 3....776
89.2 Understanding Material Design Theming....776
89.3 Material Design 3 Theming....776
89.4 Building a Custom Theme....778
89.5 Summary....779
90. A Material Design 3 Theming and Dynamic Color Tutorial....780
90.1 Creating the ThemeDemo Project....780
90.2 Designing the User Interface....780
90.3 Building a New Theme....782
90.4 Adding the Theme to the Project....783
90.5 Enabling Dynamic Color Support....784
90.6 Previewing Dynamic Colors....785
90.7 Summary....786
91. An Overview of Gradle in Android Studio....788
91.1 An Overview of Gradle....788
91.2 Gradle and Android Studio....788
91.2.1 Sensible Defaults....788
91.2.2 Dependencies....788
91.2.3 Build Variants....789
91.2.4 Manifest Entries....789
91.2.5 APK Signing....789
91.2.6 ProGuard Support....789
91.3 The Property and Settings Gradle Build File....789
91.4 The Top-level Gradle Build File....790
91.5 Module Level Gradle Build Files....791
91.6 Configuring Signing Settings in the Build File....793
91.7 Running Gradle Tasks from the Command Line....794
91.8 Summary....795
Index....796
Fully updated for Android Studio Giraffe and the new UI, this book teaches you how to develop Android-based applications using the Kotlin programming language.
This book begins with the basics and outlines how to set up an Android development and testing environment, followed by an introduction to programming in Kotlin, including data types, control flow, functions, lambdas, and object-oriented programming. Asynchronous programming using Kotlin coroutines and flow is also covered in detail.
Chapters also cover the Android Architecture Components, including view models, lifecycle management, Room database access, the Database Inspector, app navigation, live data, and data binding.
More advanced topics such as intents are also covered, as are touch screen handling, gesture recognition, and the recording and playback of audio. This book edition also covers printing, transitions, and foldable device support.
The concepts of material design are also covered in detail, including the use of floating action buttons, Snackbars, tabbed interfaces, card views, navigation drawers, and collapsing toolbars.
Other key features of Android Studio and Android are also covered in detail, including the Layout Editor, the ConstraintLayout and ConstraintSet classes, MotionLayout Editor, view binding, constraint chains, barriers, and direct reply notifications.
Chapters also cover advanced features of Android Studio, such as App Links, Gradle build configuration, in-app billing, and submitting apps to the Google Play Developer Console.
Assuming you already have some programming experience, are ready to download Android Studio and the Android SDK, have access to a Windows, Mac, or Linux system, and have ideas for some apps to develop, you are ready to get started.