Android Studio Bumble Bee Essentials - Java Edition

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Fully updated for Android Studio Bumble Bee, the goal of this book is to teach the skills necessary to develop Android-based applications using the Java programming language. Beginning with the basics, this book provides an outline of the steps necessary to set up an Android development and testing environment. An overview of Android Studio is included covering areas such as tool windows, the code editor, and the Layout Editor tool. An introduction to the architecture of Android is followed by an in-depth look at the design of Android applications and user interfaces using the Android Studio environment. Chapters are also included covering 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 edition of the book also covers printing, transitions, cloud-based file storage, 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 Bumble Bee 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, Dynamic Delivery, Gradle build configuration, and submitting apps to the Google Play Developer Console. Assuming you already have some Java 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.

Author(s): Smyth, Neil;
Edition: 1
Publisher: Payload Media
Year: 2022

Language: English
Pages: 802
Tags: java jdk android mobile apps android sdk

Table of Contents
1. Introduction ............................................................................................................................................... 1
1.1 Downloading the Code Samples....................................................................................................... 1
1.2 Feedback............................................................................................................................................... 1
1.3 Errata..................................................................................................................................................... 2
2. Setting up an Android Studio Development Environment....................................................................... 3
2.1 System Requirements.......................................................................................................................... 3
2.2 Downloading the Android Studio Package ..................................................................................... 3
2.3 Installing Android Studio................................................................................................................... 4
2.3.1 Installation on Windows............................................................................................................. 4
2.3.2 Installation on macOS ................................................................................................................. 4
2.3.3 Installation on Linux.................................................................................................................... 5
2.4 The Android Studio Setup Wizard.................................................................................................... 5
2.5 Installing Additional Android SDK Packages................................................................................. 6
2.6 Making the Android SDK Tools Command-line Accessible......................................................... 9
2.6.1 Windows 8.1 ................................................................................................................................. 9
2.6.2 Windows 10 ................................................................................................................................ 10
2.6.3 Windows 11 ................................................................................................................................ 10
2.6.4 Linux ............................................................................................................................................ 10
2.6.5 macOS.......................................................................................................................................... 10
2.7 Android Studio Memory Management.......................................................................................... 11
2.8 Updating Android Studio and the SDK ......................................................................................... 12
2.9 Summary ............................................................................................................................................ 12
3. Creating an Example Android App in Android Studio........................................................................... 13
3.1 About the Project .............................................................................................................................. 13
3.2 Creating a New Android Project..................................................................................................... 13
3.3 Creating an Activity ......................................................................................................................... 14
3.4 Defining the Project and SDK Settings .......................................................................................... 14
3.5 Modifying the Example Application............................................................................................... 15
3.6 Modifying the User Interface .......................................................................................................... 16
3.7 Reviewing the Layout and Resource Files...................................................................................... 21
3.8 Adding Interaction............................................................................................................................ 24
3.9 Summary ............................................................................................................................................ 25
4. Creating an Android Virtual Device (AVD) in Android Studio............................................................. 27
4.1 About Android Virtual Devices ...................................................................................................... 27
4.2 Starting the Emulator........................................................................................................................ 28
4.3 Running the Application in the AVD............................................................................................. 29
4.4 Running on Multiple Devices.......................................................................................................... 31
4.5 Stopping a Running Application..................................................................................................... 31
4.6 Supporting Dark Theme................................................................................................................... 32
4.7 Running the Emulator in a Separate Window............................................................................... 33
4.8 Enabling the Device Frame.............................................................................................................. 34
ii
Table of Contents
4.9 AVD Command-line Creation ........................................................................................................ 35
4.10 Android Virtual Device Configuration Files............................................................................... 37
4.11 Moving and Renaming an Android Virtual Device ................................................................... 37
4.12 Summary .......................................................................................................................................... 37
5. Using and Configuring the Android Studio AVD Emulator .................................................................. 39
5.1 The Emulator Environment ............................................................................................................. 39
5.2 Emulator Toolbar Options............................................................................................................... 39
5.3 Working in Zoom Mode .................................................................................................................. 41
5.4 Resizing the Emulator Window....................................................................................................... 41
5.5 Extended Control Options............................................................................................................... 41
5.5.1 Location....................................................................................................................................... 42
5.5.2 Displays........................................................................................................................................ 42
5.5.3 Cellular ........................................................................................................................................ 42
5.5.4 Battery.......................................................................................................................................... 42
5.5.5 Camera......................................................................................................................................... 42
5.5.6 Phone ........................................................................................................................................... 42
5.5.7 Directional Pad........................................................................................................................... 42
5.5.8 Microphone................................................................................................................................. 42
5.5.9 Fingerprint .................................................................................................................................. 42
5.5.10 Virtual Sensors ......................................................................................................................... 43
5.5.11 Snapshots................................................................................................................................... 43
5.5.12 Record and Playback ............................................................................................................... 43
5.5.13 Google Play ............................................................................................................................... 43
5.5.14 Settings ...................................................................................................................................... 43
5.5.15 Help............................................................................................................................................ 43
5.6 Working with Snapshots................................................................................................................... 43
5.7 Configuring Fingerprint Emulation ............................................................................................... 44
5.8 The Emulator in Tool Window Mode............................................................................................. 45
5.9 Summary ............................................................................................................................................ 46
6. A Tour of the Android Studio User Interface .......................................................................................... 47
6.1 The Welcome Screen......................................................................................................................... 47
6.2 The Main Window ............................................................................................................................ 48
6.3 The Tool Windows............................................................................................................................ 49
6.4 Android Studio Keyboard Shortcuts .............................................................................................. 52
6.5 Switcher and Recent Files Navigation ............................................................................................ 53
6.6 Changing the Android Studio Theme ............................................................................................ 53
6.7 Summary ............................................................................................................................................ 54
7. Testing Android Studio Apps on a Physical Android Device.................................................................. 55
7.1 An Overview of the Android Debug Bridge (ADB)..................................................................... 55
7.2 Enabling USB Debugging ADB on Android Devices................................................................... 55
7.2.1 macOS ADB Configuration...................................................................................................... 56
7.2.2 Windows ADB Configuration.................................................................................................. 57
7.2.3 Linux adb Configuration........................................................................................................... 58
7.3 Resolving USB Connection Issues.................................................................................................. 58
7.4 Enabling Wireless Debugging on Android Devices..................................................................... 59
7.5 Testing the adb Connection............................................................................................................. 61
7.6 Summary ............................................................................................................................................ 61
iii
Table of Contents
8. The Basics of the Android Studio Code Editor........................................................................................ 63
8.1 The Android Studio Editor............................................................................................................... 63
8.2 Splitting the Editor Window............................................................................................................ 65
8.3 Code Completion.............................................................................................................................. 66
8.4 Statement Completion...................................................................................................................... 67
8.5 Parameter Information..................................................................................................................... 68
8.6 Parameter Name Hints..................................................................................................................... 68
8.7 Code Generation............................................................................................................................... 68
8.8 Code Folding...................................................................................................................................... 69
8.9 Quick Documentation Lookup ....................................................................................................... 70
8.10 Code Reformatting.......................................................................................................................... 71
8.11 Finding Sample Code ..................................................................................................................... 71
8.12 Live Templates................................................................................................................................. 72
8.13 Summary .......................................................................................................................................... 72
9. An Overview of the Android Architecture .............................................................................................. 73
9.1 The Android Software Stack ............................................................................................................ 73
9.2 The Linux Kernel............................................................................................................................... 74
9.3 Android Runtime – ART.................................................................................................................. 74
9.4 Android Libraries.............................................................................................................................. 74
9.4.1 C/C++ Libraries ......................................................................................................................... 75
9.5 Application Framework.................................................................................................................... 75
9.6 Applications ....................................................................................................................................... 76
9.7 Summary ............................................................................................................................................ 76
10. The Anatomy of an Android Application .............................................................................................. 77
10.1 Android Activities........................................................................................................................... 77
10.2 Android Fragments......................................................................................................................... 77
10.3 Android Intents............................................................................................................................... 78
10.4 Broadcast Intents............................................................................................................................. 78
10.5 Broadcast Receivers ........................................................................................................................ 78
10.6 Android Services............................................................................................................................. 78
10.7 Content Providers........................................................................................................................... 79
10.8 The Application Manifest............................................................................................................... 79
10.9 Application Resources.................................................................................................................... 79
10.10 Application Context...................................................................................................................... 79
10.11 Summary ........................................................................................................................................ 79
11. An Overview of Android View Binding................................................................................................. 81
11.1 Find View by Id ............................................................................................................................... 81
11.2 View Binding .................................................................................................................................. 81
11.3 Converting the AndroidSample project....................................................................................... 82
11.4 Enabling View Binding................................................................................................................... 82
11.5 Using View Binding ........................................................................................................................ 82
11.6 Choosing an Option ....................................................................................................................... 83
11.7 View Binding in the Book Examples............................................................................................ 83
11.8 Migrating a Project to View Binding............................................................................................ 84
11.9 Summary .......................................................................................................................................... 85
12. Understanding Android Application and Activity Lifecycles............................................................... 87
12.1 Android Applications and Resource Management..................................................................... 87
iv
Table of Contents
12.2 Android Process States................................................................................................................... 87
12.2.1 Foreground Process ................................................................................................................. 88
12.2.2 Visible Process.......................................................................................................................... 88
12.2.3 Service Process ......................................................................................................................... 88
12.2.4 Background Process................................................................................................................. 88
12.2.5 Empty Process .......................................................................................................................... 89
12.3 Inter-Process Dependencies ......................................................................................................... 89
12.4 The Activity Lifecycle...................................................................................................................... 89
12.5 The Activity Stack............................................................................................................................ 89
12.6 Activity States .................................................................................................................................. 90
12.7 Configuration Changes .................................................................................................................. 90
12.8 Handling State Change................................................................................................................... 91
12.9 Summary .......................................................................................................................................... 91
13. Handling Android Activity State Changes............................................................................................. 93
13.1 New vs. Old Lifecycle Techniques................................................................................................. 93
13.2 The Activity and Fragment Classes............................................................................................... 93
13.3 Dynamic State vs. Persistent State................................................................................................. 95
13.4 The Android Lifecycle Methods.................................................................................................... 96
13.5 Lifetimes........................................................................................................................................... 97
13.6 Foldable Devices and Multi-Resume............................................................................................ 98
13.7 Disabling Configuration Change Restarts................................................................................... 98
13.8 Lifecycle Method Limitations........................................................................................................ 98
13.9 Summary .......................................................................................................................................... 99
14. Android Activity State Changes by Example ....................................................................................... 101
14.1 Creating the State Change Example Project.............................................................................. 101
14.2 Designing the User Interface ....................................................................................................... 102
14.3 Overriding the Activity Lifecycle Methods ............................................................................... 102
14.4 Filtering the Logcat Panel.............................................................................................................104
14.5 Running the Application.............................................................................................................. 105
14.6 Experimenting with the Activity................................................................................................. 106
14.7 Summary ........................................................................................................................................ 107
15. Saving and Restoring the State of an Android Activity ....................................................................... 109
15.1 Saving Dynamic State ................................................................................................................... 109
15.2 Default Saving of User Interface State ........................................................................................ 109
15.3 The Bundle Class........................................................................................................................... 110
15.4 Saving the State.............................................................................................................................. 111
15.5 Restoring the State ........................................................................................................................ 112
15.6 Testing the Application................................................................................................................. 112
15.7 Summary ........................................................................................................................................ 112
16. Understanding Android Views, View Groups and Layouts ................................................................ 115
16.1 Designing for Different Android Devices.................................................................................. 115
16.2 Views and View Groups............................................................................................................... 115
16.3 Android Layout Managers........................................................................................................... 115
16.4 The View Hierarchy ...................................................................................................................... 117
16.5 Creating User Interfaces............................................................................................................... 118
16.6 Summary ........................................................................................................................................ 118
17. A Guide to the Android Studio Layout Editor Tool ............................................................................ 119
v
Table of Contents
17.1 Basic vs. Empty Activity Templates ............................................................................................ 119
17.2 The Android Studio Layout Editor............................................................................................. 123
17.3 Design Mode.................................................................................................................................. 123
17.4 The Palette ...................................................................................................................................... 124
17.5 Design Mode and Layout Views..................................................................................................125
17.6 Night Mode .................................................................................................................................... 126
17.7 Code Mode..................................................................................................................................... 126
17.8 Split Mode ...................................................................................................................................... 127
17.9 Setting Attributes...........................................................................................................................127
17.10 Transforms................................................................................................................................... 129
17.11 Tools Visibility Toggles............................................................................................................... 130
17.12 Converting Views........................................................................................................................ 132
17.13 Displaying Sample Data ............................................................................................................. 132
17.14 Creating a Custom Device Definition..................................................................................... 133
17.15 Changing the Current Device....................................................................................................134
17.16 Layout Validation (Multi Preview)........................................................................................... 134
17.17 Summary ...................................................................................................................................... 135
18. A Guide to the Android ConstraintLayout.......................................................................................... 137
18.1 How ConstraintLayout Works..................................................................................................... 137
18.1.1 Constraints.............................................................................................................................. 137
18.1.2 Margins.................................................................................................................................... 138
18.1.3 Opposing Constraints............................................................................................................138
18.1.4 Constraint Bias....................................................................................................................... 139
18.1.5 Chains...................................................................................................................................... 140
18.1.6 Chain Styles............................................................................................................................. 140
18.2 Baseline Alignment....................................................................................................................... 141
18.3 Configuring Widget Dimensions................................................................................................ 141
18.4 Guideline Helper........................................................................................................................... 142
18.5 Group Helper................................................................................................................................. 142
18.6 Barrier Helper................................................................................................................................ 142
18.7 Flow Helper.................................................................................................................................... 144
18.8 Ratios.............................................................................................................................................. 145
18.9 ConstraintLayout Advantages..................................................................................................... 145
18.10 ConstraintLayout Availability....................................................................................................146
18.11 Summary ...................................................................................................................................... 146
19. A Guide to Using ConstraintLayout in Android Studio ..................................................................... 147
19.1 Design and Layout Views............................................................................................................. 147
19.2 Autoconnect Mode ....................................................................................................................... 148
19.3 Inference Mode.............................................................................................................................. 149
19.4 Manipulating Constraints Manually........................................................................................... 149
19.5 Adding Constraints in the Inspector.......................................................................................... 150
19.6 Viewing Constraints in the Attributes Window........................................................................ 151
19.7 Deleting Constraints..................................................................................................................... 152
19.8 Adjusting Constraint Bias............................................................................................................ 152
19.9 Understanding ConstraintLayout Margins................................................................................ 153
19.10 The Importance of Opposing Constraints and Bias............................................................... 154
19.11 Configuring Widget Dimensions.............................................................................................. 156
19.12 Design Time Tools Positioning ................................................................................................. 157
vi
Table of Contents
19.13 Adding Guidelines...................................................................................................................... 158
19.14 Adding Barriers........................................................................................................................... 160
19.15 Adding a Group........................................................................................................................... 161
19.16 Working with the Flow Helper.................................................................................................. 162
19.17 Widget Group Alignment and Distribution............................................................................ 163
19.18 Converting other Layouts to ConstraintLayout...................................................................... 164
19.19 Summary ..................................................................................................................................... 164
20. Working with ConstraintLayout Chains and Ratios in Android Studio ............................................ 165
20.1 Creating a Chain............................................................................................................................165
20.2 Changing the Chain Style ............................................................................................................ 167
20.3 Spread Inside Chain Style.............................................................................................................167
20.4 Packed Chain Style........................................................................................................................ 168
20.5 Packed Chain Style with Bias....................................................................................................... 168
20.6 Weighted Chain............................................................................................................................. 168
20.7 Working with Ratios..................................................................................................................... 169
20.8 Summary ........................................................................................................................................ 171
21. An Android Studio Layout Editor ConstraintLayout Tutorial ........................................................... 173
21.1 An Android Studio Layout Editor Tool Example ..................................................................... 173
21.2 Creating a New Activity ............................................................................................................... 173
21.3 Preparing the Layout Editor Environment................................................................................ 175
21.4 Adding the Widgets to the User Interface.................................................................................. 176
21.5 Adding the Constraints................................................................................................................ 179
21.6 Testing the Layout......................................................................................................................... 180
21.7 Using the Layout Inspector.......................................................................................................... 181
21.8 Summary ........................................................................................................................................ 182
22. Manual XML Layout Design in Android Studio ................................................................................. 183
22.1 Manually Creating an XML Layout............................................................................................ 183
22.2 Manual XML vs. Visual Layout Design...................................................................................... 186
22.3 Summary ........................................................................................................................................ 186
23. Managing Constraints using Constraint Sets...................................................................................... 187
23.1 Java Code vs. XML Layout Files.................................................................................................. 187
23.2 Creating Views............................................................................................................................... 187
23.3 View Attributes.............................................................................................................................. 188
23.4 Constraint Sets............................................................................................................................... 188
23.4.1 Establishing Connections......................................................................................................188
23.4.2 Applying Constraints to a Layout ........................................................................................ 188
23.4.3 Parent Constraint Connections............................................................................................ 188
23.4.4 Sizing Constraints.................................................................................................................. 189
23.4.5 Constraint Bias....................................................................................................................... 189
23.4.6 Alignment Constraints.......................................................................................................... 189
23.4.7 Copying and Applying Constraint Sets............................................................................... 189
23.4.8 ConstraintLayout Chains...................................................................................................... 189
23.4.9 Guidelines ............................................................................................................................... 190
23.4.10 Removing Constraints......................................................................................................... 190
23.4.11 Scaling....................................................................................................................................190
23.4.12 Rotation................................................................................................................................. 191
23.5 Summary ........................................................................................................................................ 191
vii
Table of Contents
24. An Android ConstraintSet Tutorial..................................................................................................... 193
24.1 Creating the Example Project in Android Studio..................................................................... 193
24.2 Adding Views to an Activity........................................................................................................ 193
24.3 Setting View Attributes................................................................................................................. 194
24.4 Creating View IDs......................................................................................................................... 195
24.5 Configuring the Constraint Set................................................................................................... 196
24.6 Adding the EditText View............................................................................................................ 197
24.7 Converting Density Independent Pixels (dp) to Pixels (px).................................................... 198
24.8 Summary ........................................................................................................................................ 199
25. A Guide to using Apply Changes in Android Studio........................................................................... 201
25.1 Introducing Apply Changes......................................................................................................... 201
25.2 Understanding Apply Changes Options.................................................................................... 201
25.3 Using Apply Changes.................................................................................................................... 202
25.4 Configuring Apply Changes Fallback Settings.......................................................................... 203
25.5 An Apply Changes Tutorial..........................................................................................................203
25.6 Using Apply Code Changes......................................................................................................... 203
25.7 Using Apply Changes and Restart Activity................................................................................ 204
25.8 Using Run App .............................................................................................................................. 204
25.9 Summary ........................................................................................................................................ 205
26. An Overview and Example of Android Event Handling ..................................................................... 207
26.1 Understanding Android Events...................................................................................................207
26.2 Using the android:onClick Resource.......................................................................................... 207
26.3 Event Listeners and Callback Methods...................................................................................... 208
26.4 An Event Handling Example ....................................................................................................... 208
26.5 Designing the User Interface ....................................................................................................... 209
26.6 The Event Listener and Callback Method.................................................................................. 210
26.7 Consuming Events........................................................................................................................ 211
26.8 Summary ........................................................................................................................................ 212
27. Android Touch and Multi-touch Event Handling ............................................................................... 213
27.1 Intercepting Touch Events ........................................................................................................... 213
27.2 The MotionEvent Object.............................................................................................................. 213
27.3 Understanding Touch Actions..................................................................................................... 214
27.4 Handling Multiple Touches ......................................................................................................... 214
27.5 An Example Multi-Touch Application....................................................................................... 214
27.6 Designing the Activity User Interface ........................................................................................ 215
27.7 Implementing the Touch Event Listener.................................................................................... 215
27.8 Running the Example Application............................................................................................. 218
27.9 Summary ........................................................................................................................................ 219
28. Detecting Common Gestures Using the Android Gesture Detector Class......................................... 221
28.1 Implementing Common Gesture Detection.............................................................................. 221
28.2 Creating an Example Gesture Detection Project ...................................................................... 222
28.3 Implementing the Listener Class................................................................................................. 222
28.4 Creating the GestureDetectorCompat Instance........................................................................ 224
28.5 Implementing the onTouchEvent() Method.............................................................................. 225
28.6 Testing the Application................................................................................................................. 225
28.7 Summary ........................................................................................................................................ 226
viii
Table of Contents
29. Implementing Custom Gesture and Pinch Recognition on Android ................................................. 227
29.1 The Android Gesture Builder Application................................................................................. 227
29.2 The GestureOverlayView Class................................................................................................... 227
29.3 Detecting Gestures........................................................................................................................ 227
29.4 Identifying Specific Gestures....................................................................................................... 227
29.5 Installing and Running the Gesture Builder Application ........................................................ 228
29.6 Creating a Gestures File ............................................................................................................... 228
29.7 Creating the Example Project...................................................................................................... 228
29.8 Extracting the Gestures File from the SD Card ........................................................................ 229
29.9 Adding the Gestures File to the Project ..................................................................................... 229
29.10 Designing the User Interface ..................................................................................................... 229
29.11 Loading the Gestures File .......................................................................................................... 230
29.12 Registering the Event Listener................................................................................................... 231
29.13 Implementing the onGesturePerformed Method................................................................... 231
29.14 Testing the Application...............................................................................................................232
29.15 Configuring the GestureOverlayView...................................................................................... 233
29.16 Intercepting Gestures..................................................................................................................233
29.17 Detecting Pinch Gestures........................................................................................................... 233
29.18 A Pinch Gesture Example Project............................................................................................. 234
29.19 Summary ...................................................................................................................................... 236
30. An Introduction to Android Fragments.............................................................................................. 237
30.1 What is a Fragment?..................................................................................................................... 237
30.2 Creating a Fragment ..................................................................................................................... 237
30.3 Adding a Fragment to an Activity using the Layout XML File............................................... 238
30.4 Adding and Managing Fragments in Code ............................................................................... 240
30.5 Handling Fragment Events.......................................................................................................... 241
30.6 Implementing Fragment Communication................................................................................. 242
30.7 Summary ....................................................................................................................................... 243
31. Using Fragments in Android Studio - An Example............................................................................. 245
31.1 About the Example Fragment Application ................................................................................ 245
31.2 Creating the Example Project...................................................................................................... 245
31.3 Creating the First Fragment Layout............................................................................................ 245
31.4 Migrating a Fragment to View Binding ..................................................................................... 247
31.5 Adding the Second Fragment...................................................................................................... 248
31.6 Adding the Fragments to the Activity ........................................................................................ 249
31.7 Making the Toolbar Fragment Talk to the Activity .................................................................. 250
31.8 Making the Activity Talk to the Text Fragment ........................................................................ 253
31.9 Testing the Application................................................................................................................. 254
31.10 Summary ...................................................................................................................................... 255
32. Modern Android App Architecture with Jetpack ................................................................................ 257
32.1 What is Android Jetpack? ............................................................................................................ 257
32.2 The “Old” Architecture................................................................................................................. 257
32.3 Modern Android Architecture .................................................................................................... 257
32.4 The ViewModel Component ....................................................................................................... 258
32.5 The LiveData Component............................................................................................................ 258
32.6 ViewModel Saved State.................................................................................................................259
32.7 LiveData and Data Binding.......................................................................................................... 260
ix
Table of Contents
32.8 Android Lifecycles ........................................................................................................................ 260
32.9 Repository Modules...................................................................................................................... 260
32.10 Summary ...................................................................................................................................... 261
33. An Android Jetpack ViewModel Tutorial............................................................................................ 263
33.1 About the Project .......................................................................................................................... 263
33.2 Creating the ViewModel Example Project................................................................................. 263
33.3 Reviewing the Project................................................................................................................... 264
33.3.1 The Main Activity................................................................................................................... 264
33.3.2 The Content Fragment .......................................................................................................... 264
33.3.3 The ViewModel ...................................................................................................................... 266
33.4 Designing the Fragment Layout.................................................................................................. 266
33.5 Implementing the View Model.................................................................................................... 267
33.6 Associating the Fragment with the View Model....................................................................... 267
33.7 Modifying the Fragment .............................................................................................................. 268
33.8 Accessing the ViewModel Data................................................................................................... 269
33.9 Testing the Project......................................................................................................................... 270
33.10 Summary ...................................................................................................................................... 270
34. An Android Jetpack LiveData Tutorial................................................................................................ 271
34.1 LiveData - A Recap ....................................................................................................................... 271
34.2 Adding LiveData to the ViewModel........................................................................................... 271
34.3 Implementing the Observer......................................................................................................... 273
34.4 Summary ........................................................................................................................................ 275
35. An Overview of Android Jetpack Data Binding .................................................................................. 277
35.1 An Overview of Data Binding..................................................................................................... 277
35.2 The Key Components of Data Binding ...................................................................................... 277
35.2.1 The Project Build Configuration.......................................................................................... 277
35.2.2 The Data Binding Layout File............................................................................................... 278
35.2.3 The Layout File Data Element .............................................................................................. 279
35.2.4 The Binding Classes.............................................................................................................. 280
35.2.5 Data Binding Variable Configuration.................................................................................. 280
35.2.6 Binding Expressions (One-Way).......................................................................................... 281
35.2.7 Binding Expressions (Two-Way).......................................................................................... 282
35.2.8 Event and Listener Bindings................................................................................................. 282
35.3 Summary ........................................................................................................................................ 283
36. An Android Jetpack Data Binding Tutorial......................................................................................... 285
36.1 Removing the Redundant Code.................................................................................................. 285
36.2 Enabling Data Binding ................................................................................................................. 287
36.3 Adding the Layout Element......................................................................................................... 287
36.4 Adding the Data Element to Layout File.................................................................................... 288
36.5 Working with the Binding Class ................................................................................................. 288
36.6 Assigning the ViewModel Instance to the Data Binding Variable ......................................... 289
36.7 Adding Binding Expressions ....................................................................................................... 290
36.8 Adding the Conversion Method ................................................................................................. 291
36.9 Adding a Listener Binding ........................................................................................................... 291
36.10 Testing the App............................................................................................................................ 291
36.11 Summary ...................................................................................................................................... 292
37. An Android ViewModel Saved State Tutorial...................................................................................... 293
x
Table of Contents
37.1 Understanding ViewModel State Saving.................................................................................... 293
37.2 Implementing ViewModel State Saving ..................................................................................... 294
37.3 Saving and Restoring State........................................................................................................... 295
37.4 Adding Saved State Support to the ViewModelDemo Project................................................ 295
37.5 Summary ........................................................................................................................................ 297
38. Working with Android Lifecycle-Aware Components........................................................................ 299
38.1 Lifecycle Awareness ...................................................................................................................... 299
38.2 Lifecycle Owners........................................................................................................................... 299
38.3 Lifecycle Observers....................................................................................................................... 300
38.4 Lifecycle States and Events........................................................................................................... 301
38.5 Summary ........................................................................................................................................ 302
39. An Android Jetpack Lifecycle Awareness Tutorial .............................................................................. 303
39.1 Creating the Example Lifecycle Project...................................................................................... 303
39.2 Creating a Lifecycle Observer...................................................................................................... 303
39.3 Adding the Observer .................................................................................................................... 304
39.4 Testing the Observer..................................................................................................................... 305
39.5 Creating a Lifecycle Owner.......................................................................................................... 305
39.6 Testing the Custom Lifecycle Owner.......................................................................................... 307
39.7 Summary ........................................................................................................................................ 308
40. An Overview of the Navigation Architecture Component.................................................................. 309
40.1 Understanding Navigation........................................................................................................... 309
40.2 Declaring a Navigation Host........................................................................................................ 310
40.3 The Navigation Graph .................................................................................................................. 312
40.4 Accessing the Navigation Controller.......................................................................................... 313
40.5 Triggering a Navigation Action................................................................................................... 313
40.6 Passing Arguments........................................................................................................................ 314
40.7 Summary ........................................................................................................................................ 314
41. An Android Jetpack Navigation Component Tutorial ........................................................................ 315
41.1 Creating the NavigationDemo Project....................................................................................... 315
41.2 Adding Navigation to the Build Configuration......................................................................... 315
41.3 Creating the Navigation Graph Resource File........................................................................... 316
41.4 Declaring a Navigation Host........................................................................................................ 317
41.5 Adding Navigation Destinations................................................................................................. 319
41.6 Designing the Destination Fragment Layouts........................................................................... 320
41.7 Adding an Action to the Navigation Graph............................................................................... 321
41.8 Implement the OnFragmentInteractionListener...................................................................... 323
41.9 Adding View Binding Support to the Destination Fragments................................................ 324
41.10 Triggering the Action ................................................................................................................. 325
41.11 Passing Data Using Safeargs...................................................................................................... 325
41.12 Summary ...................................................................................................................................... 329
42. An Introduction to MotionLayout....................................................................................................... 331
42.1 An Overview of MotionLayout................................................................................................... 331
42.2 MotionLayout................................................................................................................................331
42.3 MotionScene .................................................................................................................................. 331
42.4 Configuring ConstraintSets......................................................................................................... 332
42.5 Custom Attributes......................................................................................................................... 333
xi
Table of Contents
42.6 Triggering an Animation.............................................................................................................. 334
42.7 Arc Motion..................................................................................................................................... 336
42.8 Keyframes....................................................................................................................................... 336
42.8.1 Attribute Keyframes............................................................................................................... 336
42.8.2 Position Keyframes................................................................................................................ 337
42.9 Time Linearity ............................................................................................................................... 340
42.10 KeyTrigger.................................................................................................................................... 341
42.11 Cycle and Time Cycle Keyframes ............................................................................................. 341
42.12 Starting an Animation from Code............................................................................................ 341
43. An Android MotionLayout Editor Tutorial......................................................................................... 343
43.1 Creating the MotionLayoutDemo Project................................................................................. 343
43.2 ConstraintLayout to MotionLayout Conversion ...................................................................... 343
43.3 Configuring Start and End Constraints..................................................................................... 345
43.4 Previewing the MotionLayout Animation................................................................................. 347
43.5 Adding an OnClick Gesture ........................................................................................................ 348
43.6 Adding an Attribute Keyframe to the Transition...................................................................... 349
43.7 Adding a CustomAttribute to a Transition................................................................................ 352
43.8 Adding Position Keyframes......................................................................................................... 353
43.9 Summary ........................................................................................................................................ 356
44. A MotionLayout KeyCycle Tutorial ..................................................................................................... 357
44.1 An Overview of Cycle Keyframes............................................................................................... 357
44.2 Using the Cycle Editor.................................................................................................................. 361
44.3 Creating the KeyCycleDemo Project.......................................................................................... 362
44.4 Configuring the Start and End Constraints............................................................................... 362
44.5 Creating the Cycles ....................................................................................................................... 364
44.6 Previewing the Animation........................................................................................................... 366
44.7 Adding the KeyFrameSet to the MotionScene .......................................................................... 366
44.8 Summary ........................................................................................................................................ 368
45. Working with the Floating Action Button and Snackbar .................................................................... 369
45.1 The Material Design...................................................................................................................... 369
45.2 The Design Library ....................................................................................................................... 369
45.3 The Floating Action Button (FAB) ............................................................................................. 369
45.4 The Snackbar.................................................................................................................................. 370
45.5 Creating the Example Project...................................................................................................... 371
45.6 Reviewing the Project................................................................................................................... 371
45.7 Removing Navigation Features....................................................................................................372
45.8 Changing the Floating Action Button ........................................................................................ 373
45.9 Adding an Action to the Snackbar.............................................................................................. 374
45.10 Summary ...................................................................................................................................... 374
46. Creating a Tabbed Interface using the TabLayout Component .......................................................... 377
46.1 An Introduction to the ViewPager2 ........................................................................................... 377
46.2 An Overview of the TabLayout Component ............................................................................. 377
46.3 Creating the TabLayoutDemo Project........................................................................................ 378
46.4 Creating the First Fragment......................................................................................................... 379
46.5 Duplicating the Fragments...........................................................................................................380
46.6 Adding the TabLayout and ViewPager2..................................................................................... 381
46.7 Creating the Pager Adapter.......................................................................................................... 382
xii
Table of Contents
46.8 Performing the Initialization Tasks............................................................................................. 383
46.9 Testing the Application................................................................................................................. 385
46.10 Customizing the TabLayout....................................................................................................... 385
46.11 Summary ...................................................................................................................................... 386
47. Working with the RecyclerView and CardView Widgets.................................................................... 389
47.1 An Overview of the RecyclerView.............................................................................................. 389
47.2 An Overview of the CardView.................................................................................................... 391
47.3 Summary ........................................................................................................................................ 392
48. An Android RecyclerView and CardView Tutorial............................................................................. 393
48.1 Creating the CardDemo Project..................................................................................................393
48.2 Modifying the Basic Activity Project.......................................................................................... 393
48.3 Designing the CardView Layout................................................................................................. 394
48.4 Adding the RecyclerView............................................................................................................. 395
48.5 Adding the Image Files................................................................................................................. 395
48.6 Creating the RecyclerView Adapter............................................................................................ 396
48.7 Initializing the RecyclerView Component................................................................................. 398
48.8 Testing the Application................................................................................................................. 399
48.9 Responding to Card Selections....................................................................................................400
48.10 Summary ...................................................................................................................................... 401
49. A Layout Editor Sample Data Tutorial ................................................................................................ 403
49.1 Adding Sample Data to a Project................................................................................................ 403
49.2 Using Custom Sample Data ......................................................................................................... 407
49.3 Summary ........................................................................................................................................ 410
50. Working with the AppBar and Collapsing Toolbar Layouts............................................................... 411
50.1 The Anatomy of an AppBar......................................................................................................... 411
50.2 The Example Project..................................................................................................................... 412
50.3 Coordinating the RecyclerView and Toolbar............................................................................ 412
50.4 Introducing the Collapsing Toolbar Layout .............................................................................. 414
50.5 Changing the Title and Scrim Color .......................................................................................... 417
50.6 Summary ........................................................................................................................................ 418
51. An Android Studio Primary/Detail Flow Tutorial.............................................................................. 419
51.1 The Primary/Detail Flow.............................................................................................................. 419
51.2 Creating a Primary/Detail Flow Activity ................................................................................... 420
51.3 Modifying the Primary/Detail Flow Template.......................................................................... 421
51.4 Changing the Content Model...................................................................................................... 421
51.5 Changing the Detail Pane ............................................................................................................ 423
51.6 Modifying the WebsiteDetailFragment Class............................................................................ 424
51.7 Modifying the WebsiteListFragment Class................................................................................ 425
51.8 Adding Manifest Permissions...................................................................................................... 426
51.9 Running the Application.............................................................................................................. 426
51.10 Summary ...................................................................................................................................... 426
52. An Overview of Android Intents ......................................................................................................... 427
52.1 An Overview of Intents................................................................................................................ 427
52.2 Explicit Intents............................................................................................................................... 427
52.3 Returning Data from an Activity ................................................................................................ 428
52.4 Implicit Intents .............................................................................................................................. 429
xiii
Table of Contents
52.5 Using Intent Filters........................................................................................................................ 430
52.6 Automatic Link Verification ........................................................................................................ 431
52.7 Manually Enabling Links ............................................................................................................. 433
52.8 Checking Intent Availability ........................................................................................................ 435
52.9 Summary ........................................................................................................................................ 435
53. Android Explicit Intents – A Worked Example................................................................................... 437
53.1 Creating the Explicit Intent Example Application.................................................................... 437
53.2 Designing the User Interface Layout for MainActivity............................................................ 437
53.3 Creating the Second Activity Class............................................................................................. 438
53.4 Designing the User Interface Layout for SecondActivity ........................................................ 439
53.5 Reviewing the Application Manifest File ................................................................................... 439
53.6 Creating the Intent........................................................................................................................ 440
53.7 Extracting Intent Data .................................................................................................................. 441
53.8 Launching SecondActivity as a Sub-Activity............................................................................. 442
53.9 Returning Data from a Sub-Activity...........................................................................................443
53.10 Testing the Application...............................................................................................................443
53.11 Summary ...................................................................................................................................... 443
54. Android Implicit Intents – A Worked Example .................................................................................. 445
54.1 Creating the Android Studio Implicit Intent Example Project ............................................... 445
54.2 Designing the User Interface ....................................................................................................... 445
54.3 Creating the Implicit Intent ......................................................................................................... 446
54.4 Adding a Second Matching Activity........................................................................................... 447
54.5 Adding the Web View to the UI.................................................................................................. 447
54.6 Obtaining the Intent URL............................................................................................................ 448
54.7 Modifying the MyWebView Project Manifest File ................................................................... 449
54.8 Installing the MyWebView Package on a Device...................................................................... 450
54.9 Testing the Application................................................................................................................. 451
54.10 Manually Enabling the Link ...................................................................................................... 451
54.11 Automatic Link Verification...................................................................................................... 453
54.12 Summary ...................................................................................................................................... 455
55. Android Broadcast Intents and Broadcast Receivers.......................................................................... 457
55.1 An Overview of Broadcast Intents.............................................................................................. 457
55.2 An Overview of Broadcast Receivers ......................................................................................... 458
55.3 Obtaining Results from a Broadcast........................................................................................... 459
55.4 Sticky Broadcast Intents............................................................................................................... 459
55.5 The Broadcast Intent Example..................................................................................................... 460
55.6 Creating the Example Application.............................................................................................. 460
55.7 Creating and Sending the Broadcast Intent............................................................................... 460
55.8 Creating the Broadcast Receiver................................................................................................. 461
55.9 Registering the Broadcast Receiver............................................................................................. 462
55.10 Testing the Broadcast Example ................................................................................................. 463
55.11 Listening for System Broadcasts................................................................................................ 463
55.12 Summary ...................................................................................................................................... 464
56. A Basic Overview of Java Threads, Handlers and Executors............................................................... 465
56.1 An Overview of Threads .............................................................................................................. 465
56.2 The Application Main Thread...................................................................................................... 465
56.3 Thread Handlers............................................................................................................................ 465
xiv
Table of Contents
56.4 A Threading Example ................................................................................................................... 465
56.5 Building the App ........................................................................................................................... 466
56.6 Creating a New Thread................................................................................................................. 467
56.7 Implementing a Thread Handler................................................................................................. 468
56.8 Passing a Message to the Handler............................................................................................... 469
56.9 Java Executor Concurrency ......................................................................................................... 470
56.10 Working with Runnable Tasks................................................................................................... 471
56.11 Shutting down an Executor Service.......................................................................................... 472
56.12 Working with Callable Tasks and Futures ............................................................................... 472
56.13 Handling a Future Result ........................................................................................................... 474
56.14 Scheduling Tasks......................................................................................................................... 475
56.15 Summary ...................................................................................................................................... 476
57. An Overview of Android Services........................................................................................................ 477
57.1 Started Services.............................................................................................................................. 477
57.2 Intent Service ................................................................................................................................. 477
57.3 Bound Service................................................................................................................................ 478
57.4 The Anatomy of a Service ............................................................................................................ 478
57.5 Controlling Destroyed Service Restart Options........................................................................ 479
57.6 Declaring a Service in the Manifest File..................................................................................... 479
57.7 Starting a Service Running on System Startup.......................................................................... 480
57.8 Summary ........................................................................................................................................ 480
58. Implementing an Android Started Service – A Worked Example ...................................................... 481
58.1 Creating the Example Project...................................................................................................... 481
58.2 Designing the User Interface ....................................................................................................... 481
58.3 Creating the Service Class............................................................................................................ 481
58.4 Adding the Service to the Manifest File ..................................................................................... 483
58.5 Starting the Service ....................................................................................................................... 483
58.6 Testing the IntentService Example.............................................................................................. 484
58.7 Using the Service Class................................................................................................................. 484
58.8 Creating the New Service............................................................................................................. 485
58.9 Launching the Service .................................................................................................................. 486
58.10 Running the Application............................................................................................................ 486
58.11 Adding Threading to the Service .............................................................................................. 487
58.12 Summary ...................................................................................................................................... 488
59. Android Local Bound Services – A Worked Example......................................................................... 489
59.1 Understanding Bound Services................................................................................................... 489
59.2 Bound Service Interaction Options............................................................................................ 489
59.3 A Local Bound Service Example................................................................................................. 489
59.4 Adding a Bound Service to the Project...................................................................................... 490
59.5 Implementing the Binder............................................................................................................. 490
59.6 Binding the Client to the Service ................................................................................................ 493
59.7 Completing the Example.............................................................................................................. 494
59.8 Testing the Application................................................................................................................. 495
59.9 Summary ........................................................................................................................................ 495
60. Android Remote Bound Services – A Worked Example ..................................................................... 497
60.1 Client to Remote Service Communication................................................................................ 497
60.2 Creating the Example Application.............................................................................................. 497
xv
Table of Contents
60.3 Designing the User Interface ....................................................................................................... 497
60.4 Implementing the Remote Bound Service................................................................................. 498
60.5 Configuring a Remote Service in the Manifest File.................................................................. 499
60.6 Launching and Binding to the Remote Service......................................................................... 500
60.7 Sending a Message to the Remote Service ................................................................................. 501
60.8 Summary ........................................................................................................................................ 502
61. An Android Notifications Tutorial ...................................................................................................... 503
61.1 An Overview of Notifications...................................................................................................... 503
61.2 Creating the NotifyDemo Project............................................................................................... 505
61.3 Designing the User Interface ....................................................................................................... 505
61.4 Creating the Second Activity....................................................................................................... 505
61.5 Creating a Notification Channel ................................................................................................. 506
61.6 Creating and Issuing a Notification............................................................................................ 508
61.7 Launching an Activity from a Notification................................................................................ 510
61.8 Adding Actions to a Notification ................................................................................................ 512
61.9 Bundled Notifications................................................................................................................... 512
61.10 Summary ...................................................................................................................................... 514
62. An Android Direct Reply Notification Tutorial .................................................................................. 517
62.1 Creating the DirectReply Project................................................................................................ 517
62.2 Designing the User Interface ....................................................................................................... 517
62.3 Creating the Notification Channel.............................................................................................. 518
62.4 Building the RemoteInput Object............................................................................................... 519
62.5 Creating the PendingIntent.......................................................................................................... 520
62.6 Creating the Reply Action............................................................................................................ 520
62.7 Receiving Direct Reply Input....................................................................................................... 523
62.8 Updating the Notification ............................................................................................................ 524
62.9 Summary ........................................................................................................................................ 525
63. Foldable Devices and Multi-Window Support ................................................................................... 527
63.1 Foldables and Multi-Window Support....................................................................................... 527
63.2 Using a Foldable Emulator........................................................................................................... 528
63.3 Entering Multi-Window Mode ................................................................................................... 529
63.4 Enabling and using Freeform Support ....................................................................................... 530
63.5 Checking for Freeform Support.................................................................................................. 530
63.6 Enabling Multi-Window Support in an App............................................................................. 530
63.7 Specifying Multi-Window Attributes......................................................................................... 531
63.8 Detecting Multi-Window Mode in an Activity......................................................................... 532
63.9 Receiving Multi-Window Notifications..................................................................................... 532
63.10 Launching an Activity in Multi-Window Mode ..................................................................... 533
63.11 Configuring Freeform Activity Size and Position................................................................... 533
63.12 Summary ...................................................................................................................................... 534
64. An Overview of Android SQLite Databases........................................................................................ 535
64.1 Understanding Database Tables.................................................................................................. 535
64.2 Introducing Database Schema .................................................................................................... 535
64.3 Columns and Data Types ............................................................................................................ 535
64.4 Database Rows .............................................................................................................................. 536
64.5 Introducing Primary Keys ...........................................................................................................536
64.6 What is SQLite?............................................................................................................................. 536
xvi
Table of Contents
64.7 Structured Query Language (SQL)............................................................................................. 536
64.8 Trying SQLite on an Android Virtual Device (AVD) .............................................................. 537
64.9 The Android Room Persistence Library..................................................................................... 539
64.10 Summary ...................................................................................................................................... 539
65. The Android Room Persistence Library .............................................................................................. 541
65.1 Revisiting Modern App Architecture ......................................................................................... 541
65.2 Key Elements of Room Database Persistence............................................................................ 541
65.2.1 Repository ............................................................................................................................... 542
65.2.2 Room Database ...................................................................................................................... 542
65.2.3 Data Access Object (DAO)................................................................................................... 542
65.2.4 Entities..................................................................................................................................... 542
65.2.5 SQLite Database ..................................................................................................................... 542
65.3 Understanding Entities................................................................................................................. 543
65.4 Data Access Objects...................................................................................................................... 546
65.5 The Room Database...................................................................................................................... 547
65.6 The Repository............................................................................................................................... 548
65.7 In-Memory Databases.................................................................................................................. 549
65.8 Database Inspector........................................................................................................................ 549
65.9 Summary ........................................................................................................................................ 549
66. An Android TableLayout and TableRow Tutorial ............................................................................... 551
66.1 The TableLayout and TableRow Layout Views.......................................................................... 551
66.2 Creating the Room Database Project ......................................................................................... 552
66.3 Converting to a LinearLayout......................................................................................................552
66.4 Adding the TableLayout to the User Interface........................................................................... 553
66.5 Configuring the TableRows ......................................................................................................... 554
66.6 Adding the Button Bar to the Layout ......................................................................................... 555
66.7 Adding the RecyclerView............................................................................................................. 556
66.8 Adjusting the Layout Margins..................................................................................................... 557
66.9 Summary ........................................................................................................................................ 557
67. An Android Room Database and Repository Tutorial........................................................................ 559
67.1 About the RoomDemo Project.................................................................................................... 559
67.2 Modifying the Build Configuration............................................................................................ 559
67.3 Building the Entity ........................................................................................................................ 560
67.4 Creating the Data Access Object................................................................................................. 561
67.5 Adding the Room Database......................................................................................................... 562
67.6 Adding the Repository ................................................................................................................. 563
67.7 Modifying the ViewModel........................................................................................................... 566
67.8 Creating the Product Item Layout .............................................................................................. 567
67.9 Adding the RecyclerView Adapter.............................................................................................. 568
67.10 Preparing the Main Fragment................................................................................................... 569
67.11 Adding the Button Listeners...................................................................................................... 570
67.12 Adding LiveData Observers ...................................................................................................... 572
67.13 Initializing the RecyclerView..................................................................................................... 573
67.14 Testing the RoomDemo App..................................................................................................... 573
67.15 Using the Database Inspector.................................................................................................... 573
67.16 Summary ...................................................................................................................................... 574
68. Accessing Cloud Storage using the Android Storage Access Framework........................................... 575
xvii
Table of Contents
68.1 The Storage Access Framework................................................................................................... 575
68.2 Working with the Storage Access Framework.......................................................................... 576
68.3 Filtering Picker File Listings........................................................................................................ 576
68.4 Handling Intent Results................................................................................................................ 577
68.5 Reading the Content of a File ...................................................................................................... 577
68.6 Writing Content to a File ............................................................................................................. 578
68.7 Deleting a File................................................................................................................................ 579
68.8 Gaining Persistent Access to a File..............................................................................................579
68.9 Summary ........................................................................................................................................ 579
69. An Android Storage Access Framework Example ............................................................................... 581
69.1 About the Storage Access Framework Example........................................................................ 581
69.2 Creating the Storage Access Framework Example.................................................................... 581
69.3 Designing the User Interface ....................................................................................................... 581
69.4 Adding the Activity Launchers.................................................................................................... 582
69.5 Creating a New Storage File......................................................................................................... 584
69.6 Saving to a Storage File................................................................................................................. 586
69.7 Opening and Reading a Storage File .......................................................................................... 587
69.8 Testing the Storage Access Application...................................................................................... 588
69.9 Summary ........................................................................................................................................ 589
70. Video Playback on Android using the VideoView and MediaController Classes.............................. 591
70.1 Introducing the Android VideoView Class............................................................................... 591
70.2 Introducing the Android MediaController Class..................................................................... 592
70.3 Creating the Video Playback Example ....................................................................................... 592
70.4 Designing the VideoPlayer Layout ............................................................................................. 592
70.5 Downloading the Video File........................................................................................................ 593
70.6 Configuring the VideoView......................................................................................................... 593
70.7 Adding the MediaController to the Video View....................................................................... 595
70.8 Setting up the onPreparedListener ............................................................................................. 596
70.9 Summary ........................................................................................................................................ 597
71. Android Picture-in-Picture Mode........................................................................................................ 599
71.1 Picture-in-Picture Features.......................................................................................................... 599
71.2 Enabling Picture-in-Picture Mode.............................................................................................. 600
71.3 Configuring Picture-in-Picture Parameters .............................................................................. 600
71.4 Entering Picture-in-Picture Mode .............................................................................................. 601
71.5 Detecting Picture-in-Picture Mode Changes............................................................................ 601
71.6 Adding Picture-in-Picture Actions............................................................................................. 602
71.7 Summary ........................................................................................................................................ 602
72. An Android Picture-in-Picture Tutorial.............................................................................................. 605
72.1 Adding Picture-in-Picture Support to the Manifest................................................................. 605
72.2 Adding a Picture-in-Picture Button ........................................................................................... 605
72.3 Entering Picture-in-Picture Mode .............................................................................................. 605
72.4 Detecting Picture-in-Picture Mode Changes............................................................................ 607
72.5 Adding a Broadcast Receiver....................................................................................................... 608
72.6 Adding the PiP Action.................................................................................................................. 609
72.7 Testing the Picture-in-Picture Action ........................................................................................ 611
72.8 Summary ........................................................................................................................................ 612
73. Making Runtime Permission Requests in Android............................................................................. 613
xviii
Table of Contents
73.1 Understanding Normal and Dangerous Permissions............................................................... 613
73.2 Creating the Permissions Example Project................................................................................ 615
73.3 Checking for a Permission........................................................................................................... 615
73.4 Requesting Permission at Runtime............................................................................................. 617
73.5 Providing a Rationale for the Permission Request ................................................................... 618
73.6 Testing the Permissions App........................................................................................................ 620
73.7 Summary ........................................................................................................................................ 620
74. Android Audio Recording and Playback using MediaPlayer and MediaRecorder ............................ 621
74.1 Playing Audio ................................................................................................................................ 621
74.2 Recording Audio and Video using the MediaRecorder Class................................................. 622
74.3 About the Example Project.......................................................................................................... 623
74.4 Creating the AudioApp Project................................................................................................... 623
74.5 Designing the User Interface ....................................................................................................... 623
74.6 Checking for Microphone Availability....................................................................................... 624
74.7 Initializing the Activity................................................................................................................. 625
74.8 Implementing the recordAudio() Method................................................................................. 626
74.9 Implementing the stopAudio() Method..................................................................................... 627
74.10 Implementing the playAudio() method................................................................................... 627
74.11 Configuring and Requesting Permissions ............................................................................... 627
74.12 Testing the Application...............................................................................................................630
74.13 Summary ...................................................................................................................................... 630
75. Working with the Google Maps Android API in Android Studio ...................................................... 631
75.1 The Elements of the Google Maps Android API ...................................................................... 631
75.2 Creating the Google Maps Project.............................................................................................. 632
75.3 Obtaining Your Developer Signature ......................................................................................... 632
75.4 Adding the Apache HTTP Legacy Library Requirement........................................................ 633
75.5 Testing the Application................................................................................................................. 633
75.6 Understanding Geocoding and Reverse Geocoding ................................................................ 634
75.7 Adding a Map to an Application................................................................................................. 636
75.8 Requesting Current Location Permission.................................................................................. 636
75.9 Displaying the User’s Current Location ..................................................................................... 637
75.10 Changing the Map Type............................................................................................................. 638
75.11 Displaying Map Controls to the User....................................................................................... 639
75.12 Handling Map Gesture Interaction........................................................................................... 640
75.12.1 Map Zooming Gestures....................................................................................................... 640
75.12.2 Map Scrolling/Panning Gestures....................................................................................... 640
75.12.3 Map Tilt Gestures................................................................................................................. 641
75.12.4 Map Rotation Gestures........................................................................................................ 641
75.13 Creating Map Markers................................................................................................................ 641
75.14 Controlling the Map Camera .................................................................................................... 642
75.15 Summary ...................................................................................................................................... 643
76. Printing with the Android Printing Framework ................................................................................. 645
76.1 The Android Printing Architecture ............................................................................................ 645
76.2 The Print Service Plugins............................................................................................................. 645
76.3 Google Cloud Print....................................................................................................................... 646
76.4 Printing to Google Drive.............................................................................................................. 646
76.5 Save as PDF.................................................................................................................................... 647
76.6 Printing from Android Devices .................................................................................................. 647
xix
Table of Contents
76.7 Options for Building Print Support into Android Apps.......................................................... 648
76.7.1 Image Printing........................................................................................................................ 648
76.7.2 Creating and Printing HTML Content ............................................................................... 649
76.7.3 Printing a Web Page............................................................................................................... 650
76.7.4 Printing a Custom Document.............................................................................................. 651
76.8 Summary ........................................................................................................................................ 651
77. An Android HTML and Web Content Printing Example ................................................................... 653
77.1 Creating the HTML Printing Example Application................................................................. 653
77.2 Printing Dynamic HTML Content............................................................................................. 653
77.3 Creating the Web Page Printing Example.................................................................................. 656
77.4 Removing the Floating Action Button ....................................................................................... 656
77.5 Removing Navigation Features....................................................................................................656
77.6 Designing the User Interface Layout.......................................................................................... 658
77.7 Accessing the WebView from the Main Activity ...................................................................... 658
77.8 Loading the Web Page into the WebView.................................................................................. 659
77.9 Adding the Print Menu Option................................................................................................... 660
77.10 Summary ...................................................................................................................................... 661
78. A Guide to Android Custom Document Printing............................................................................... 663
78.1 An Overview of Android Custom Document Printing ........................................................... 663
78.1.1 Custom Print Adapters.......................................................................................................... 663
78.2 Preparing the Custom Document Printing Project.................................................................. 664
78.3 Creating the Custom Print Adapter............................................................................................ 665
78.4 Implementing the onLayout() Callback Method...................................................................... 666
78.5 Implementing the onWrite() Callback Method ........................................................................ 669
78.6 Checking a Page is in Range ........................................................................................................ 671
78.7 Drawing the Content on the Page Canvas................................................................................. 672
78.8 Starting the Print Job .................................................................................................................... 674
78.9 Testing the Application................................................................................................................. 675
78.10 Summary ...................................................................................................................................... 675
79. An Introduction to Android App Links............................................................................................... 677
79.1 An Overview of Android App Links.......................................................................................... 677
79.2 App Link Intent Filters................................................................................................................. 677
79.3 Handling App Link Intents.......................................................................................................... 678
79.4 Associating the App with a Website............................................................................................678
79.5 Summary ........................................................................................................................................ 679
80. An Android Studio App Links Tutorial ............................................................................................... 681
80.1 About the Example App ............................................................................................................... 681
80.2 The Database Schema ................................................................................................................... 681
80.3 Loading and Running the Project............................................................................................... 682
80.4 Adding the URL Mapping............................................................................................................ 683
80.5 Adding the Intent Filter................................................................................................................ 686
80.6 Adding Intent Handling Code..................................................................................................... 686
80.7 Testing the App.............................................................................................................................. 689
80.8 Creating the Digital Asset Links File.......................................................................................... 689
80.9 Testing the App Link..................................................................................................................... 690
80.10 Summary ...................................................................................................................................... 690
81. An Android Biometric Authentication Tutorial.................................................................................. 691
xx
Table of Contents
81.1 An Overview of Biometric Authentication................................................................................ 691
81.2 Creating the Biometric Authentication Project ........................................................................ 691
81.3 Configuring Device Fingerprint Authentication ...................................................................... 692
81.4 Adding the Biometric Permission to the Manifest File............................................................ 692
81.5 Designing the User Interface ....................................................................................................... 693
81.6 Adding a Toast Convenience Method........................................................................................ 693
81.7 Checking the Security Settings.................................................................................................... 694
81.8 Configuring the Authentication Callbacks................................................................................ 695
81.9 Adding the CancellationSignal.................................................................................................... 696
81.10 Starting the Biometric Prompt.................................................................................................. 697
81.11 Testing the Project....................................................................................................................... 697
81.12 Summary ...................................................................................................................................... 698
82. Creating, Testing and Uploading an Android App Bundle................................................................. 699
82.1 The Release Preparation Process................................................................................................. 699
82.2 Android App Bundles................................................................................................................... 699
82.3 Register for a Google Play Developer Console Account.......................................................... 700
82.4 Configuring the App in the Console .......................................................................................... 701
82.5 Enabling Google Play App Signing............................................................................................. 702
82.6 Creating a Keystore File ............................................................................................................... 702
82.7 Creating the Android App Bundle.............................................................................................. 704
82.8 Generating Test APK Files........................................................................................................... 705
82.9 Uploading the App Bundle to the Google Play Developer Console....................................... 706
82.10 Exploring the App Bundle ......................................................................................................... 707
82.11 Managing Testers ........................................................................................................................ 709
82.12 Rolling the App Out for Testing................................................................................................ 709
82.13 Uploading New App Bundle Revisions.................................................................................... 709
82.14 Analyzing the App Bundle File ................................................................................................. 710
82.15 Summary ...................................................................................................................................... 711
83. An Overview of Android Dynamic Feature Modules.......................................................................... 713
83.1 An Overview of Dynamic Feature Modules.............................................................................. 713
83.2 Dynamic Feature Module Architecture ..................................................................................... 713
83.3 Creating a Dynamic Feature Module ......................................................................................... 714
83.4 Converting an Existing Module for Dynamic Delivery........................................................... 716
83.5 Working with Dynamic Feature Modules.................................................................................. 719
83.6 Handling Large Dynamic Feature Modules .............................................................................. 721
83.7 Summary ........................................................................................................................................ 722
84. An Android Studio Dynamic Feature Tutorial.................................................................................... 723
84.1 Creating the DynamicFeature Project........................................................................................ 723
84.2 Adding Dynamic Feature Support to the Project ..................................................................... 723
84.3 Designing the Base Activity User Interface ............................................................................... 724
84.4 Adding the Dynamic Feature Module........................................................................................ 725
84.5 Reviewing the Dynamic Feature Module................................................................................... 726
84.6 Adding the Dynamic Feature Activity........................................................................................ 727
84.7 Implementing the launchIntent() Method................................................................................. 730
84.8 Uploading the App Bundle for Testing....................................................................................... 731
84.9 Implementing the installFeature() Method ............................................................................... 732
84.10 Adding the Update Listener....................................................................................................... 734
84.11 Using Deferred Installation ....................................................................................................... 737
xxi
Table of Contents
84.12 Removing a Dynamic Module .................................................................................................. 737
84.13 Summary ...................................................................................................................................... 737
85. Working with Material Design 3 Theming .......................................................................................... 739
85.1 Material Design 2 vs Material Design 3 ..................................................................................... 739
85.2 Understanding Material Design Theming ................................................................................. 739
85.3 Material Design 2 Theming ......................................................................................................... 739
85.4 Material Design 3 Theming ......................................................................................................... 741
85.5 Building a Custom Theme............................................................................................................742
85.6 Summary ........................................................................................................................................ 743
86. A Material Design 3 Theming and Dynamic Color Tutorial............................................................... 745
86.1 Creating the ThemeDemo Project .............................................................................................. 745
86.2 Designing the User Interface ....................................................................................................... 745
86.3 Migrating to Material Design 3................................................................................................... 747
86.4 Building a New Theme ................................................................................................................. 748
86.5 Adding the Theme to the Project................................................................................................ 749
86.6 Enabling Dynamic Color Support .............................................................................................. 750
86.7 Summary ........................................................................................................................................ 751
87. An Overview of Gradle in Android Studio.......................................................................................... 753
87.1 An Overview of Gradle ................................................................................................................ 753
87.2 Gradle and Android Studio ......................................................................................................... 753
87.2.1 Sensible Defaults .................................................................................................................... 753
87.2.2 Dependencies..........................................................................................................................753
87.2.3 Build Variants......................................................................................................................... 754
87.2.4 Manifest Entries ..................................................................................................................... 754
87.2.5 APK Signing............................................................................................................................ 754
87.2.6 ProGuard Support.................................................................................................................. 754
87.3 The Property and Settings Gradle Build File............................................................................. 754
87.4 The Top-level Gradle Build File................................................................................................... 755
87.5 Module Level Gradle Build Files................................................................................................. 756
87.6 Configuring Signing Settings in the Build File.......................................................................... 758
87.7 Running Gradle Tasks from the Command-line ...................................................................... 759
87.8 Summary ........................................................................................................................................ 759
Index ........................................................................................................................................................... 76