Jetpack Compose Basic
1
What you'll learn
How modifiers help you augment your composables.
How standard layout components like Column and
LazyRow position child composables.
How alignments and arrangements change the position of
child composables in their parent.
How Material composables like Scaffold and Bottom
Navigation help you create comprehensive layouts.
How to build flexible composables using slot APIs.
2
What you'll need
Have Android Studio Chipmunk or later installed.
Basic experience with Compose.
Basic knowledge of what a composable is, and what
modifiers are.
3
What you'll build
4
Q1: Why do we need Jetpack Compose?
-> The view gets more dynamic currently
-> syncing data between fragment and layout can cause
troubles
5
Q2: Compose versus Databinding?
-> Similar, but databinding mostly work on attributes, while
compose can change the UI under different condition easily
@Composable
fun MessageList(messages: List<String>) {
Column {
if (messages.size == 0 {
Text("No messages")
} else {
messages.forEach { message ->
Text(text = message)
}
}
}
6
Q3. Declarative UI?
You completely describe your UI for a given state
The framework updates your UI when the state changes
7
Q4. UI is immutable vs. Recomposition
When state change, you regenerate the entier UI
Solve the problem of syncing
If we want it to change, the code must reflect that
compare to view, you cannot hold a reference to it
and query it later
@Composable
fun MessageList(message: List<String>) {
Column {
var selectAll by remember { mutableStateOf(false) }
Checkbox(
checked = selectAll,
onCheckChange = { checked ->
selectAll = checked // this is essential to update checkbox visually
}
)
}
}
8
Go to this link to start the codelab
9
Issues you might encounter
Error says that it require Android 2021.2.1
As stated in the requirement, you will need Android
Studio Chipmunk to follow this codelab
Cannot import centain component correctly ( Image ,
Icon , lazy.items , lazy.grid.items )
Manually import these dependencies will solve this
issue
10