1. Introduction....41
1.1 Downloading the Code Samples....42
1.2 Feedback....42
1.3 Errata....42
2. Setting up an Android Studio Development Environment....43
2.1 System requirements....43
2.2 Downloading the Android Studio package....43
2.3 Installing Android Studio....44
2.3.1 Installation on Windows....44
2.3.2 Installation on macOS....45
2.3.3 Installation on Linux....46
2.4 The Android Studio setup wizard....47
2.5 Installing additional Android SDK packages....48
2.6 Installing the Android SDK Command-line Tools....51
2.6.1 Windows 8.1....53
2.6.2 Windows 10....54
2.6.3 Windows 11....54
2.6.4 Linux....54
2.6.5 macOS....55
2.7 Android Studio memory management....55
2.8 Updating Android Studio and the SDK....57
2.9 Summary....57
3. Creating an Example Android App in Android Studio....58
3.1 About the Project....58
3.2 Creating a New Android Project....58
3.3 Creating an Activity....59
3.4 Defining the Project and SDK Settings....60
3.5 Modifying the Example Application....61
3.6 Modifying the User Interface....62
3.7 Reviewing the Layout and Resource Files....69
3.8 Adding Interaction....73
3.9 Summary....75
4. Creating an Android Virtual Device (AVD) in Android Studio....77
4.1 About Android Virtual Devices....77
4.2 Starting the Emulator....80
4.3 Running the Application in the AVD....81
4.4 Running on Multiple Devices....83
4.5 Stopping a Running Application....83
4.6 Supporting Dark Theme....84
4.7 Running the Emulator in a Separate Window....85
4.8 Enabling the Device Frame....87
4.9 Summary....87
5. Using and Configuring the Android Studio AVD Emulator....89
5.1 The Emulator Environment....89
5.2 Emulator Toolbar Options....89
5.3 Working in Zoom Mode....91
5.4 Resizing the Emulator Window....92
5.5 Extended Control Options....92
5.5.1 Location....92
5.5.2 Displays....93
5.5.3 Cellular....93
5.5.4 Battery....93
5.5.5 Camera....93
5.5.6 Phone....93
5.5.7 Directional Pad....94
5.5.8 Microphone....94
5.5.9 Fingerprint....94
5.5.10 Virtual Sensors....94
5.5.11 Snapshots....94
5.5.12 Record and Playback....95
5.5.13 Google Play....95
5.5.14 Settings....95
5.5.15 Help....95
5.6 Working with Snapshots....95
5.7 Configuring Fingerprint Emulation....97
5.8 The Emulator in Tool Window Mode....98
5.9 Creating a Resizable Emulator....99
5.10 Summary....100
6. A Tour of the Android Studio User Interface....101
6.1 The Welcome Screen....101
6.2 The Main Window....102
6.3 The Tool Windows....104
6.4 Android Studio Keyboard Shortcuts....109
6.5 Switcher and Recent Files Navigation....110
6.6 Changing the Android Studio Theme....111
6.7 Summary....112
7. Testing Android Studio Apps on a Physical Android Device....113
7.1 An Overview of the Android Debug Bridge (ADB)....113
7.2 Enabling USB Debugging ADB on Android Devices....114
7.2.1 macOS ADB Configuration....115
7.2.2 Windows ADB Configuration....116
7.2.3 Linux adb Configuration....117
7.3 Resolving USB Connection Issues....118
7.4 Enabling Wireless Debugging on Android Devices....118
7.5 Testing the adb Connection....121
7.6 Device Mirroring....121
7.7 Summary....121
8. The Basics of the Android Studio Code Editor....123
8.1 The Android Studio Editor....123
8.2 Splitting the Editor Window....127
8.3 Code Completion....128
8.4 Statement Completion....129
8.5 Parameter Information....130
8.6 Parameter Name Hints....130
8.7 Code Generation....131
8.8 Code Folding....132
8.9 Quick Documentation Lookup....134
8.10 Code Reformatting....134
8.11 Finding Sample Code....135
8.12 Live Templates....136
8.13 Summary....137
9. An Overview of the Android Architecture....138
9.1 The Android Software Stack....138
9.2 The Linux Kernel....139
9.3 Android Runtime – ART....140
9.4 Android Libraries....140
9.4.1 C/C++ Libraries....142
9.5 Application Framework....142
9.6 Applications....143
9.7 Summary....144
10. The Anatomy of an Android Application....145
10.1 Android Activities....145
10.2 Android Fragments....146
10.3 Android Intents....147
10.4 Broadcast Intents....147
10.5 Broadcast Receivers....147
10.6 Android Services....148
10.7 Content Providers....148
10.8 The Application Manifest....149
10.9 Application Resources....149
10.10 Application Context....149
10.11 Summary....150
11. An Overview of Android View Binding....151
11.1 Find View by Id....151
11.2 View Binding....152
11.3 Converting the AndroidSample project....152
11.4 Enabling View Binding....152
11.5 Using View Binding....153
11.6 Choosing an Option....155
11.7 View Binding in the Book Examples....155
11.8 Migrating a Project to View Binding....155
11.9 Summary....157
12. Understanding Android Application and Activity Lifecycles....158
12.1 Android Applications and Resource Management....158
12.2 Android Process States....159
12.2.1 Foreground Process....159
12.2.2 Visible Process....160
12.2.3 Service Process....160
12.2.4 Background Process....160
12.2.5 Empty Process....160
12.3 Inter-Process Dependencies....161
12.4 The Activity Lifecycle....161
12.5 The Activity Stack....161
12.6 Activity States....162
12.7 Configuration Changes....163
12.8 Handling State Change....164
12.9 Summary....165
13. Handling Android Activity State Changes....166
13.1 New vs. Old Lifecycle Techniques....166
13.2 The Activity and Fragment Classes....167
13.3 Dynamic State vs. Persistent State....169
13.4 The Android Lifecycle Methods....171
13.5 Lifetimes....173
13.6 Foldable Devices and Multi-Resume....174
13.7 Disabling Configuration Change Restarts....175
13.8 Lifecycle Method Limitations....175
13.9 Summary....176
14. Android Activity State Changes by Example....178
14.1 Creating the State Change Example Project....178
14.2 Designing the User Interface....179
14.3 Overriding the Activity Lifecycle Methods....181
14.4 Filtering the Logcat Panel....184
14.5 Running the Application....186
14.6 Experimenting with the Activity....186
14.7 Summary....188
15. Saving and Restoring the State of an Android Activity....189
15.1 Saving Dynamic State....189
15.2 Default Saving of User Interface State....189
15.3 The Bundle Class....191
15.4 Saving the State....192
15.5 Restoring the State....194
15.6 Testing the Application....195
15.7 Summary....195
16. Understanding Android Views, View Groups and Layouts....197
16.1 Designing for Different Android Devices....197
16.2 Views and View Groups....198
16.3 Android Layout Managers....198
16.4 The View Hierarchy....200
16.5 Creating User Interfaces....202
16.6 Summary....202
17. A Guide to the Android Studio Layout Editor Tool....204
17.1 Basic vs. Empty Views Activity Templates....204
17.2 The Android Studio Layout Editor....210
17.3 Design Mode....210
17.4 The Palette....212
17.5 Design Mode and Layout Views....213
17.6 Night Mode....214
17.7 Code Mode....214
17.8 Split Mode....215
17.9 Setting Attributes....216
17.10 Transforms....218
17.11 Tools Visibility Toggles....218
17.12 Converting Views....221
17.13 Displaying Sample Data....222
17.14 Creating a Custom Device Definition....223
17.15 Changing the Current Device....224
17.16 Layout Validation....224
17.17 Summary....225
18. A Guide to the Android ConstraintLayout....227
18.1 How ConstraintLayout Works....227
18.1.1 Constraints....228
18.1.2 Margins....228
18.1.3 Opposing Constraints....229
18.1.4 Constraint Bias....230
18.1.5 Chains....231
18.1.6 Chain Styles....232
18.2 Baseline Alignment....233
18.3 Configuring Widget Dimensions....233
18.4 Guideline Helper....234
18.5 Group Helper....234
18.6 Barrier Helper....235
18.7 Flow Helper....237
18.8 Ratios....238
18.9 ConstraintLayout Advantages....239
18.10 ConstraintLayout Availability....239
18.11 Summary....239
19. A Guide to Using ConstraintLayout in Android Studio....241
19.1 Design and Layout Views....241
19.2 Autoconnect Mode....243
19.3 Inference Mode....243
19.4 Manipulating Constraints Manually....244
19.5 Adding Constraints in the Inspector....246
19.6 Viewing Constraints in the Attributes Window....246
19.7 Deleting Constraints....247
19.8 Adjusting Constraint Bias....248
19.9 Understanding ConstraintLayout Margins....248
19.10 The Importance of Opposing Constraints and Bias....250
19.11 Configuring Widget Dimensions....252
19.12 Design Time Tools Positioning....254
19.13 Adding Guidelines....255
19.14 Adding Barriers....257
19.15 Adding a Group....259
19.16 Working with the Flow Helper....260
19.17 Widget Group Alignment and Distribution....261
19.18 Converting other Layouts to ConstraintLayout....262
19.19 Summary....263
20. Working with ConstraintLayout Chains and Ratios in Android Studio....264
20.1 Creating a Chain....264
20.2 Changing the Chain Style....267
20.3 Spread Inside Chain Style....267
20.4 Packed Chain Style....268
20.5 Packed Chain Style with Bias....268
20.6 Weighted Chain....268
20.7 Working with Ratios....270
20.8 Summary....272
21. An Android Studio Layout Editor ConstraintLayout Tutorial....273
21.1 An Android Studio Layout Editor Tool Example....273
21.2 Preparing the Layout Editor Environment....273
21.3 Adding the Widgets to the User Interface....275
21.4 Adding the Constraints....278
21.5 Testing the Layout....280
21.6 Using the Layout Inspector....281
21.7 Summary....282
22. Manual XML Layout Design in Android Studio....283
22.1 Manually Creating an XML Layout....283
22.2 Manual XML vs. Visual Layout Design....287
22.3 Summary....288
23. Managing Constraints using Constraint Sets....289
23.1 Java Code vs. XML Layout Files....289
23.2 Creating Views....290
23.3 View Attributes....291
23.4 Constraint Sets....291
23.4.1 Establishing Connections....292
23.4.2 Applying Constraints to a Layout....292
23.4.3 Parent Constraint Connections....292
23.4.4 Sizing Constraints....292
23.4.5 Constraint Bias....293
23.4.6 Alignment Constraints....293
23.4.7 Copying and Applying Constraint Sets....293
23.4.8 ConstraintLayout Chains....294
23.4.9 Guidelines....295
23.4.10 Removing Constraints....295
23.4.11 Scaling....295
23.4.12 Rotation....296
23.5 Summary....296
24. An Android ConstraintSet Tutorial....298
24.1 Creating the Example Project in Android Studio....298
24.2 Adding Views to an Activity....298
24.3 Setting View Attributes....300
24.4 Creating View IDs....301
24.5 Configuring the Constraint Set....302
24.6 Adding the EditText View....304
24.7 Converting Density Independent Pixels (dp) to Pixels (px)....306
24.8 Summary....307
25. A Guide to using Apply Changes in Android Studio....308
25.1 Introducing Apply Changes....308
25.2 Understanding Apply Changes Options....309
25.3 Using Apply Changes....309
25.4 Configuring Apply Changes Fallback Settings....311
25.5 An Apply Changes Tutorial....311
25.6 Using Apply Code Changes....312
25.7 Using Apply Changes and Restart Activity....313
25.8 Using Run App....313
25.9 Summary....314
26. An Overview and Example of Android Event Handling....315
26.1 Understanding Android Events....315
26.2 Using the android:onClick Resource....316
26.3 Event Listeners and Callback Methods....317
26.4 An Event Handling Example....318
26.5 Designing the User Interface....318
26.6 The Event Listener and Callback Method....319
26.7 Consuming Events....322
26.8 Summary....324
27. Android Touch and Multi-touch Event Handling....325
27.1 Intercepting Touch Events....325
27.2 The MotionEvent Object....326
27.3 Understanding Touch Actions....326
27.4 Handling Multiple Touches....327
27.5 An Example Multi-Touch Application....328
27.6 Designing the Activity User Interface....328
27.7 Implementing the Touch Event Listener....329
27.8 Running the Example Application....333
27.9 Summary....334
28. Detecting Common Gestures Using the Android Gesture Detector Class....336
28.1 Implementing Common Gesture Detection....336
28.2 Creating an Example Gesture Detection Project....338
28.3 Implementing the Listener Class....338
28.4 Creating the GestureDetectorCompat Instance....341
28.5 Implementing the onTouchEvent() Method....342
28.6 Testing the Application....343
28.7 Summary....343
29. Implementing Custom Gesture and Pinch Recognition on Android....345
29.1 The Android Gesture Builder Application....345
29.2 The GestureOverlayView Class....345
29.3 Detecting Gestures....346
29.4 Identifying Specific Gestures....346
29.5 Installing and Running the Gesture Builder Application....346
29.6 Creating a Gestures File....347
29.7 Creating the Example Project....347
29.8 Extracting the Gestures File from the SD Card....348
29.9 Adding the Gestures File to the Project....349
29.10 Designing the User Interface....349
29.11 Loading the Gestures File....350
29.12 Registering the Event Listener....352
29.13 Implementing the onGesturePerformed Method....352
29.14 Testing the Application....354
29.15 Configuring the GestureOverlayView....354
29.16 Intercepting Gestures....355
29.17 Detecting Pinch Gestures....355
29.18 A Pinch Gesture Example Project....356
29.19 Summary....359
30. An Introduction to Android Fragments....361
30.1 What is a Fragment?....361
30.2 Creating a Fragment....362
30.3 Adding a Fragment to an Activity using the Layout XML File....363
30.4 Adding and Managing Fragments in Code....365
30.5 Handling Fragment Events....367
30.6 Implementing Fragment Communication....368
30.7 Summary....370
31. Using Fragments in Android Studio - An Example....372
31.1 About the Example Fragment Application....372
31.2 Creating the Example Project....373
31.3 Creating the First Fragment Layout....373
31.4 Migrating a Fragment to View Binding....375
31.5 Adding the Second Fragment....376
31.6 Adding the Fragments to the Activity....378
31.7 Making the Toolbar Fragment Talk to the Activity....380
31.8 Making the Activity Talk to the Text Fragment....384
31.9 Testing the Application....385
31.10 Summary....386
32. Modern Android App Architecture with Jetpack....387
32.1 What is Android Jetpack?....387
32.2 The “Old” Architecture....388
32.3 Modern Android Architecture....388
32.4 The ViewModel Component....388
32.5 The LiveData Component....389
32.6 ViewModel Saved State....391
32.7 LiveData and Data Binding....392
32.8 Android Lifecycles....393
32.9 Repository Modules....393
32.10 Summary....394
33. An Android ViewModel Tutorial....396
33.1 About the Project....396
33.2 Creating the ViewModel Example Project....396
33.3 Removing Unwanted Project Elements....397
33.4 Designing the Fragment Layout....398
33.5 Implementing the View Model....400
33.6 Associating the Fragment with the View Model....401
33.7 Modifying the Fragment....402
33.8 Accessing the ViewModel Data....403
33.9 Testing the Project....404
33.10 Summary....405
34. An Android Jetpack LiveData Tutorial....406
34.1 LiveData - A Recap....406
34.2 Adding LiveData to the ViewModel....406
34.3 Implementing the Observer....409
34.4 Summary....412
35. An Overview of Android Jetpack Data Binding....413
35.1 An Overview of Data Binding....413
35.2 The Key Components of Data Binding....414
35.2.1 The Project Build Configuration....414
35.2.2 The Data Binding Layout File....415
35.2.3 The Layout File Data Element....416
35.2.4 The Binding Classes....417
35.2.5 Data Binding Variable Configuration....419
35.2.6 Binding Expressions (One-Way)....419
35.2.7 Binding Expressions (Two-Way)....422
35.2.8 Event and Listener Bindings....422
35.3 Summary....423
36. An Android Jetpack Data Binding Tutorial....424
36.1 Removing the Redundant Code....424
36.2 Enabling Data Binding....426
36.3 Adding the Layout Element....427
36.4 Adding the Data Element to Layout File....429
36.5 Working with the Binding Class....429
36.6 Assigning the ViewModel Instance to the Data Binding Variable....431
36.7 Adding Binding Expressions....431
36.8 Adding the Conversion Method....433
36.9 Adding a Listener Binding....434
36.10 Testing the App....434
36.11 Summary....434
37. An Android ViewModel Saved State Tutorial....436
37.1 Understanding ViewModel State Saving....436
37.2 Implementing ViewModel State Saving....437
37.3 Saving and Restoring State....439
37.4 Adding Saved State Support to the ViewModelDemo Project....440
37.5 Summary....443
38. Working with Android Lifecycle-Aware Components....444
38.1 Lifecycle Awareness....444
38.2 Lifecycle Owners....445
38.3 Lifecycle Observers....446
38.4 Lifecycle States and Events....447
38.5 Summary....448
39. An Android Jetpack Lifecycle Awareness Tutorial....449
39.1 Creating the Example Lifecycle Project....449
39.2 Creating a Lifecycle Observer....449
39.3 Adding the Observer....451
39.4 Testing the Observer....452
39.5 Creating a Lifecycle Owner....453
39.6 Testing the Custom Lifecycle Owner....456
39.7 Summary....456
40. An Overview of the Navigation Architecture Component....457
40.1 Understanding Navigation....457
40.2 Declaring a Navigation Host....459
40.3 The Navigation Graph....461
40.4 Accessing the Navigation Controller....463
40.5 Triggering a Navigation Action....464
40.6 Passing Arguments....464
40.7 Summary....465
41. An Android Jetpack Navigation Component Tutorial....467
41.1 Creating the NavigationDemo Project....467
41.2 Adding Navigation to the Build Configuration....467
41.3 Creating the Navigation Graph Resource File....468
41.4 Declaring a Navigation Host....470
41.5 Adding Navigation Destinations....472
41.6 Designing the Destination Fragment Layouts....474
41.7 Adding an Action to the Navigation Graph....475
41.8 Implement the OnFragmentInteractionListener....477
41.9 Adding View Binding Support to the Destination Fragments....479
41.10 Triggering the Action....480
41.11 Passing Data Using Safeargs....481
41.12 Summary....486
42. An Introduction to MotionLayout....487
42.1 An Overview of MotionLayout....487
42.2 MotionLayout....488
42.3 MotionScene....488
42.4 Configuring ConstraintSets....490
42.5 Custom Attributes....491
42.6 Triggering an Animation....493
42.7 Arc Motion....494
42.8 Keyframes....495
42.8.1 Attribute Keyframes....496
42.8.2 Position Keyframes....496
42.9 Time Linearity....500
42.10 KeyTrigger....501
42.11 Cycle and Time Cycle Keyframes....502
42.12 Starting an Animation from Code....502
42.13 Summary....503
43. An Android MotionLayout Editor Tutorial....505
43.1 Creating the MotionLayoutDemo Project....505
43.2 ConstraintLayout to MotionLayout Conversion....505
43.3 Configuring Start and End Constraints....507
43.4 Previewing the MotionLayout Animation....511
43.5 Adding an OnClick Gesture....511
43.6 Adding an Attribute Keyframe to the Transition....513
43.7 Adding a CustomAttribute to a Transition....516
43.8 Adding Position Keyframes....518
43.9 Summary....521
44. A MotionLayout KeyCycle Tutorial....522
44.1 An Overview of Cycle Keyframes....522
44.2 Using the Cycle Editor....526
44.3 Creating the KeyCycleDemo Project....527
44.4 Configuring the Start and End Constraints....528
44.5 Creating the Cycles....530
44.6 Previewing the Animation....532
44.7 Adding the KeyFrameSet to the MotionScene....532
44.8 Summary....535
45. Working with the Floating Action Button and Snackbar....536
45.1 The Material Design....536
45.2 The Design Library....537
45.3 The Floating Action Button (FAB)....537
45.4 The Snackbar....538
45.5 Creating the Example Project....539
45.6 Reviewing the Project....539
45.7 Removing Navigation Features....541
45.8 Changing the Floating Action Button....542
45.9 Adding an Action to the Snackbar....544
45.10 Summary....545
46. Creating a Tabbed Interface using the TabLayout Component....546
46.1 An Introduction to the ViewPager2....546
46.2 An Overview of the TabLayout Component....546
46.3 Creating the TabLayoutDemo Project....547
46.4 Creating the First Fragment....549
46.5 Duplicating the Fragments....550
46.6 Adding the TabLayout and ViewPager2....552
46.7 Performing the Initialization Tasks....555
46.8 Testing the Application....557
46.9 Customizing the TabLayout....558
46.10 Summary....560
47. Working with the RecyclerView and CardView Widgets....561
47.1 An Overview of the RecyclerView....561
47.2 An Overview of the CardView....564
47.3 Summary....566
48. An Android RecyclerView and CardView Tutorial....567
48.1 Creating the CardDemo Project....567
48.2 Modifying the Basic Views Activity Project....567
48.3 Designing the CardView Layout....568
48.4 Adding the RecyclerView....570
48.5 Adding the Image Files....571
48.6 Creating the RecyclerView Adapter....571
48.7 Initializing the RecyclerView Component....574
48.8 Testing the Application....576
48.9 Responding to Card Selections....577
48.10 Summary....579
49. A Layout Editor Sample Data Tutorial....580
49.1 Adding Sample Data to a Project....580
49.2 Using Custom Sample Data....585
49.3 Summary....589
50. Working with the AppBar and Collapsing Toolbar Layouts....590
50.1 The Anatomy of an AppBar....590
50.2 The Example Project....591
50.3 Coordinating the RecyclerView and Toolbar....592
50.4 Introducing the Collapsing Toolbar Layout....594
50.5 Changing the Title and Scrim Color....599
50.6 Summary....600
51. An Android Studio Primary/Detail Flow Tutorial....601
51.1 The Primary/Detail Flow....601
51.2 Creating a Primary/Detail Flow Activity....602
51.3 Adding the Primary/Detail Flow Activity....603
51.4 Modifying the Primary/Detail Flow Template....604
51.5 Changing the Content Model....604
51.6 Changing the Detail Pane....606
51.7 Modifying the ItemDetailFragment Class....608
51.8 Modifying the ItemListFragment Class....609
51.9 Adding Manifest Permissions....610
51.10 Running the Application....611
51.11 Summary....612
52. An Overview of Android Services....613
52.1 Intent Service....613
52.2 Bound Service....614
52.3 The Anatomy of a Service....615
52.4 Controlling Destroyed Service Restart Options....616
52.5 Declaring a Service in the Manifest File....616
52.6 Starting a Service Running on System Startup....618
52.7 Summary....618
53. An Overview of Android Intents....620
53.1 An Overview of Intents....620
53.2 Explicit Intents....621
53.3 Returning Data from an Activity....623
53.4 Implicit Intents....624
53.5 Using Intent Filters....625
53.6 Automatic Link Verification....626
53.7 Manually Enabling Links....630
53.8 Checking Intent Availability....631
53.9 Summary....632
54. Android Explicit Intents – A Worked Example....634
54.1 Creating the Explicit Intent Example Application....634
54.2 Designing the User Interface Layout for MainActivity....634
54.3 Creating the Second Activity Class....636
54.4 Designing the User Interface Layout for SecondActivity....636
54.5 Reviewing the Application Manifest File....637
54.6 Creating the Intent....638
54.7 Extracting Intent Data....640
54.8 Launching SecondActivity as a Sub-Activity....641
54.9 Returning Data from a Sub-Activity....643
54.10 Testing the Application....643
54.11 Summary....644
55. Android Implicit Intents – A Worked Example....645
55.1 Creating the Android Studio Implicit Intent Example Project....645
55.2 Designing the User Interface....645
55.3 Creating the Implicit Intent....646
55.4 Adding a Second Matching Activity....648
55.5 Adding the Web View to the UI....648
55.6 Obtaining the Intent URL....649
55.7 Modifying the MyWebView Project Manifest File....651
55.8 Installing the MyWebView Package on a Device....653
55.9 Testing the Application....654
55.10 Manually Enabling the Link....654
55.11 Automatic Link Verification....656
55.12 Summary....658
56. Android Broadcast Intents and Broadcast Receivers....659
56.1 An Overview of Broadcast Intents....659
56.2 An Overview of Broadcast Receivers....660
56.3 Obtaining Results from a Broadcast....663
56.4 Sticky Broadcast Intents....663
56.5 The Broadcast Intent Example....664
56.6 Creating the Example Application....664
56.7 Creating and Sending the Broadcast Intent....664
56.8 Creating the Broadcast Receiver....665
56.9 Registering the Broadcast Receiver....667
56.10 Testing the Broadcast Example....668
56.11 Listening for System Broadcasts....669
56.12 Summary....670
57. Android Local Bound Services – A Worked Example....671
57.1 Understanding Bound Services....671
57.2 Bound Service Interaction Options....671
57.3 A Local Bound Service Example....672
57.4 Adding a Bound Service to the Project....673
57.5 Implementing the Binder....673
57.6 Binding the Client to the Service....677
57.7 Completing the Example....679
57.8 Testing the Application....681
57.9 Summary....681
58. Android Remote Bound Services – A Worked Example....682
58.1 Client to Remote Service Communication....682
58.2 Creating the Example Application....683
58.3 Designing the User Interface....683
58.4 Implementing the Remote Bound Service....683
58.5 Configuring a Remote Service in the Manifest File....685
58.6 Launching and Binding to the Remote Service....687
58.7 Sending a Message to the Remote Service....689
58.8 Summary....689
59. A Basic Overview of Java Threads, Handlers and Executors....691
59.1 The Application Main Thread....691
59.2 Thread Handlers....691
59.3 A Threading Example....692
59.4 Building the App....692
59.5 Creating a New Thread....694
59.6 Implementing a Thread Handler....696
59.7 Passing a Message to the Handler....698
59.8 Java Executor Concurrency....699
59.9 Working with Runnable Tasks....700
59.10 Shutting down an Executor Service....702
59.11 Working with Callable Tasks and Futures....702
59.12 Handling a Future Result....704
59.13 Scheduling Tasks....706
59.14 Summary....707
60. Making Runtime Permission Requests in Android....708
60.1 Understanding Normal and Dangerous Permissions....708
60.2 Creating the Permissions Example Project....711
60.3 Checking for a Permission....711
60.4 Requesting Permission at Runtime....714
60.5 Providing a Rationale for the Permission Request....716
60.6 Testing the Permissions App....718
60.7 Summary....719
61. An Android Notifications Tutorial....720
61.1 An Overview of Notifications....720
61.2 Creating the NotifyDemo Project....722
61.3 Designing the User Interface....722
61.4 Creating the Second Activity....723
61.5 Creating a Notification Channel....724
61.6 Requesting Notification Permission....726
61.7 Creating and Issuing a Notification....730
61.8 Launching an Activity from a Notification....733
61.9 Adding Actions to a Notification....735
61.10 Bundled Notifications....737
61.11 Summary....739
62. An Android Direct Reply Notification Tutorial....741
62.1 Creating the DirectReply Project....741
62.2 Designing the User Interface....741
62.3 Requesting Notification Permission....742
62.4 Creating the Notification Channel....744
62.5 Building the RemoteInput Object....746
62.6 Creating the PendingIntent....748
62.7 Creating the Reply Action....748
62.8 Receiving Direct Reply Input....752
62.9 Updating the Notification....753
62.10 Summary....755
63. Foldable Devices and Multi-Window Support....756
63.1 Foldables and Multi-Window Support....756
63.2 Using a Foldable Emulator....757
63.3 Entering Multi-Window Mode....758
63.4 Enabling and using Freeform Support....759
63.5 Checking for Freeform Support....760
63.6 Enabling Multi-Window Support in an App....761
63.7 Specifying Multi-Window Attributes....761
63.8 Detecting Multi-Window Mode in an Activity....763
63.9 Receiving Multi-Window Notifications....763
63.10 Launching an Activity in Multi-Window Mode....764
63.11 Configuring Freeform Activity Size and Position....765
63.12 Summary....766
64. An Overview of Android SQLite Databases....768
64.1 Understanding Database Tables....768
64.2 Introducing Database Schema....769
64.3 Columns and Data Types....769
64.4 Database Rows....769
64.5 Introducing Primary Keys....770
64.6 What is SQLite?....770
64.7 Structured Query Language (SQL)....771
64.8 Trying SQLite on an Android Virtual Device (AVD)....772
64.9 The Android Room Persistence Library....775
64.10 Summary....775
65. The Android Room Persistence Library....777
65.1 Revisiting Modern App Architecture....777
65.2 Key Elements of Room Database Persistence....778
65.2.1 Repository....778
65.2.2 Room Database....778
65.2.3 Data Access Object (DAO)....778
65.2.4 Entities....778
65.2.5 SQLite Database....779
65.3 Understanding Entities....780
65.4 Data Access Objects....784
65.5 The Room Database....786
65.6 The Repository....787
65.7 In-Memory Databases....789
65.8 Database Inspector....789
65.9 Summary....790
66. An Android TableLayout and TableRow Tutorial....791
66.1 The TableLayout and TableRow Layout Views....791
66.2 Creating the Room Database Project....793
66.3 Converting to a LinearLayout....793
66.4 Adding the TableLayout to the User Interface....794
66.5 Configuring the TableRows....795
66.6 Adding the Button Bar to the Layout....796
66.7 Adding the RecyclerView....797
66.8 Adjusting the Layout Margins....798
66.9 Summary....798
67. An Android Room Database and Repository Tutorial....800
67.1 About the RoomDemo Project....800
67.2 Modifying the Build Configuration....800
67.3 Building the Entity....801
67.4 Creating the Data Access Object....803
67.5 Adding the Room Database....805
67.6 Adding the Repository....806
67.7 Adding the ViewModel....810
67.8 Creating the Product Item Layout....812
67.9 Adding the RecyclerView Adapter....813
67.10 Preparing the Main Activity....815
67.11 Adding the Button Listeners....816
67.12 Adding LiveData Observers....817
67.13 Initializing the RecyclerView....819
67.14 Testing the RoomDemo App....819
67.15 Using the Database Inspector....820
67.16 Summary....821
68. Accessing Cloud Storage using the Android Storage Access Framework....822
68.1 The Storage Access Framework....822
68.2 Working with the Storage Access Framework....823
68.3 Filtering Picker File Listings....824
68.4 Handling Intent Results....825
68.5 Reading the Content of a File....826
68.6 Writing Content to a File....827
68.7 Deleting a File....828
68.8 Gaining Persistent Access to a File....828
68.9 Summary....829
69. An Android Storage Access Framework Example....831
69.1 About the Storage Access Framework Example....831
69.2 Creating the Storage Access Framework Example....831
69.3 Designing the User Interface....832
69.4 Adding the Activity Launchers....833
69.5 Creating a New Storage File....835
69.6 Saving to a Storage File....836
69.7 Opening and Reading a Storage File....838
69.8 Testing the Storage Access Application....840
69.9 Summary....842
70. Video Playback on Android using the VideoView and MediaController Classes....843
70.1 Introducing the Android VideoView Class....843
70.2 Introducing the Android MediaController Class....844
70.3 Creating the Video Playback Example....845
70.4 Designing the VideoPlayer Layout....846
70.5 Downloading the Video File....846
70.6 Configuring the VideoView....847
70.7 Adding the MediaController to the Video View....849
70.8 Setting up the onPreparedListener....850
70.9 Summary....851
71. Android Picture-in-Picture Mode....852
71.1 Picture-in-Picture Features....852
71.2 Enabling Picture-in-Picture Mode....853
71.3 Configuring Picture-in-Picture Parameters....854
71.4 Entering Picture-in-Picture Mode....855
71.5 Detecting Picture-in-Picture Mode Changes....855
71.6 Adding Picture-in-Picture Actions....856
71.7 Summary....857
72. An Android Picture-in-Picture Tutorial....859
72.1 Adding Picture-in-Picture Support to the Manifest....859
72.2 Adding a Picture-in-Picture Button....859
72.3 Entering Picture-in-Picture Mode....860
72.4 Detecting Picture-in-Picture Mode Changes....862
72.5 Adding a Broadcast Receiver....863
72.6 Adding the PiP Action....865
72.7 Testing the Picture-in-Picture Action....868
72.8 Summary....869
73. Android Audio Recording and Playback using MediaPlayer and MediaRecorder....870
73.1 Playing Audio....870
73.2 Recording Audio and Video using the MediaRecorder Class....871
73.3 About the Example Project....873
73.4 Creating the AudioApp Project....874
73.5 Designing the User Interface....874
73.6 Checking for Microphone Availability....875
73.7 Initializing the Activity....876
73.8 Implementing the recordAudio() Method....878
73.9 Implementing the stopAudio() Method....878
73.10 Implementing the playAudio() method....879
73.11 Configuring and Requesting Permissions....880
73.12 Testing the Application....883
73.13 Summary....883
74. Working with the Google Maps Android API in Android Studio....885
74.1 The Elements of the Google Maps Android API....885
74.2 Creating the Google Maps Project....887
74.3 Creating a Google Cloud Billing Account....888
74.4 Creating a New Google Cloud Project....888
74.5 Enabling the Google Maps SDK....890
74.6 Generating a Google Maps API Key....891
74.7 Adding the API Key to the Android Studio Project....891
74.8 Testing the Application....892
74.9 Understanding Geocoding and Reverse Geocoding....893
74.10 Adding a Map to an Application....895
74.11 Requesting Current Location Permission....896
74.12 Displaying the User’s Current Location....898
74.13 Changing the Map Type....900
74.14 Displaying Map Controls to the User....901
74.15 Handling Map Gesture Interaction....902
74.15.1 Map Zooming Gestures....902
74.15.2 Map Scrolling/Panning Gestures....903
74.15.3 Map Tilt Gestures....903
74.15.4 Map Rotation Gestures....904
74.16 Creating Map Markers....904
74.17 Controlling the Map Camera....905
74.18 Summary....908
75. Printing with the Android Printing Framework....909
75.1 The Android Printing Architecture....909
75.2 The Print Service Plugins....909
75.3 Google Cloud Print....910
75.4 Printing to Google Drive....911
75.5 Save as PDF....911
75.6 Printing from Android Devices....912
75.7 Options for Building Print Support into Android Apps....913
75.7.1 Image Printing....913
75.7.2 Creating and Printing HTML Content....914
75.7.3 Printing a Web Page....917
75.7.4 Printing a Custom Document....918
75.8 Summary....919
76. An Android HTML and Web Content Printing Example....920
76.1 Creating the HTML Printing Example Application....920
76.2 Printing Dynamic HTML Content....920
76.3 Creating the Web Page Printing Example....924
76.4 Removing the Floating Action Button....924
76.5 Removing Navigation Features....925
76.6 Designing the User Interface Layout....927
76.7 Accessing the WebView from the Main Activity....928
76.8 Loading the Web Page into the WebView....929
76.9 Adding the Print Menu Option....930
76.10 Summary....933
77. A Guide to Android Custom Document Printing....934
77.1 An Overview of Android Custom Document Printing....934
77.1.1 Custom Print Adapters....935
77.2 Preparing the Custom Document Printing Project....936
77.3 Creating the Custom Print Adapter....937
77.4 Implementing the onLayout() Callback Method....939
77.5 Implementing the onWrite() Callback Method....943
77.6 Checking a Page is in Range....947
77.7 Drawing the Content on the Page Canvas....948
77.8 Starting the Print Job....951
77.9 Testing the Application....952
77.10 Summary....953
78. An Introduction to Android App Links....954
78.1 An Overview of Android App Links....954
78.2 App Link Intent Filters....955
78.3 Handling App Link Intents....956
78.4 Associating the App with a Website....957
78.5 Summary....957
79. An Android Studio App Links Tutorial....958
79.1 About the Example App....958
79.2 The Database Schema....959
79.3 Loading and Running the Project....959
79.4 Adding the URL Mapping....961
79.5 Adding the Intent Filter....964
79.6 Adding Intent Handling Code....965
79.7 Testing the App....969
79.8 Creating the Digital Asset Links File....969
79.9 Testing the App Link....970
79.10 Summary....971
80. An Android Biometric Authentication Tutorial....972
80.1 An Overview of Biometric Authentication....972
80.2 Creating the Biometric Authentication Project....973
80.3 Configuring Device Fingerprint Authentication....973
80.4 Adding the Biometric Permission to the Manifest File....974
80.5 Designing the User Interface....975
80.6 Adding a Toast Convenience Method....975
80.7 Checking the Security Settings....976
80.8 Configuring the Authentication Callbacks....978
80.9 Adding the CancellationSignal....979
80.10 Starting the Biometric Prompt....980
80.11 Testing the Project....981
80.12 Summary....983
81. Creating, Testing and Uploading an Android App Bundle....984
81.1 The Release Preparation Process....984
81.2 Android App Bundles....984
81.3 Register for a Google Play Developer Console Account....986
81.4 Configuring the App in the Console....987
81.5 Enabling Google Play App Signing....988
81.6 Creating a Keystore File....989
81.7 Creating the Android App Bundle....991
81.8 Generating Test APK Files....993
81.9 Uploading the App Bundle to the Google Play Developer Console....995
81.10 Exploring the App Bundle....996
81.11 Managing Testers....997
81.12 Rolling the App Out for Testing....997
81.13 Uploading New App Bundle Revisions....998
81.14 Analyzing the App Bundle File....999
81.15 Summary....1000
82. An Overview of Android In-App Billing....1001
82.1 Preparing a Project for In-App Purchasing....1001
82.2 Creating In-App Products and Subscriptions....1002
82.3 Billing Client Initialization....1003
82.4 Connecting to the Google Play Billing Library....1004
82.5 Querying Available Products....1005
82.6 Starting the Purchase Process....1006
82.7 Completing the Purchase....1007
82.8 Querying Previous Purchases....1009
82.9 Summary....1010
83. An Android In-App Purchasing Tutorial....1011
83.1 About the In-App Purchasing Example Project....1011
83.2 Creating the InAppPurchase Project....1011
83.3 Adding Libraries to the Project....1012
83.4 Designing the User Interface....1012
83.5 Adding the App to the Google Play Store....1013
83.6 Creating an In-App Product....1014
83.7 Enabling License Testers....1015
83.8 Initializing the Billing Client....1015
83.9 Querying the Product....1018
83.10 Launching the Purchase Flow....1019
83.11 Handling Purchase Updates....1020
83.12 Consuming the Product....1021
83.13 Restoring a Previous Purchase....1022
83.14 Testing the App....1024
83.15 Troubleshooting....1026
83.16 Summary....1027
84. An Overview of Android Dynamic Feature Modules....1028
84.1 An Overview of Dynamic Feature Modules....1028
84.2 Dynamic Feature Module Architecture....1029
84.3 Creating a Dynamic Feature Module....1031
84.4 Converting an Existing Module for Dynamic Delivery....1033
84.5 Working with Dynamic Feature Modules....1036
84.6 Handling Large Dynamic Feature Modules....1039
84.7 Summary....1041
85. An Android Studio Dynamic Feature Tutorial....1042
85.1 Creating the DynamicFeature Project....1042
85.2 Adding Dynamic Feature Support to the Project....1042
85.3 Designing the Base Activity User Interface....1044
85.4 Adding the Dynamic Feature Module....1045
85.5 Reviewing the Dynamic Feature Module....1046
85.6 Adding the Dynamic Feature Activity....1049
85.7 Implementing the launchIntent() Method....1052
85.8 Uploading the App Bundle for Testing....1054
85.9 Implementing the installFeature() Method....1055
85.10 Adding the Update Listener....1058
85.11 Using Deferred Installation....1062
85.12 Removing a Dynamic Module....1063
85.13 Summary....1063
86. Working with Material Design 3 Theming....1064
86.1 Material Design 2 vs Material Design 3....1064
86.2 Understanding Material Design Theming....1064
86.3 Material Design 3 Theming....1065
86.4 Building a Custom Theme....1068
86.5 Summary....1069
87. A Material Design 3 Theming and Dynamic Color Tutorial....1070
87.1 Creating the ThemeDemo Project....1070
87.2 Designing the User Interface....1070
87.3 Building a New Theme....1072
87.4 Adding the Theme to the Project....1073
87.5 Enabling Dynamic Color Support....1075
87.6 Previewing Dynamic Colors....1077
87.7 Summary....1078
88. An Overview of Gradle in Android Studio....1079
88.1 An Overview of Gradle....1079
88.2 Gradle and Android Studio....1080
88.2.1 Sensible Defaults....1080
88.2.2 Dependencies....1080
88.2.3 Build Variants....1081
88.2.4 Manifest Entries....1081
88.2.5 APK Signing....1081
88.2.6 ProGuard Support....1082
88.3 The Property and Settings Gradle Build File....1082
88.4 The Top-level Gradle Build File....1083
88.5 Module Level Gradle Build Files....1084
88.6 Configuring Signing Settings in the Build File....1087
88.7 Running Gradle Tasks from the Command-line....1089
88.8 Summary....1090
Index....1091
Fully updated for Android Studio Flamingo, this book aims to teach you how to develop Android-based applications using the Java programming language.This book begins with the basics and outlines how to set up an Android development and testing environment, followed by an overview of 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 book edition also covers printing, transitions, and foldable device support.The concepts of material design are also covered in detail, including the use of floating action buttons, Snackbars, tabbed interfaces, card views, navigation drawers, and collapsing toolbars.Other key features of Android Studio and Android are also covered in detail, including the Layout Editor, the ConstraintLayout and ConstraintSet classes, MotionLayout Editor, view binding, constraint chains, barriers, and direct reply notifications.Chapters also cover advanced features of Android Studio, such as App Links, Dynamic Delivery, Gradle build configuration, in-app billing, 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.