Android Studio Giraffe Essentials - Kotlin Edition: Developing Android Apps Using Android Studio 2022.3.1 and Kotlin

Android Studio Giraffe Essentials - Kotlin Edition: Developing Android Apps Using Android Studio 2022.3.1 and Kotlin

Android Studio Giraffe Essentials - Kotlin Edition: Developing Android Apps Using Android Studio 2022.3.1 and Kotlin

Автор: Neil Smyth
Дата выхода: 2023
Издательство: Payload Media, Inc.
Количество страниц: 815
Размер файла: 9,0 МБ
Тип файла: PDF
Добавил: codelibs
 Проверить на вирусы

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 Installing the Android SDK Command-line Tools.......................................................................... 9
2.6.1 Windows 8.1................................................................................................................................ 10
2.6.2 Windows 10................................................................................................................................. 11
2.6.3 Windows 11................................................................................................................................. 11
2.6.4 Linux............................................................................................................................................. 11
2.6.5 macOS........................................................................................................................................... 11
2.7 Android Studio memory management........................................................................................... 11
2.8 Updating Android Studio and the SDK.......................................................................................... 12
2.9 Summary............................................................................................................................................. 13
3. Creating an Example Android App in Android Studio............................................................................ 15
3.1 About the Project............................................................................................................................... 15
3.2 Creating a New Android Project...................................................................................................... 15
3.3 Creating an Activity........................................................................................................................... 16
3.4 Defining the Project and SDK Settings........................................................................................... 16
3.5 Enabling the New Android Studio UI............................................................................................. 17
3.6 Modifying the Example Application................................................................................................ 18
3.7 Modifying the User Interface ........................................................................................................... 19
3.8 Reviewing the Layout and Resource Files....................................................................................... 25
3.9 Adding Interaction............................................................................................................................. 28
3.10 Summary........................................................................................................................................... 29
4. Creating an Android Virtual Device (AVD) in Android Studio.............................................................. 31
4.1 About Android Virtual Devices....................................................................................................... 31
4.2 Starting the Emulator......................................................................................................................... 33
4.3 Running the Application in the AVD.............................................................................................. 34
4.4 Running on Multiple Devices........................................................................................................... 35
4.5 Stopping a Running Application...................................................................................................... 36
4.6 Supporting Dark Theme.................................................................................................................... 36
4.7 Running the Emulator in a Separate Window................................................................................ 37
Contents
4.8 Enabling the Device Frame............................................................................................................... 40
4.9 Summary............................................................................................................................................. 41
5. Using and Configuring the Android Studio AVD Emulator ................................................................... 43
5.1 The Emulator Environment.............................................................................................................. 43
5.2 Emulator Toolbar Options................................................................................................................ 43
5.3 Working in Zoom Mode................................................................................................................... 45
5.4 Resizing the Emulator Window....................................................................................................... 45
5.5 Extended Control Options................................................................................................................ 45
5.5.1 Location........................................................................................................................................ 46
5.5.2 Displays......................................................................................................................................... 46
5.5.3 Cellular......................................................................................................................................... 46
5.5.4 Battery........................................................................................................................................... 46
5.5.5 Camera.......................................................................................................................................... 46
5.5.6 Phone............................................................................................................................................ 46
5.5.7 Directional Pad............................................................................................................................ 46
5.5.8 Microphone.................................................................................................................................. 46
5.5.9 Fingerprint................................................................................................................................... 46
5.5.10 Virtual Sensors.......................................................................................................................... 47
5.5.11 Snapshots.................................................................................................................................... 47
5.5.12 Record and Playback................................................................................................................ 47
5.5.13 Google Play................................................................................................................................ 47
5.5.14 Settings....................................................................................................................................... 47
5.5.15 Help............................................................................................................................................. 47
5.6 Working with Snapshots.................................................................................................................... 47
5.7 Configuring Fingerprint Emulation................................................................................................ 48
5.8 The Emulator in Tool Window Mode.............................................................................................. 50
5.9 Creating a Resizable Emulator.......................................................................................................... 50
5.10 Summary........................................................................................................................................... 51
6. A Tour of the Android Studio User Interface........................................................................................... 53
6.1 The Welcome Screen.......................................................................................................................... 53
6.2 The Menu Bar..................................................................................................................................... 54
6.3 The Main Window............................................................................................................................. 54
6.4 The Tool Windows............................................................................................................................. 56
6.5 The Tool Window Menus.................................................................................................................. 59
6.6 Android Studio Keyboard Shortcuts............................................................................................... 59
6.7 Switcher and Recent Files Navigation............................................................................................. 60
6.8 Changing the Android Studio Theme............................................................................................. 61
6.9 Summary............................................................................................................................................. 62
7. Testing Android Studio Apps on a Physical Android Device................................................................... 63
7.1 An Overview of the Android Debug Bridge (ADB)...................................................................... 63
7.2 Enabling USB Debugging ADB on Android Devices.................................................................... 63
7.2.1 macOS ADB Configuration....................................................................................................... 64
7.2.2 Windows ADB Configuration................................................................................................... 65
7.2.3 Linux adb Configuration............................................................................................................ 66
7.3 Resolving USB Connection Issues................................................................................................... 66
7.4 Enabling Wireless Debugging on Android Devices...................................................................... 67
7.5 Testing the adb Connection.............................................................................................................. 69
7.6 Device Mirroring................................................................................................................................ 69
7.7 Summary............................................................................................................................................. 69
8. The Basics of the Android Studio Code Editor......................................................................................... 71
8.1 The Android Studio Editor............................................................................................................... 71
8.2 Splitting the Editor Window............................................................................................................. 74
8.3 Code Completion............................................................................................................................... 74
8.4 Statement Completion....................................................................................................................... 76
8.5 Parameter Information...................................................................................................................... 76
8.6 Parameter Name Hints...................................................................................................................... 76
8.7 Code Generation................................................................................................................................ 76
8.8 Code Folding....................................................................................................................................... 78
8.9 Quick Documentation Lookup........................................................................................................ 79
8.10 Code Reformatting.......................................................................................................................... 79
8.11 Finding Sample Code...................................................................................................................... 80
8.12 Live Templates.................................................................................................................................. 80
8.13 Summary........................................................................................................................................... 81
9. An Overview of the Android Architecture............................................................................................... 83
9.1 The Android Software Stack............................................................................................................. 83
9.2 The Linux Kernel................................................................................................................................ 84
9.3 Android Runtime – ART................................................................................................................... 84
9.4 Android Libraries............................................................................................................................... 84
9.4.1 C/C++ Libraries.......................................................................................................................... 85
9.5 Application Framework..................................................................................................................... 85
9.6 Applications........................................................................................................................................ 86
9.7 Summary............................................................................................................................................. 86
10. The Anatomy of an Android App............................................................................................................ 87
10.1 Android Activities............................................................................................................................ 87
10.2 Android Fragments.......................................................................................................................... 87
10.3 Android Intents................................................................................................................................ 88
10.4 Broadcast Intents.............................................................................................................................. 88
10.5 Broadcast Receivers......................................................................................................................... 88
10.6 Android Services.............................................................................................................................. 88
10.7 Content Providers............................................................................................................................ 89
10.8 The Application Manifest................................................................................................................ 89
10.9 Application Resources..................................................................................................................... 89
10.10 Application Context....................................................................................................................... 89
10.11 Summary......................................................................................................................................... 89
11. An Introduction to Kotlin....................................................................................................................... 91
11.1 What is Kotlin?................................................................................................................................. 91
11.2 Kotlin and Java.................................................................................................................................. 91
11.3 Converting from Java to Kotlin...................................................................................................... 91
11.4 Kotlin and Android Studio............................................................................................................. 92
11.5 Experimenting with Kotlin............................................................................................................. 92
11.6 Semi-colons in Kotlin...................................................................................................................... 93
11.7 Summary........................................................................................................................................... 93
12. Kotlin Data Types, Variables, and Nullability........................................................................................ 95
12.1 Kotlin Data Types............................................................................................................................. 95
12.1.1 Integer Data Types.................................................................................................................... 96
12.1.2 Floating-Point Data Types...................................................................................................... 96
12.1.3 Boolean Data Type.................................................................................................................... 96
12.1.4 Character Data Type................................................................................................................. 96
12.1.5 String Data Type........................................................................................................................ 96
12.1.6 Escape Sequences...................................................................................................................... 97
12.2 Mutable Variables............................................................................................................................. 98
12.3 Immutable Variables........................................................................................................................ 98
12.4 Declaring Mutable and Immutable Variables............................................................................... 98
12.5 Data Types are Objects.................................................................................................................... 98
12.6 Type Annotations and Type Inference.......................................................................................... 99
12.7 Nullable Type.................................................................................................................................. 100
12.8 The Safe Call Operator.................................................................................................................. 100
12.9 Not-Null Assertion......................................................................................................................... 101
12.10 Nullable Types and the let Function.......................................................................................... 101
12.11 Late Initialization (lateinit)......................................................................................................... 102
12.12 The Elvis Operator....................................................................................................................... 103
12.13 Type Casting and Type Checking.............................................................................................. 103
12.14 Summary....................................................................................................................................... 104
13. Kotlin Operators and Expressions........................................................................................................ 105
13.1 Expression Syntax in Kotlin.......................................................................................................... 105
13.2 The Basic Assignment Operator................................................................................................... 105
13.3 Kotlin Arithmetic Operators........................................................................................................ 105
13.4 Augmented Assignment Operators............................................................................................. 106
13.5 Increment and Decrement Operators......................................................................................... 106
13.6 Equality Operators......................................................................................................................... 107
13.7 Boolean Logical Operators........................................................................................................... 107
13.8 Range Operator.............................................................................................................................. 108
13.9 Bitwise Operators........................................................................................................................... 108
13.9.1 Bitwise Inversion..................................................................................................................... 108
13.9.2 Bitwise AND............................................................................................................................ 109
13.9.3 Bitwise OR................................................................................................................................ 109
13.9.4 Bitwise XOR............................................................................................................................. 109
13.9.5 Bitwise Left Shift...................................................................................................................... 110
13.9.6 Bitwise Right Shift................................................................................................................... 110
13.10 Summary....................................................................................................................................... 111
14. Kotlin Control Flow.............................................................................................................................. 113
14.1 Looping Control flow.................................................................................................................... 113
14.1.1 The Kotlin for-in Statement.................................................................................................... 113
14.1.2 The while Loop ....................................................................................................................... 114
14.1.3 The do ... while loop ............................................................................................................... 115
14.1.4 Breaking from Loops.............................................................................................................. 115
14.1.5 The continue Statement .......................................................................................................... 116
14.1.6 Break and Continue Labels.................................................................................................... 116
14.2 Conditional Control Flow............................................................................................................. 117
14.2.1 Using the if Expressions ........................................................................................................ 117
14.2.2 Using if ... else … Expressions ............................................................................................... 118
14.2.3 Using if ... else if ... Expressions ............................................................................................ 118
14.2.4 Using the when Statement...................................................................................................... 118
14.3 Summary......................................................................................................................................... 119
15. An Overview of Kotlin Functions and Lambdas.................................................................................. 121
15.1 What is a Function?....................................................................................................................... 121
15.2 How to Declare a Kotlin Function............................................................................................... 121
15.3 Calling a Kotlin Function.............................................................................................................. 122
15.4 Single Expression Functions......................................................................................................... 122
15.5 Local Functions.............................................................................................................................. 122
15.6 Handling Return Values................................................................................................................ 123
15.7 Declaring Default Function Parameters...................................................................................... 123
15.8 Variable Number of Function Parameters ................................................................................. 123
15.9 Lambda Expressions...................................................................................................................... 124
15.10 Higher-order Functions.............................................................................................................. 125
15.11 Summary....................................................................................................................................... 126
16. The Basics of Object Oriented Programming in Kotlin....................................................................... 127
16.1 What is an Object?......................................................................................................................... 127
16.2 What is a Class?.............................................................................................................................. 127
16.3 Declaring a Kotlin Class................................................................................................................ 127
16.4 Adding Properties to a Class........................................................................................................ 128
16.5 Defining Methods.......................................................................................................................... 128
16.6 Declaring and Initializing a Class Instance................................................................................. 128
16.7 Primary and Secondary Constructors......................................................................................... 128
16.8 Initializer Blocks............................................................................................................................. 131
16.9 Calling Methods and Accessing Properties................................................................................ 131
16.10 Custom Accessors........................................................................................................................ 131
16.11 Nested and Inner Classes............................................................................................................ 132
16.12 Companion Objects..................................................................................................................... 133
16.13 Summary....................................................................................................................................... 135
17. An Introduction to Kotlin Inheritance and Subclassing...................................................................... 137
17.1 Inheritance, Classes and Subclasses............................................................................................. 137
17.2 Subclassing Syntax......................................................................................................................... 137
17.3 A Kotlin Inheritance Example...................................................................................................... 138
17.4 Extending the Functionality of a Subclass.................................................................................. 139
17.5 Overriding Inherited Methods..................................................................................................... 140
17.6 Adding a Custom Secondary Constructor................................................................................. 141
17.7 Using the SavingsAccount Class.................................................................................................. 141
17.8 Summary......................................................................................................................................... 141
18. An Overview of Android View Binding................................................................................................ 143
18.1 Find View by Id.............................................................................................................................. 143
18.2 View Binding ................................................................................................................................. 143
18.3 Converting the AndroidSample project...................................................................................... 144
18.4 Enabling View Binding.................................................................................................................. 144
18.5 Using View Binding....................................................................................................................... 144
18.6 Choosing an Option...................................................................................................................... 145
18.7 View Binding in the Book Examples........................................................................................... 145
18.8 Migrating a Project to View Binding........................................................................................... 146
18.9 Summary......................................................................................................................................... 146
19. Understanding Android Application and Activity Lifecycles.............................................................. 147
19.1 Android Applications and Resource Management.................................................................... 147
19.2 Android Process States.................................................................................................................. 147
19.2.1 Foreground Process................................................................................................................ 148
19.2.2 Visible Process......................................................................................................................... 148
19.2.3 Service Process........................................................................................................................ 148
19.2.4 Background Process................................................................................................................ 148
19.2.5 Empty Process......................................................................................................................... 149
19.3 Inter-Process Dependencies ........................................................................................................ 149
19.4 The Activity Lifecycle..................................................................................................................... 149
19.5 The Activity Stack........................................................................................................................... 149
19.6 Activity States................................................................................................................................. 150
19.7 Configuration Changes................................................................................................................. 150
19.8 Handling State Change.................................................................................................................. 151
19.9 Summary......................................................................................................................................... 151
20. Handling Android Activity State Changes........................................................................................... 153
20.1 New vs. Old Lifecycle Techniques................................................................................................ 153
20.2 The Activity and Fragment Classes.............................................................................................. 153
20.3 Dynamic State vs. Persistent State................................................................................................ 155
20.4 The Android Lifecycle Methods................................................................................................... 155
20.5 Lifetimes.......................................................................................................................................... 157
20.6 Foldable Devices and Multi-Resume........................................................................................... 158
20.7 Disabling Configuration Change Restarts.................................................................................. 158
20.8 Lifecycle Method Limitations....................................................................................................... 158
20.9 Summary......................................................................................................................................... 159
21. Android Activity State Changes by Example........................................................................................ 161
21.1 Creating the State Change Example Project............................................................................... 161
21.2 Designing the User Interface........................................................................................................ 162
21.3 Overriding the Activity Lifecycle Methods................................................................................ 163
21.4 Filtering the Logcat Panel............................................................................................................. 165
21.5 Running the Application............................................................................................................... 166
21.6 Experimenting with the Activity.................................................................................................. 167
21.7 Summary......................................................................................................................................... 168
22. Saving and Restoring the State of an Android Activity........................................................................ 169
22.1 Saving Dynamic State.................................................................................................................... 169
22.2 Default Saving of User Interface State......................................................................................... 169
22.3 The Bundle Class............................................................................................................................ 170
22.4 Saving the State............................................................................................................................... 171
22.5 Restoring the State......................................................................................................................... 172
22.6 Testing the Application.................................................................................................................. 172
22.7 Summary......................................................................................................................................... 172
23. Understanding Android Views, View Groups and Layouts................................................................. 173
23.1 Designing for Different Android Devices................................................................................... 173
23.2 Views and View Groups................................................................................................................ 173
23.3 Android Layout Managers............................................................................................................ 173
23.4 The View Hierarchy....................................................................................................................... 175
23.5 Creating User Interfaces................................................................................................................ 176
23.6 Summary......................................................................................................................................... 176
24. A Guide to the Android Studio Layout Editor Tool............................................................................. 177
24.1 Basic vs. Empty Views Activity Templates.................................................................................. 177
24.2 The Android Studio Layout Editor.............................................................................................. 181
24.3 Design Mode................................................................................................................................... 181
24.4 The Palette....................................................................................................................................... 182
24.5 Design Mode and Layout Views.................................................................................................. 183
24.6 Night Mode..................................................................................................................................... 184
24.7 Code Mode...................................................................................................................................... 184
24.8 Split Mode....................................................................................................................................... 185
24.9 Setting Attributes........................................................................................................................... 186
24.10 Transforms.................................................................................................................................... 187
24.11 Tools Visibility Toggles................................................................................................................ 188
24.12 Converting Views......................................................................................................................... 189
24.13 Displaying Sample Data.............................................................................................................. 190
24.14 Creating a Custom Device Definition....................................................................................... 191
24.15 Changing the Current Device.................................................................................................... 191
24.16 Layout Validation......................................................................................................................... 192
24.17 Summary....................................................................................................................................... 193
25. A Guide to the Android ConstraintLayout........................................................................................... 195
25.1 How ConstraintLayout Works...................................................................................................... 195
25.1.1 Constraints............................................................................................................................... 195
25.1.2 Margins..................................................................................................................................... 196
25.1.3 Opposing Constraints............................................................................................................ 196
25.1.4 Constraint Bias........................................................................................................................ 197
25.1.5 Chains....................................................................................................................................... 198
25.1.6 Chain Styles.............................................................................................................................. 198
25.2 Baseline Alignment........................................................................................................................ 199
25.3 Configuring Widget Dimensions................................................................................................. 199
25.4 Guideline Helper............................................................................................................................ 200
25.5 Group Helper.................................................................................................................................. 200
25.6 Barrier Helper................................................................................................................................. 200
25.7 Flow Helper..................................................................................................................................... 202
25.8 Ratios............................................................................................................................................... 203
25.9 ConstraintLayout Advantages...................................................................................................... 203
25.10 ConstraintLayout Availability.................................................................................................... 204
25.11 Summary....................................................................................................................................... 204
26. A Guide to Using ConstraintLayout in Android Studio...................................................................... 205
26.1 Design and Layout Views.............................................................................................................. 205
26.2 Autoconnect Mode........................................................................................................................ 207
26.3 Inference Mode............................................................................................................................... 207
26.4 Manipulating Constraints Manually............................................................................................ 207
26.5 Adding Constraints in the Inspector........................................................................................... 209
26.6 Viewing Constraints in the Attributes Window......................................................................... 209
26.7 Deleting Constraints...................................................................................................................... 210
26.8 Adjusting Constraint Bias............................................................................................................. 211
26.9 Understanding ConstraintLayout Margins................................................................................. 211
26.10 The Importance of Opposing Constraints and Bias................................................................ 213
26.11 Configuring Widget Dimensions............................................................................................... 215
26.12 Design Time Tools Positioning.................................................................................................. 216
26.13 Adding Guidelines....................................................................................................................... 217
26.14 Adding Barriers............................................................................................................................ 219
26.15 Adding a Group............................................................................................................................ 220
26.16 Working with the Flow Helper................................................................................................... 221
26.17 Widget Group Alignment and Distribution............................................................................. 221
26.18 Converting other Layouts to ConstraintLayout....................................................................... 223
26.19 Summary ...................................................................................................................................... 223
27. Working with ConstraintLayout Chains and Ratios in Android Studio............................................. 225
27.1 Creating a Chain............................................................................................................................ 225
27.2 Changing the Chain Style............................................................................................................. 227
27.3 Spread Inside Chain Style............................................................................................................. 228
27.4 Packed Chain Style......................................................................................................................... 228
27.5 Packed Chain Style with Bias........................................................................................................ 228
27.6 Weighted Chain.............................................................................................................................. 228
27.7 Working with Ratios...................................................................................................................... 229
27.8 Summary......................................................................................................................................... 231
28. An Android Studio Layout Editor ConstraintLayout Tutorial............................................................ 233
28.1 An Android Studio Layout Editor Tool Example...................................................................... 233
28.2 Preparing the Layout Editor Environment................................................................................. 233
28.3 Adding the Widgets to the User Interface.................................................................................. 234
28.4 Adding the Constraints................................................................................................................. 237
28.5 Testing the Layout.......................................................................................................................... 239
28.6 Using the Layout Inspector........................................................................................................... 239
28.7 Summary......................................................................................................................................... 240
29. Manual XML Layout Design in Android Studio.................................................................................. 241
29.1 Manually Creating an XML Layout............................................................................................. 241
29.2 Manual XML vs. Visual Layout Design....................................................................................... 244
29.3 Summary......................................................................................................................................... 244
30. Managing Constraints using Constraint Sets....................................................................................... 245
30.1 Kotlin Code vs. XML Layout Files............................................................................................... 245
30.2 Creating Views................................................................................................................................ 245
30.3 View Attributes............................................................................................................................... 246
30.4 Constraint Sets................................................................................................................................ 246
30.4.1 Establishing Connections...................................................................................................... 246
30.4.2 Applying Constraints to a Layout......................................................................................... 246
30.4.3 Parent Constraint Connections............................................................................................. 246
30.4.4 Sizing Constraints................................................................................................................... 247
30.4.5 Constraint Bias........................................................................................................................ 247
30.4.6 Alignment Constraints........................................................................................................... 247
30.4.7 Copying and Applying Constraint Sets................................................................................ 247
30.4.8 ConstraintLayout Chains....................................................................................................... 247
30.4.9 Guidelines................................................................................................................................ 248
30.4.10 Removing Constraints.......................................................................................................... 248
30.4.11 Scaling.................................................................................................................................... 248
30.4.12 Rotation.................................................................................................................................. 249
30.5 Summary......................................................................................................................................... 249
31. An Android ConstraintSet Tutorial...................................................................................................... 251
31.1 Creating the Example Project in Android Studio...................................................................... 251
31.2 Adding Views to an Activity......................................................................................................... 251
31.3 Setting View Attributes.................................................................................................................. 252
31.4 Creating View IDs.......................................................................................................................... 253
31.5 Configuring the Constraint Set.................................................................................................... 254
31.6 Adding the EditText View............................................................................................................. 255
31.7 Converting Density Independent Pixels (dp) to Pixels (px)..................................................... 256
31.8 Summary......................................................................................................................................... 257
32. A Guide to Using Apply Changes in Android Studio........................................................................... 259
32.1 Introducing Apply Changes.......................................................................................................... 259
32.2 Understanding Apply Changes Options..................................................................................... 259
32.3 Using Apply Changes..................................................................................................................... 260
32.4 Configuring Apply Changes Fallback Settings........................................................................... 261
32.5 An Apply Changes Tutorial.......................................................................................................... 261
32.6 Using Apply Code Changes.......................................................................................................... 261
32.7 Using Apply Changes and Restart Activity................................................................................. 262
32.8 Using Run App............................................................................................................................... 262
32.9 Summary......................................................................................................................................... 262
33. An Overview and Example of Android Event Handling...................................................................... 263
33.1 Understanding Android Events................................................................................................... 263
33.2 Using the android:onClick Resource........................................................................................... 263
33.3 Event Listeners and Callback Methods....................................................................................... 264
33.4 An Event Handling Example........................................................................................................ 264
33.5 Designing the User Interface........................................................................................................ 265
33.6 The Event Listener and Callback Method................................................................................... 265
33.7 Consuming Events......................................................................................................................... 267
33.8 Summary......................................................................................................................................... 268
34. Android Touch and Multi-touch Event Handling................................................................................ 269
34.1 Intercepting Touch Events............................................................................................................ 269
34.2 The MotionEvent Object............................................................................................................... 270
34.3 Understanding Touch Actions...................................................................................................... 270
34.4 Handling Multiple Touches.......................................................................................................... 270
34.5 An Example Multi-Touch Application........................................................................................ 271
34.6 Designing the Activity User Interface......................................................................................... 271
34.7 Implementing the Touch Event Listener..................................................................................... 271
34.8 Running the Example Application............................................................................................... 274
34.9 Summary......................................................................................................................................... 274
35. Detecting Common Gestures Using the Android Gesture Detector Class.......................................... 275
35.1 Implementing Common Gesture Detection............................................................................... 275
35.2 Creating an Example Gesture Detection Project....................................................................... 276
35.3 Implementing the Listener Class.................................................................................................. 276
35.4 Creating the GestureDetectorCompat Instance......................................................................... 278
35.5 Implementing the onTouchEvent() Method............................................................................... 278
35.6 Testing the Application.................................................................................................................. 279
35.7 Summary......................................................................................................................................... 279
36. Implementing Custom Gesture and Pinch Recognition on Android.................................................. 281
36.1 The Android Gesture Builder Application.................................................................................. 281
36.2 The GestureOverlayView Class.................................................................................................... 281
36.3 Detecting Gestures......................................................................................................................... 281
36.4 Identifying Specific Gestures........................................................................................................ 281
36.5 Installing and Running the Gesture Builder Application......................................................... 281
36.6 Creating a Gestures File................................................................................................................ 282
36.7 Creating the Example Project....................................................................................................... 282
36.8 Extracting the Gestures File from the SD Card......................................................................... 282
36.9 Adding the Gestures File to the Project...................................................................................... 283
36.10 Designing the User Interface...................................................................................................... 283
36.11 Loading the Gestures File........................................................................................................... 284
36.12 Registering the Event Listener.................................................................................................... 285
36.13 Implementing the onGesturePerformed Method.................................................................... 285
36.14 Testing the Application............................................................................................................... 286
36.15 Configuring the GestureOverlayView....................................................................................... 286
36.16 Intercepting Gestures.................................................................................................................. 287
36.17 Detecting Pinch Gestures............................................................................................................ 287
36.18 A Pinch Gesture Example Project.............................................................................................. 287
36.19 Summary....................................................................................................................................... 289
37. An Introduction to Android Fragments............................................................................................... 291
37.1 What is a Fragment?...................................................................................................................... 291
37.2 Creating a Fragment...................................................................................................................... 291
37.3 Adding a Fragment to an Activity using the Layout XML File................................................ 292
37.4 Adding and Managing Fragments in Code................................................................................ 294
37.5 Handling Fragment Events........................................................................................................... 295
37.6 Implementing Fragment Communication................................................................................. 295
37.7 Summary ........................................................................................................................................ 297
38. Using Fragments in Android Studio - An Example.............................................................................. 299
38.1 About the Example Fragment Application................................................................................. 299
38.2 Creating the Example Project....................................................................................................... 299
38.3 Creating the First Fragment Layout............................................................................................. 299
38.4 Migrating a Fragment to View Binding...................................................................................... 301
38.5 Adding the Second Fragment....................................................................................................... 302
38.6 Adding the Fragments to the Activity......................................................................................... 303
38.7 Making the Toolbar Fragment Talk to the Activity................................................................... 304
38.8 Making the Activity Talk to the Text Fragment......................................................................... 307
38.9 Testing the Application.................................................................................................................. 308
38.10 Summary....................................................................................................................................... 308
39. Modern Android App Architecture with Jetpack................................................................................. 309
39.1 What is Android Jetpack?............................................................................................................. 309
39.2 The “Old” Architecture.................................................................................................................. 309
39.3 Modern Android Architecture..................................................................................................... 309
39.4 The ViewModel Component........................................................................................................ 310
39.5 The LiveData Component............................................................................................................. 310
39.6 ViewModel Saved State................................................................................................................. 311
39.7 LiveData and Data Binding........................................................................................................... 311
39.8 Android Lifecycles......................................................................................................................... 312
39.9 Repository Modules....................................................................................................................... 312
39.10 Summary....................................................................................................................................... 313
40. An Android ViewModel Tutorial.......................................................................................................... 315
40.1 About the Project........................................................................................................................... 315
40.2 Creating the ViewModel Example Project.................................................................................. 315
40.3 Removing Unwanted Project Elements....................................................................................... 315
40.4 Designing the Fragment Layout................................................................................................... 316
40.5 Implementing the View Model..................................................................................................... 317
40.6 Associating the Fragment with the View Model........................................................................ 318
40.7 Modifying the Fragment............................................................................................................... 319
40.8 Accessing the ViewModel Data.................................................................................................... 319
40.9 Testing the Project.......................................................................................................................... 320
40.10 Summary....................................................................................................................................... 320
41. An Android Jetpack LiveData Tutorial................................................................................................. 321
41.1 LiveData - A Recap........................................................................................................................ 321
41.2 Adding LiveData to the ViewModel............................................................................................ 321
41.3 Implementing the Observer.......................................................................................................... 323
41.4 Summary......................................................................................................................................... 324
42. An Overview of Android Jetpack Data Binding................................................................................... 325
42.1 An Overview of Data Binding...................................................................................................... 325
42.2 The Key Components of Data Binding....................................................................................... 325
42.2.1 The Project Build Configuration........................................................................................... 325
42.2.2 The Data Binding Layout File................................................................................................ 326
42.2.3 The Layout File Data Element............................................................................................... 327
42.2.4 The Binding Classes................................................................................................................ 328
42.2.5 Data Binding Variable Configuration................................................................................... 328
42.2.6 Binding Expressions (One-Way)........................................................................................... 329
42.2.7 Binding Expressions (Two-Way)........................................................................................... 330
42.2.8 Event and Listener Bindings.................................................................................................. 330
42.3 Summary......................................................................................................................................... 331
43. An Android Jetpack Data Binding Tutorial.......................................................................................... 333
43.1 Removing the Redundant Code................................................................................................... 333
43.2 Enabling Data Binding.................................................................................................................. 334
43.3 Adding the Layout Element.......................................................................................................... 335
43.4 Adding the Data Element to Layout File..................................................................................... 336
43.5 Working with the Binding Class.................................................................................................. 336
43.6 Assigning the ViewModel Instance to the Data Binding Variable.......................................... 337
43.7 Adding Binding Expressions........................................................................................................ 338
43.8 Adding the Conversion Method.................................................................................................. 339
43.9 Adding a Listener Binding............................................................................................................ 339
43.10 Testing the App............................................................................................................................. 340
43.11 Summary....................................................................................................................................... 340
44. An Android ViewModel Saved State Tutorial....................................................................................... 341
44.1 Understanding ViewModel State Saving..................................................................................... 341
44.2 Implementing ViewModel State Saving...................................................................................... 341
44.3 Saving and Restoring State............................................................................................................ 342
44.4 Adding Saved State Support to the ViewModelDemo Project................................................. 343
44.5 Summary......................................................................................................................................... 344
45. Working with Android Lifecycle-Aware Components......................................................................... 345
45.1 Lifecycle Awareness....................................................................................................................... 345
45.2 Lifecycle Owners............................................................................................................................ 345
45.3 Lifecycle Observers........................................................................................................................ 346
45.4 Lifecycle States and Events............................................................................................................ 346
45.5 Summary......................................................................................................................................... 347
46. An Android Jetpack Lifecycle Awareness Tutorial............................................................................... 349
46.1 Creating the Example Lifecycle Project....................................................................................... 349
46.2 Creating a Lifecycle Observer....................................................................................................... 349
46.3 Adding the Observer..................................................................................................................... 350
46.4 Testing the Observer...................................................................................................................... 351
46.5 Creating a Lifecycle Owner........................................................................................................... 351
46.6 Testing the Custom Lifecycle Owner........................................................................................... 353
46.7 Summary......................................................................................................................................... 353
47. An Overview of the Navigation Architecture Component................................................................... 355
47.1 Understanding Navigation............................................................................................................ 355
47.2 Declaring a Navigation Host......................................................................................................... 356
47.3 The Navigation Graph................................................................................................................... 358
47.4 Accessing the Navigation Controller........................................................................................... 359
47.5 Triggering a Navigation Action.................................................................................................... 359
47.6 Passing Arguments......................................................................................................................... 360
47.7 Summary......................................................................................................................................... 360
48. An Android Jetpack Navigation Component Tutorial......................................................................... 361
48.1 Creating the NavigationDemo Project........................................................................................ 361
48.2 Adding Navigation to the Build Configuration......................................................................... 361
48.3 Creating the Navigation Graph Resource File............................................................................ 362
48.4 Declaring a Navigation Host......................................................................................................... 363
48.5 Adding Navigation Destinations.................................................................................................. 364
48.6 Designing the Destination Fragment Layouts............................................................................ 366
48.7 Adding an Action to the Navigation Graph............................................................................... 367
48.8 Implement the OnFragmentInteractionListener....................................................................... 369
48.9 Adding View Binding Support to the Destination Fragments................................................. 370
48.10 Triggering the Action.................................................................................................................. 370
48.11 Passing Data Using Safeargs....................................................................................................... 371
48.12 Summary....................................................................................................................................... 374
49. An Introduction to MotionLayout........................................................................................................ 375
49.1 An Overview of MotionLayout.................................................................................................... 375
49.2 MotionLayout................................................................................................................................. 375
49.3 MotionScene................................................................................................................................... 375
49.4 Configuring ConstraintSets.......................................................................................................... 376
49.5 Custom Attributes.......................................................................................................................... 377
49.6 Triggering an Animation............................................................................................................... 379
49.7 Arc Motion...................................................................................................................................... 380
49.8 Keyframes........................................................................................................................................ 380
49.8.1 Attribute Keyframes................................................................................................................ 380
49.8.2 Position Keyframes................................................................................................................. 381
49.9 Time Linearity................................................................................................................................ 384
49.10 KeyTrigger..................................................................................................................................... 384
49.11 Cycle and Time Cycle Keyframes.............................................................................................. 385
49.12 Starting an Animation from Code............................................................................................. 385
49.13 Summary....................................................................................................................................... 386
50. An Android MotionLayout Editor Tutorial.......................................................................................... 387
50.1 Creating the MotionLayoutDemo Project.................................................................................. 387
50.2 ConstraintLayout to MotionLayout Conversion....................................................................... 387
50.3 Configuring Start and End Constraints...................................................................................... 389
50.4 Previewing the MotionLayout Animation.................................................................................. 392
50.5 Adding an OnClick Gesture......................................................................................................... 392
50.6 Adding an Attribute Keyframe to the Transition....................................................................... 394
50.7 Adding a CustomAttribute to a Transition................................................................................. 396
50.8 Adding Position Keyframes.......................................................................................................... 398
50.9 Summary......................................................................................................................................... 400
51. A MotionLayout KeyCycle Tutorial...................................................................................................... 401
51.1 An Overview of Cycle Keyframes................................................................................................ 401
51.2 Using the Cycle Editor................................................................................................................... 405
51.3 Creating the KeyCycleDemo Project........................................................................................... 406
51.4 Configuring the Start and End Constraints................................................................................ 406
51.5 Creating the Cycles........................................................................................................................ 408
51.6 Previewing the Animation............................................................................................................ 410
51.7 Adding the KeyFrameSet to the MotionScene........................................................................... 410
51.8 Summary......................................................................................................................................... 412
52. Working with the Floating Action Button and Snackbar..................................................................... 413
52.1 The Material Design....................................................................................................................... 413
52.2 The Design Library........................................................................................................................ 413
52.3 The Floating Action Button (FAB) .............................................................................................. 413
52.4 The Snackbar................................................................................................................................... 414
52.5 Creating the Example Project....................................................................................................... 415
52.6 Reviewing the Project.................................................................................................................... 415
52.7 Removing Navigation Features.................................................................................................... 416
52.8 Changing the Floating Action Button......................................................................................... 416
52.9 Adding an Action to the Snackbar............................................................................................... 418
52.10 Summary....................................................................................................................................... 418
53. Creating a Tabbed Interface using the TabLayout Component........................................................... 419
53.1 An Introduction to the ViewPager2............................................................................................ 419
53.2 An Overview of the TabLayout Component.............................................................................. 419
53.3 Creating the TabLayoutDemo Project......................................................................................... 420
53.4 Creating the First Fragment.......................................................................................................... 420
53.5 Duplicating the Fragments........................................................................................................... 422
53.6 Adding the TabLayout and ViewPager2...................................................................................... 423
53.7 Performing the Initialization Tasks.............................................................................................. 425
53.8 Testing the Application.................................................................................................................. 427
53.9 Customizing the TabLayout.......................................................................................................... 427
53.10 Summary....................................................................................................................................... 429
54. Working with the RecyclerView and CardView Widgets..................................................................... 431
54.1 An Overview of the RecyclerView............................................................................................... 431
54.2 An Overview of the CardView..................................................................................................... 433
54.3 Summary......................................................................................................................................... 434
55. An Android RecyclerView and CardView Tutorial.............................................................................. 435
55.1 Creating the CardDemo Project.................................................................................................. 435
55.2 Modifying the Basic Views Activity Project............................................................................... 435
55.3 Designing the CardView Layout.................................................................................................. 436
55.4 Adding the RecyclerView.............................................................................................................. 437
55.5 Adding the Image Files.................................................................................................................. 437
55.6 Creating the RecyclerView Adapter............................................................................................. 437
55.7 Initializing the RecyclerView Component.................................................................................. 439
55.8 Testing the Application.................................................................................................................. 440
55.9 Responding to Card Selections.................................................................................................... 441
55.10 Summary....................................................................................................................................... 443
56. Working with the AppBar and Collapsing Toolbar Layouts................................................................ 445
56.1 The Anatomy of an AppBar.......................................................................................................... 445
56.2 The Example Project...................................................................................................................... 446
56.3 Coordinating the RecyclerView and Toolbar............................................................................. 446
56.4 Introducing the Collapsing Toolbar Layout............................................................................... 448
56.5 Changing the Title and Scrim Color........................................................................................... 451
56.6 Summary......................................................................................................................................... 452
57. An Overview of Android Intents.......................................................................................................... 453
57.1 An Overview of Intents................................................................................................................. 453
57.2 Explicit Intents................................................................................................................................ 453
57.3 Returning Data from an Activity................................................................................................. 454
57.4 Implicit Intents............................................................................................................................... 455
57.5 Using Intent Filters......................................................................................................................... 456
57.6 Automatic Link Verification......................................................................................................... 456
57.7 Manually Enabling Links.............................................................................................................. 459
57.8 Checking Intent Availability......................................................................................................... 460
57.9 Summary......................................................................................................................................... 461
58. Android Explicit Intents – A Worked Example.................................................................................... 463
58.1 Creating the Explicit Intent Example Application..................................................................... 463
58.2 Designing the User Interface Layout for MainActivity............................................................. 463
58.3 Creating the Second Activity Class.............................................................................................. 464
58.4 Designing the User Interface Layout for SecondActivity......................................................... 465
58.5 Reviewing the Application Manifest File.................................................................................... 465
58.6 Creating the Intent......................................................................................................................... 466
58.7 Extracting Intent Data................................................................................................................... 467
58.8 Launching SecondActivity as a Sub-Activity.............................................................................. 468
58.9 Returning Data from a Sub-Activity........................................................................................... 469
58.10 Testing the Application............................................................................................................... 469
58.11 Summary....................................................................................................................................... 469
59. Android Implicit Intents – A Worked Example................................................................................... 471
59.1 Creating the Android Studio Implicit Intent Example Project................................................ 471
59.2 Designing the User Interface........................................................................................................ 471
59.3 Creating the Implicit Intent.......................................................................................................... 472
59.4 Adding a Second Matching Activity............................................................................................ 473
59.5 Adding the Web View to the UI................................................................................................... 473
59.6 Obtaining the Intent URL............................................................................................................. 474
59.7 Modifying the MyWebView Project Manifest File.................................................................... 475
59.8 Installing the MyWebView Package on a Device....................................................................... 476
59.9 Testing the Application.................................................................................................................. 477
59.10 Manually Enabling the Link....................................................................................................... 477
59.11 Automatic Link Verification....................................................................................................... 479
59.12 Summary....................................................................................................................................... 481

 Fully updated for Android Studio Giraffe and the new UI, this book teaches you how to develop Android-based applications using the Kotlin programming language.

 This book begins with the basics and outlines how to set up an Android development and testing environment, followed by an introduction to programming in Kotlin, including data types, control flow, functions, lambdas, and object-oriented programming. Asynchronous programming using Kotlin coroutines and flow is also covered in detail.

 Chapters also cover the Android Architecture Components, including view models, lifecycle management, Room database access, the Database Inspector, app navigation, live data, and data binding.

 More advanced topics such as intents are also covered, as are touch screen handling, gesture recognition, and the recording and playback of audio. This book edition also covers printing, transitions, and foldable device support.

 The concepts of material design are also covered in detail, including the use of floating action buttons, Snackbars, tabbed interfaces, card views, navigation drawers, and collapsing toolbars.

 Other key features of Android Studio and Android are also covered in detail, including the Layout Editor, the ConstraintLayout and ConstraintSet classes, MotionLayout Editor, view binding, constraint chains, barriers, and direct reply notifications.

 Chapters also cover advanced features of Android Studio, such as App Links, Gradle build configuration, in-app billing, and submitting apps to the Google Play Developer Console.

 Assuming you already have some programming experience, are ready to download Android Studio and the Android SDK, have access to a Windows, Mac, or Linux system, and have ideas for some apps to develop, you are ready to get started.


Похожее:

Список отзывов:

Нет отзывов к книге.