Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction Jetpack Compose

Introduction Jetpack Compose

A simple explanation about the new UI toolkit for Android Development -- Jetpack Compose.

Avatar for M. Taufiq Hidayat

M. Taufiq Hidayat

July 22, 2023
Tweet

Other Decks in Technology

Transcript

  1. What is Jetpack Compose? The idea behind the framework Jetpack

    Compose is a new and fully declarative way to design and render Android user interfaces. It relies on composable functions, modifiers and reacting to state changes to display beautiful, material-design-inspired UI using Kotlin!
  2. Benefits of using Jetpack Compose Less code Do more with

    less code and avoid entire classes of bugs. Code is simpler and easier to maintain Intuitive Just describe your UI, and Compose takes care of the rest. As app state changes, your UI automatically updates. Accelerates Development Compatible with all your existing code so you can adopt when and where you want. Iterate fast with live previews and full Android Studio support. Powerful Create beautiful apps with direct access to the Android platform APIs and built-in support for Material Design, Dark theme, animations, and more
  3. @Composable fun CartItem() { var quantity = ... ¯\_(ツ)_/¯ ...

    ? Row { Button(onClick = { quantity++ }) { Text("+") } Text(quantity.toString()) Button(onClick = { quantity-- }) { Text("-") } } } How to keep track of the quantity?
  4. @Composable fun CartItem() { var quantity: Int = 1 Row

    { Button(onClick = { quantity++ }) { Text("+") } Text(quantity.toString()) Button(onClick = { quantity-- }) { Text("-") } } } State not tracked by Compose
  5. @Composable fun CartItem() { var quantity: Int = 1 Row

    { Button(onClick = { quantity++ }) { Text("+") } Text(quantity.toString()) Button(onClick = { quantity-- }) { Text("-") } } } State not tracked by Compose This state is not tracked by Compose
  6. Compose State APIs • State<T> • MutableState<T> interface State<out T>

    { val value: T } interface MutableState<T> : State<T> { override var value: T ... } I I
  7. @Composable fun CartItem() { val quantity: MutableState<Int> = mutableStateOf(1) Row

    { Button(onClick = { quantity.value++ }) { Text("+") } Text(quantity.value.toString()) Button(onClick = { quantity.value-- }) { Text("-") } } } State tracked and remembered by Compose
  8. @Composable fun CartItem() { val quantity = remember { mutableStateOf(1)

    } Row { Button(onClick = { quantity.value++ }) { Text("+") } Text(quantity.value.toString()) Button(onClick = { quantity.value-- }) { Text("-") } } } State tracked and remembered by Compose
  9. @Composable fun CartItem() { val quantity = remember { mutableStateOf(1)

    } Row { Button(onClick = { quantity.value++ }) { Text("+") } Text(quantity.value.toString()) Button(onClick = { quantity.value-- }) { Text("-") } } } State tracked and remembered by Compose
  10. @Composable fun CartItem() { val quantity = remember { mutableStateOf(1)

    } Row { Button(onClick = { quantity.value++ }) { Text("+") } Text(quantity.value.toString()) Button(onClick = { quantity.value-- }) { Text("-") } } } State tracked and remembered by Compose
  11. @Composable fun CartItem() { var quantity by rememberSaveable { mutableStateOf(1)

    } Row { Button(onClick = { quantity++ }) { Text("+") } Text(quantity.toString()) Button(onClick = { quantity-- }) { Text("-") } } } Property delegate
  12. @Composable fun CartItem() { val quantity = rememberSaveable { mutableStateOf(1)

    } Row { Button(onClick = { quantity.value++ }) { Text("+") } Text(quantity.value.toString()) Button(onClick = { quantity.value-- }) { Text("-") } } } BONUS — rememberSaveable State survives configuration changes
  13. How about List? 1 2 1. LazyColumn produces a vertical

    scrolling list of an unknown length 2. LazyRow produces a horizontal scrolling list of an unknown length
  14. buildFeatures { ... compose true } dependencies { val composeBom

    = platform("androidx.compose:compose-bom:2022.10.00") implementation composeBom implementation("androidx.compose.material:material") implementation("androidx.compose.foundation:foundation") implementation("androidx.compose.ui:ui") implementation("androidx.compose.ui:ui-tooling-preview") } 1. app/build.gradle