Jetpack Compose 1.3 Essentials: Developing Android Apps with Jetpack Compose 1.3, Android Studio, and Kotlin

Jetpack Compose 1.3 Essentials: Developing Android Apps with Jetpack Compose 1.3, Android Studio, and Kotlin

Jetpack Compose 1.3 Essentials: Developing Android Apps with Jetpack Compose 1.3, Android Studio, and Kotlin

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

1. Start Here..................................................................................................................................................... 1
1.1 For Kotlin programmers..................................................................................................................... 1
1.2 For new Kotlin programmers............................................................................................................. 1
1.3 Downloading the code samples.......................................................................................................... 1
1.4 Feedback................................................................................................................................................ 2
1.5 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................................................................................................................................. 10
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............................................................................................................................................. 12
3. A Compose Project Overview................................................................................................................... 13
3.1 About the project................................................................................................................................ 13
3.2 Creating the project........................................................................................................................... 14
3.3 Creating an activity............................................................................................................................ 14
3.4 Defining the project and SDK settings............................................................................................ 15
3.5 Previewing the example project....................................................................................................... 16
3.6 Reviewing the main activity.............................................................................................................. 18
3.7 Preview updates.................................................................................................................................. 22
3.8 Bill of Materials and the Compose version..................................................................................... 22
3.9 Summary............................................................................................................................................. 24
4. An Example Compose Project.................................................................................................................. 25
4.1 Getting started.................................................................................................................................... 25
4.2 Removing the template Code........................................................................................................... 25
4.3 The Composable hierarchy............................................................................................................... 26
4.4 Adding the DemoText composable................................................................................................. 26
4.5 Previewing the DemoText composable........................................................................................... 28
4.6 Adding the DemoSlider composable............................................................................................... 28
4.7 Adding the DemoScreen composable............................................................................................. 29
4.8 Previewing the DemoScreen composable....................................................................................... 31
4.9 Adjusting preview settings................................................................................................................ 31
4.10 Testing in interactive mode............................................................................................................. 32
4.11 Completing the project.................................................................................................................... 33
4.12 Summary........................................................................................................................................... 34
5. Creating an Android Virtual Device (AVD) in Android Studio.............................................................. 35
5.1 About Android Virtual Devices....................................................................................................... 35
5.2 Starting the emulator......................................................................................................................... 36
5.3 Running the application in the AVD............................................................................................... 37
5.4 Real-time updates with Live Edit..................................................................................................... 39
5.5 Running on multiple devices............................................................................................................ 40
5.6 Stopping a running application........................................................................................................ 40
5.7 Supporting dark theme...................................................................................................................... 41
5.8 Running the emulator in a separate window.................................................................................. 41
5.9 Enabling the device frame................................................................................................................. 43
5.10 Summary........................................................................................................................................... 43
6. Using and Configuring the Android Studio AVD Emulator ................................................................... 45
6.1 The Emulator Environment.............................................................................................................. 45
6.2 Emulator Toolbar Options................................................................................................................ 45
6.3 Working in Zoom Mode................................................................................................................... 47
6.4 Resizing the Emulator Window....................................................................................................... 47
6.5 Extended Control Options................................................................................................................ 47
6.5.1 Location........................................................................................................................................ 48
6.5.2 Displays......................................................................................................................................... 48
6.5.3 Cellular......................................................................................................................................... 48
6.5.4 Battery........................................................................................................................................... 48
6.5.5 Camera.......................................................................................................................................... 48
6.5.6 Phone............................................................................................................................................ 48
6.5.7 Directional Pad............................................................................................................................ 48
6.5.8 Microphone.................................................................................................................................. 48
6.5.9 Fingerprint................................................................................................................................... 48
6.5.10 Virtual Sensors.......................................................................................................................... 49
6.5.11 Snapshots.................................................................................................................................... 49
6.5.12 Record and Playback................................................................................................................ 49
6.5.13 Google Play................................................................................................................................ 49
6.5.14 Settings....................................................................................................................................... 49
6.5.15 Help............................................................................................................................................. 49
6.6 Working with Snapshots.................................................................................................................... 49
6.7 Configuring Fingerprint Emulation................................................................................................ 50
6.8 The Emulator in Tool Window Mode.............................................................................................. 51
6.9 Creating a Resizable Emulator.......................................................................................................... 52
6.10 Summary........................................................................................................................................... 53
7. A Tour of the Android Studio User Interface........................................................................................... 55
7.1 The Welcome Screen.......................................................................................................................... 55
7.2 The Main Window............................................................................................................................. 56
7.3 The Tool Windows............................................................................................................................. 57
7.4 Android Studio Keyboard Shortcuts............................................................................................... 60
7.5 Switcher and Recent Files Navigation............................................................................................. 61
7.6 Changing the Android Studio Theme............................................................................................. 62
7.7 Summary............................................................................................................................................. 63
8. Testing Android Studio Apps on a Physical Android Device................................................................... 65
8.1 An overview of the Android Debug Bridge (ADB)....................................................................... 65
8.2 Enabling USB debugging ADB on Android devices..................................................................... 65
8.2.1 macOS ADB configuration........................................................................................................ 66
8.2.2 Windows ADB configuration.................................................................................................... 67
8.2.3 Linux adb configuration............................................................................................................. 68
8.3 Resolving USB connection issues..................................................................................................... 68
8.4 Enabling wireless debugging on Android devices......................................................................... 69
8.5 Testing the adb connection............................................................................................................... 71
8.6 Summary............................................................................................................................................. 71
9. The Basics of the Android Studio Code Editor......................................................................................... 73
9.1 The Android Studio editor................................................................................................................ 73
9.2 Code mode.......................................................................................................................................... 75
9.3 Splitting the editor window............................................................................................................... 76
9.4 Code completion................................................................................................................................ 76
9.5 Statement completion........................................................................................................................ 78
9.6 Parameter information...................................................................................................................... 78
9.7 Parameter name hints........................................................................................................................ 78
9.8 Code generation................................................................................................................................. 78
9.9 Code folding....................................................................................................................................... 79
9.10 Quick documentation lookup........................................................................................................ 81
9.11 Code reformatting............................................................................................................................ 81
9.12 Finding sample code........................................................................................................................ 82
9.13 Live templates................................................................................................................................... 82
9.14 Summary........................................................................................................................................... 83
10. An Overview of the Android Architecture............................................................................................. 85
10.1 The Android software stack............................................................................................................ 85
10.2 The Linux kernel.............................................................................................................................. 86
10.3 Android runtime – ART................................................................................................................. 86
10.4 Android libraries.............................................................................................................................. 86
10.4.1 C/C++ libraries.......................................................................................................................... 86
10.5 Application framework.................................................................................................................... 87
10.6 Applications...................................................................................................................................... 87
10.7 Summary........................................................................................................................................... 87
11. An Introduction to Kotlin....................................................................................................................... 89
11.1 What is Kotlin?................................................................................................................................. 89
11.2 Kotlin and Java.................................................................................................................................. 89
11.3 Converting from Java to Kotlin...................................................................................................... 89
11.4 Kotlin and Android Studio............................................................................................................. 90
11.5 Experimenting with Kotlin............................................................................................................. 90
11.6 Semi-colons in Kotlin...................................................................................................................... 91
11.7 Summary........................................................................................................................................... 91
12. Kotlin Data Types, Variables and Nullability......................................................................................... 93
12.1 Kotlin data types............................................................................................................................... 93
12.1.1 Integer data types...................................................................................................................... 94
12.1.2 Floating point data types......................................................................................................... 94
12.1.3 Boolean data type...................................................................................................................... 94
12.1.4 Character data type................................................................................................................... 94
12.1.5 String data type.......................................................................................................................... 94
12.1.6 Escape sequences....................................................................................................................... 95
12.2 Mutable variables............................................................................................................................. 96
12.3 Immutable variables......................................................................................................................... 96
12.4 Declaring mutable and immutable variables................................................................................ 96
12.5 Data types are objects...................................................................................................................... 96
12.6 Type annotations and type inference............................................................................................. 97
12.7 Nullable type..................................................................................................................................... 98
12.8 The safe call operator....................................................................................................................... 98
12.9 Not-null assertion............................................................................................................................ 99
12.10 Nullable types and the let function.............................................................................................. 99
12.11 Late initialization (lateinit)......................................................................................................... 100
12.12 The Elvis operator........................................................................................................................ 101
12.13 Type casting and type checking................................................................................................. 101
12.14 Summary....................................................................................................................................... 102
13. Kotlin Operators and Expressions........................................................................................................ 103
13.1 Expression syntax in Kotlin.......................................................................................................... 103
13.2 The Basic assignment operator..................................................................................................... 103
13.3 Kotlin arithmetic operators.......................................................................................................... 103
13.4 Augmented assignment operators............................................................................................... 104
13.5 Increment and decrement operators........................................................................................... 104
13.6 Equality operators.......................................................................................................................... 105
13.7 Boolean logical operators.............................................................................................................. 105
13.8 Range operator............................................................................................................................... 106
13.9 Bitwise operators............................................................................................................................ 106
13.9.1 Bitwise inversion..................................................................................................................... 106
13.9.2 Bitwise AND............................................................................................................................ 107
13.9.3 Bitwise OR................................................................................................................................ 107
13.9.4 Bitwise XOR............................................................................................................................. 107
13.9.5 Bitwise left shift....................................................................................................................... 108
13.9.6 Bitwise right shift.................................................................................................................... 108
13.10 Summary....................................................................................................................................... 109
14. Kotlin Control Flow.............................................................................................................................. 111
14.1 Looping control flow..................................................................................................................... 111
14.1.1 The Kotlin for-in Statement.................................................................................................... 111
14.1.2 The while loop ......................................................................................................................... 112
14.1.3 The do ... while loop ............................................................................................................... 113
14.1.4 Breaking from Loops.............................................................................................................. 113
14.1.5 The continue statement .......................................................................................................... 114
14.1.6 Break and continue labels...................................................................................................... 114
14.2 Conditional control flow............................................................................................................... 115
14.2.1 Using the if expressions ......................................................................................................... 115
14.2.2 Using if ... else … expressions ............................................................................................... 116
14.2.3 Using if ... else if ... Expressions ............................................................................................ 116
14.2.4 Using the when statement...................................................................................................... 116
14.3 Summary......................................................................................................................................... 117
15. An Overview of Kotlin Functions and Lambdas.................................................................................. 119
15.1 What is a function?........................................................................................................................ 119
15.2 How to declare a Kotlin function................................................................................................. 119
15.3 Calling a Kotlin function............................................................................................................... 120
15.4 Single expression functions.......................................................................................................... 120
15.5 Local functions............................................................................................................................... 120
15.6 Handling return values.................................................................................................................. 121
15.7 Declaring default function parameters....................................................................................... 121
15.8 Variable number of function parameters ................................................................................... 121
15.9 Lambda expressions....................................................................................................................... 122
15.10 Higher-order functions............................................................................................................... 123
15.11 Summary....................................................................................................................................... 124
16. The Basics of Object-Oriented Programming in Kotlin....................................................................... 125
16.1 What is an object?.......................................................................................................................... 125
16.2 What is a class?............................................................................................................................... 125
16.3 Declaring a Kotlin class................................................................................................................. 125
16.4 Adding properties to a class.......................................................................................................... 126
16.5 Defining methods........................................................................................................................... 126
16.6 Declaring and initializing a class instance.................................................................................. 126
16.7 Primary and secondary constructors.......................................................................................... 126
16.8 Initializer blocks............................................................................................................................. 129
16.9 Calling methods and accessing properties.................................................................................. 129
16.10 Custom accessors......................................................................................................................... 129
16.11 Nested and inner classes............................................................................................................. 130
16.12 Companion objects...................................................................................................................... 131
16.13 Summary....................................................................................................................................... 133
17. An Introduction to Kotlin Inheritance and Subclassing...................................................................... 135
17.1 Inheritance, classes, and subclasses............................................................................................. 135
17.2 Subclassing syntax.......................................................................................................................... 135
17.3 A Kotlin inheritance example....................................................................................................... 136
17.4 Extending the functionality of a subclass.................................................................................... 137
17.5 Overriding inherited methods..................................................................................................... 138
17.6 Adding a custom secondary constructor.................................................................................... 139
17.7 Using the SavingsAccount class.................................................................................................... 139
17.8 Summary......................................................................................................................................... 139
18. An Overview of Compose..................................................................................................................... 141
18.1 Development before Compose..................................................................................................... 141
18.2 Compose declarative syntax........................................................................................................ 141
18.3 Compose is data-driven................................................................................................................ 142
18.4 Summary......................................................................................................................................... 142
19. Composable Functions Overview......................................................................................................... 143
19.1 What is a composable function?.................................................................................................. 143
19.2 Stateful vs. stateless composables................................................................................................. 143
19.3 Composable function syntax........................................................................................................ 144
19.4 Foundation and Material composables....................................................................................... 146
19.5 Summary......................................................................................................................................... 147
20. An Overview of Compose State and Recomposition............................................................................ 149
20.1 The basics of state........................................................................................................................... 149
20.2 Introducing recomposition........................................................................................................... 149
20.3 Creating the StateExample project............................................................................................... 150
20.4 Declaring state in a composable................................................................................................... 150
20.5 Unidirectional data flow................................................................................................................ 153
20.6 State hoisting................................................................................................................................... 155
20.7 Saving state through configuration changes............................................................................... 157
20.8 Summary......................................................................................................................................... 158
21. An Introduction to Composition Local................................................................................................ 161
21.1 Understanding CompositionLocal.............................................................................................. 161
21.2 Using CompositionLocal.............................................................................................................. 162
21.3 Creating the CompLocalDemo project....................................................................................... 163
21.4 Designing the layout...................................................................................................................... 163
21.5 Adding the CompositionLocal state............................................................................................ 164
21.6 Accessing the CompositionLocal state........................................................................................ 165
21.7 Testing the design........................................................................................................................... 165
21.8 Summary......................................................................................................................................... 168
22. An Overview of Compose Slot APIs..................................................................................................... 169
22.1 Understanding slot APIs............................................................................................................... 169
22.2 Declaring a slot API....................................................................................................................... 170
22.3 Calling slot API composables....................................................................................................... 170
22.4 Summary......................................................................................................................................... 172
23. A Compose Slot API Tutorial................................................................................................................ 173
23.1 About the project............................................................................................................................ 173
23.2 Creating the SlotApiDemo project.............................................................................................. 173
23.3 Preparing the MainActivity class file........................................................................................... 173
23.4 Creating the MainScreen composable......................................................................................... 174
23.5 Adding the ScreenContent composable ..................................................................................... 175
23.6 Creating the Checkbox composable............................................................................................ 176
23.7 Implementing the ScreenContent slot API................................................................................. 177
23.8 Adding an Image drawable resource........................................................................................... 178
23.9 Writing the TitleImage composable............................................................................................ 179
23.10 Completing the MainScreen composable................................................................................. 180
23.11 Previewing the project................................................................................................................. 182
23.12 Summary....................................................................................................................................... 183
24. Using Modifiers in Compose................................................................................................................. 185
24.1 An overview of modifiers.............................................................................................................. 185
24.2 Creating the ModifierDemo project............................................................................................ 185
24.3 Creating a modifier........................................................................................................................ 186
24.4 Modifier ordering........................................................................................................................... 188
24.5 Adding modifier support to a composable................................................................................. 188
24.6 Common built-in modifiers......................................................................................................... 192
24.7 Combining modifiers.................................................................................................................... 192
24.8 Summary......................................................................................................................................... 193
25. Annotated Strings and Brush Styles..................................................................................................... 195
25.1 What are annotated strings?......................................................................................................... 195
25.2 Using annotated strings................................................................................................................. 195
25.3 Brush Text Styling.......................................................................................................................... 196
25.4 Creating the example project........................................................................................................ 197
25.5 An example SpanStyle annotated string...................................................................................... 197
25.6 An example ParagraphStyle annotated string............................................................................ 198
25.7 A Brush style example................................................................................................................... 201
25.8 Summary......................................................................................................................................... 202
26. Composing Layouts with Row and Column......................................................................................... 203
26.1 Creating the RowColDemo project............................................................................................. 203
26.2 Row composable............................................................................................................................. 204
26.3 Column composable...................................................................................................................... 204
26.4 Combining Row and Column composables............................................................................... 205
26.5 Layout alignment........................................................................................................................... 206
26.6 Layout arrangement positioning.................................................................................................. 208
26.7 Layout arrangement spacing......................................................................................................... 210
26.8 Row and Column scope modifiers............................................................................................... 211
26.9 Scope modifier weights................................................................................................................. 215
26.10 Summary....................................................................................................................................... 216
27. Box Layouts in Compose....................................................................................................................... 217
27.1 An introduction to the Box composable..................................................................................... 217
27.2 Creating the BoxLayout project................................................................................................... 217
27.3 Adding the TextCell composable................................................................................................. 217
27.4 Adding a Box layout....................................................................................................................... 218
27.5 Box alignment................................................................................................................................. 219
27.6 BoxScope modifiers....................................................................................................................... 221
27.7 Using the clip() modifier............................................................................................................... 221
27.8 Summary......................................................................................................................................... 223
28. Custom Layout Modifiers...................................................................................................................... 225
28.1 Compose layout basics.................................................................................................................. 225
28.2 Custom layouts............................................................................................................................... 225
28.3 Creating the LayoutModifier project........................................................................................... 225
28.4 Adding the ColorBox composable............................................................................................... 226
28.5 Creating a custom layout modifier.............................................................................................. 227
28.6 Understanding default position................................................................................................... 227
28.7 Completing the layout modifier................................................................................................... 227
28.8 Using a custom modifier............................................................................................................... 228
28.9 Working with alignment lines...................................................................................................... 229
28.10 Working with baselines............................................................................................................... 231
28.11 Summary....................................................................................................................................... 231
29. Building Custom Layouts...................................................................................................................... 233
29.1 An overview of custom layouts.................................................................................................... 233
29.2 Custom layout syntax.................................................................................................................... 233
29.3 Using a custom layout.................................................................................................................... 234
29.4 Creating the CustomLayout project............................................................................................ 235
29.5 Creating the CascadeLayout composable................................................................................... 235
29.6 Using the CascadeLayout composable........................................................................................ 237
29.7 Summary......................................................................................................................................... 238
30. A Guide to ConstraintLayout in Compose........................................................................................... 239
30.1 An introduction to ConstraintLayout......................................................................................... 239
30.2 How ConstraintLayout works...................................................................................................... 239
30.2.1 Constraints............................................................................................................................... 239
30.2.2 Margins..................................................................................................................................... 240
30.2.3 Opposing constraints.............................................................................................................. 240
30.2.4 Constraint bias......................................................................................................................... 241
30.2.5 Chains....................................................................................................................................... 242
30.2.6 Chain styles.............................................................................................................................. 242
30.3 Configuring dimensions................................................................................................................ 243
30.4 Guideline helper............................................................................................................................. 243
30.5 Barrier helper.................................................................................................................................. 244
30.6 Summary......................................................................................................................................... 245
31. Working with ConstraintLayout in Compose...................................................................................... 247
31.1 Calling ConstraintLayout.............................................................................................................. 247
31.2 Generating references.................................................................................................................... 247
31.3 Assigning a reference to a composable........................................................................................ 247
31.4 Adding constraints......................................................................................................................... 248
31.5 Creating the ConstraintLayout project....................................................................................... 248
31.6 Adding the ConstraintLayout library.......................................................................................... 249
31.7 Adding a custom button composable.......................................................................................... 249
31.8 Basic constraints............................................................................................................................. 250
31.9 Opposing constraints..................................................................................................................... 251
31.10 Constraint bias............................................................................................................................. 252
31.11 Constraint margins...................................................................................................................... 253
31.12 The importance of opposing constraints and bias................................................................... 254
31.13 Creating chains............................................................................................................................. 257
31.14 Working with guidelines............................................................................................................. 258
31.15 Working with barriers................................................................................................................. 259
31.16 Decoupling constraints with constraint sets............................................................................ 262
31.17 Summary....................................................................................................................................... 264
32. Working with IntrinsicSize in Compose............................................................................................... 265
32.1 Intrinsic measurements................................................................................................................. 265
32.2 Max. vs Min. Intrinsic Size measurements................................................................................. 265
32.3 About the example project............................................................................................................ 266
32.4 Creating the IntrinsicSizeDemo project...................................................................................... 267
32.5 Creating the custom text field....................................................................................................... 267
32.6 Adding the Text and Box components........................................................................................ 268
32.7 Adding the top-level Column....................................................................................................... 268
32.8 Testing the project.......................................................................................................................... 269
32.9 Applying IntrinsicSize.Max measurements................................................................................ 269
32.10 Applying IntrinsicSize.Min measurements.............................................................................. 270
32.11 Summary....................................................................................................................................... 270
33. Coroutines and LaunchedEffects in Jetpack Compose......................................................................... 271
33.1 What are coroutines?..................................................................................................................... 271
33.2 Threads vs. coroutines................................................................................................................... 271
33.3 Coroutine Scope............................................................................................................................. 272
33.4 Suspend functions.......................................................................................................................... 272
33.5 Coroutine dispatchers.................................................................................................................... 272
33.6 Coroutine builders......................................................................................................................... 273
33.7 Jobs................................................................................................................................................... 273
33.8 Coroutines – suspending and resuming..................................................................................... 274
33.9 Coroutine channel communication............................................................................................. 275
33.10 Understanding side effects.......................................................................................................... 276
33.11 Summary....................................................................................................................................... 277
34. An Overview of Lists and Grids in Compose....................................................................................... 279
34.1 Standard vs. lazy lists..................................................................................................................... 279
34.2 Working with Column and Row lists.......................................................................................... 279
34.3 Creating lazy lists........................................................................................................................... 280
34.4 Enabling scrolling with ScrollState.............................................................................................. 281
34.5 Programmatic scrolling................................................................................................................. 281
34.6 Sticky headers................................................................................................................................. 282
34.7 Responding to scroll position....................................................................................................... 284
34.8 Creating a lazy grid........................................................................................................................ 284
34.9 Summary......................................................................................................................................... 287
35. A Compose Row and Column List Tutorial ......................................................................................... 289
35.1 Creating the ListDemo project..................................................................................................... 289
35.2 Creating a Column-based list....................................................................................................... 289
35.3 Enabling list scrolling.................................................................................................................... 291
35.4 Manual scrolling............................................................................................................................. 291
35.5 A Row list example......................................................................................................................... 294
35.6 Summary......................................................................................................................................... 294
36. A Compose Lazy List Tutorial .............................................................................................................. 295
36.1 Creating the LazyListDemo project............................................................................................. 295
36.2 Adding list data to the project...................................................................................................... 295
36.3 Reading the XML data................................................................................................................... 297
36.4 Handling image loading................................................................................................................ 298
36.5 Designing the list item composable............................................................................................. 300
36.6 Building the lazy list....................................................................................................................... 301
36.7 Testing the project.......................................................................................................................... 302
36.8 Making list items clickable............................................................................................................ 302
36.9 Summary......................................................................................................................................... 304
37. Lazy List Sticky Headers and Scroll Detection .................................................................................... 305
37.1 Grouping the list item data........................................................................................................... 305
37.2 Displaying the headers and items................................................................................................ 305
37.3 Adding sticky headers.................................................................................................................... 306
37.4 Reacting to scroll position............................................................................................................ 307
37.5 Adding the scroll button............................................................................................................... 309
37.6 Testing the finished app................................................................................................................. 310
37.7 Summary......................................................................................................................................... 311
38. A Compose Lazy Staggered Grid Tutorial ........................................................................................... 313
38.1 Lazy Staggered Grids..................................................................................................................... 313
38.2 Creating the StaggeredGridDemo project.................................................................................. 314
38.3 Adding the Box composable......................................................................................................... 315
38.4 Generating random height and color values.............................................................................. 315
38.5 Creating the Staggered List........................................................................................................... 316
38.6 Testing the project.......................................................................................................................... 317
38.7 Switching to a horizontal staggered grid..................................................................................... 318
38.8 Summary......................................................................................................................................... 319
39. Compose Visibility Animation............................................................................................................. 321
39.1 Creating the AnimateVisibility project....................................................................................... 321
39.2 Animating visibility....................................................................................................................... 321
39.3 Defining enter and exit animations............................................................................................. 324
39.4 Animation specs and animation easing...................................................................................... 325
39.5 Repeating an animation................................................................................................................ 327
39.6 Different animations for different children................................................................................ 327
39.7 Auto-starting an animation.......................................................................................................... 328
39.8 Implementing crossfading............................................................................................................ 329
39.9 Summary......................................................................................................................................... 331
40. Compose State-Driven Animation........................................................................................................ 333
40.1 Understanding state-driven animation....................................................................................... 333
40.2 Introducing animate as state functions....................................................................................... 333
40.3 Creating the AnimateState project............................................................................................... 334
40.4 Animating rotation with animateFloatAsState........................................................................... 334
40.5 Animating color changes with animateColorAsState............................................................... 337
40.6 Animating motion with animateDpAsState............................................................................... 339
40.7 Adding spring effects..................................................................................................................... 342
40.8 Working with keyframes............................................................................................................... 343
40.9 Combining multiple animations.................................................................................................. 344
40.10 Using the Animation Inspector.................................................................................................. 347
40.11 Summary....................................................................................................................................... 348
41. Canvas Graphics Drawing in Compose................................................................................................ 349
41.1 Introducing the Canvas component............................................................................................ 349
41.2 Creating the CanvasDemo project............................................................................................... 349
41.3 Drawing a line and getting the canvas size................................................................................. 349
41.4 Drawing dashed lines.................................................................................................................... 351
41.5 Drawing a rectangle....................................................................................................................... 351
41.6 Applying rotation........................................................................................................................... 355
41.7 Drawing circles and ovals.............................................................................................................. 356
41.8 Drawing gradients.......................................................................................................................... 357
41.9 Drawing arcs................................................................................................................................... 360
41.10 Drawing paths.............................................................................................................................. 361
41.11 Drawing points............................................................................................................................. 362
41.12 Drawing an image........................................................................................................................ 363
41.13 Drawing text................................................................................................................................. 365
41.14 Summary....................................................................................................................................... 367
42. Working with ViewModels in Compose............................................................................................... 369
42.1 What is Android Jetpack?............................................................................................................. 369
42.2 The “old” architecture.................................................................................................................... 369
42.3 Modern Android architecture...................................................................................................... 369
42.4 The ViewModel component......................................................................................................... 369
42.5 ViewModel implementation using state...................................................................................... 370
42.6 Connecting a ViewModel state to an activity............................................................................. 371
42.7 ViewModel implementation using LiveData.............................................................................. 372
42.8 Observing ViewModel LiveData within an activity.................................................................. 373
42.9 Summary......................................................................................................................................... 373
43. A Compose ViewModel Tutorial........................................................................................................... 375
43.1 About the project............................................................................................................................ 375
43.2 Creating the ViewModelDemo project....................................................................................... 376
43.3 Adding the ViewModel................................................................................................................. 376
43.4 Accessing DemoViewModel from MainActivity...................................................................... 377
43.5 Designing the temperature input composable........................................................................... 378
43.6 Designing the temperature input composable........................................................................... 380
43.7 Completing the user interface design.......................................................................................... 382
43.8 Testing the app................................................................................................................................ 384
43.9 Summary......................................................................................................................................... 384
44. An Overview of Android SQLite Databases......................................................................................... 385
44.1 Understanding database tables..................................................................................................... 385
44.2 Introducing database schema ...................................................................................................... 385
44.3 Columns and data types ............................................................................................................... 385
44.4 Database rows ................................................................................................................................ 386
44.5 Introducing primary keys ............................................................................................................ 386
44.6 What is SQLite?.............................................................................................................................. 386
44.7 Structured Query Language (SQL).............................................................................................. 386
44.8 Trying SQLite on an Android Virtual Device (AVD)............................................................... 387
44.9 The Android Room persistence library....................................................................................... 389
44.10 Summary....................................................................................................................................... 389
45. Room Databases and Compose ............................................................................................................ 391
45.1 Revisiting modern app architecture............................................................................................ 391
45.2 Key elements of Room database persistence.............................................................................. 391
45.2.1 Repository................................................................................................................................ 391
45.2.2 Room database........................................................................................................................ 392
45.2.3 Data Access Object (DAO).................................................................................................... 392
45.2.4 Entities...................................................................................................................................... 392
45.2.5 SQLite database....................................................................................................................... 392
45.3 Understanding entities.................................................................................................................. 393
45.4 Data Access Objects....................................................................................................................... 395
45.5 The Room database........................................................................................................................ 396
45.6 The Repository................................................................................................................................ 397
45.7 In-Memory databases.................................................................................................................... 398
45.8 Database Inspector......................................................................................................................... 399
45.9 Summary......................................................................................................................................... 399
46. A Compose Room Database and Repository Tutorial......................................................................... 401
46.1 About the RoomDemo project..................................................................................................... 401
46.2 Creating the RoomDemo project................................................................................................. 402
46.3 Modifying the build configuration.............................................................................................. 402
46.4 Building the entity.......................................................................................................................... 403
46.5 Creating the Data Access Object.................................................................................................. 404
46.6 Adding the Room database........................................................................................................... 405
46.7 Adding the repository.................................................................................................................... 406
46.8 Adding the ViewModel................................................................................................................. 408
46.9 Designing the user interface......................................................................................................... 410
46.10 Writing a ViewModelProvider Factory class............................................................................ 412
46.11 Completing the MainScreen function....................................................................................... 414
46.12 Testing the RoomDemo app....................................................................................................... 417
46.13 Using the Database Inspector..................................................................................................... 417
46.14 Summary....................................................................................................................................... 418
47. An Overview of Navigation in Compose.............................................................................................. 419
47.1 Understanding navigation............................................................................................................. 419
47.2 Declaring a navigation controller................................................................................................ 421
47.3 Declaring a navigation host.......................................................................................................... 421
47.4 Adding destinations to the navigation graph............................................................................. 421
47.5 Navigating to destinations............................................................................................................ 422
47.6 Passing arguments to a destination.............................................................................................. 424
47.7 Working with bottom navigation bars........................................................................................ 425
47.8 Summary......................................................................................................................................... 427
48. A Compose Navigation Tutorial........................................................................................................... 429
48.1 Creating the NavigationDemo project........................................................................................ 429
48.2 About the NavigationDemo project............................................................................................ 429
48.3 Declaring the navigation routes................................................................................................... 429
48.4 Adding the home screen............................................................................................................... 430
48.5 Adding the welcome screen.......................................................................................................... 431
48.6 Adding the profile screen.............................................................................................................. 432
48.7 Creating the navigation controller and host............................................................................... 433
48.8 Implementing the screen navigation........................................................................................... 433
48.9 Passing the user name argument.................................................................................................. 434
48.10 Testing the project........................................................................................................................ 435
48.11 Summary....................................................................................................................................... 437
49. A Compose Navigation Bar Tutorial..................................................................................................... 439
49.1 Creating the BottomBarDemo project........................................................................................ 439
49.2 Declaring the navigation routes................................................................................................... 439
49.3 Designing bar items....................................................................................................................... 440
49.4 Creating the bar item list............................................................................................................... 440
49.5 Adding the destination screens.................................................................................................... 441
49.6 Creating the navigation controller and host............................................................................... 443
49.7 Designing the navigation bar........................................................................................................ 443
49.8 Working with the Scaffold component........................................................................................ 445
49.9 Testing the project.......................................................................................................................... 446
49.10 Summary....................................................................................................................................... 446
50. Detecting Gestures in Compose............................................................................................................ 447
50.1 Compose gesture detection........................................................................................................... 447
50.2 Creating the GestureDemo project.............................................................................................. 447
50.3 Detecting click gestures................................................................................................................. 447
50.4 Detecting taps using PointerInputScope..................................................................................... 449
50.5 Detecting drag gestures................................................................................................................. 450
50.6 Detecting drag gestures using PointerInputScope..................................................................... 452
50.7 Scrolling using the scrollable modifier........................................................................................ 453
50.8 Scrolling using the scroll modifiers............................................................................................. 454
50.9 Detecting pinch gestures............................................................................................................... 456
50.10 Detecting rotation gestures......................................................................................................... 457
50.11 Detecting translation gestures.................................................................................................... 458
50.12 Summary....................................................................................................................................... 459

 This book aims to teach you how to build Android applications using Jetpack Compose 1.3, Android Studio Flamingo (2022.2.1), Material Design 3, and the Kotlin programming language.

 The book begins with the basics by explaining how to set up an Android Studio development environment.

 The book also includes in-depth chapters introducing the Kotlin programming language, including data types, operators, control flow, functions, lambdas, coroutines, and object-oriented programming.

 An introduction to the key concepts of Jetpack Compose and Android project architecture is followed by a guided tour of Android Studio in Compose development mode. The book also covers the creation of custom Composables and explains how functions are combined to create user interface layouts, including row, column, box, and list components.

 Other topics covered include data handling using state properties, key user interface design concepts such as modifiers, navigation bars, and user interface navigation. Additional chapters explore building your own reusable custom layout components.

 The book covers graphics drawing, user interface animation, transitions, Kotlin Flows, and gesture handling.

 Chapters also cover view models, SQLite databases, Room database access, the Database Inspector, live data, and custom theme creation. Using in-app billing, you will also learn to generate extra revenue from your app.

 Finally, the book explains how to package up a completed app and upload it to the Google Play Store for publication.

 Along the way, the topics covered in the book are put into practice through detailed tutorials, the source code for which is also available for download.

 Assuming you already have some rudimentary programming experience, are ready to download Android Studio and the Android SDK, and have access to a Windows, Mac, or Linux system, you are ready to start.


Похожее:

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

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