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

Introduction to Jetpack Compose (By: Tauheed Al...

Introduction to Jetpack Compose (By: Tauheed Ali) - Android Seekho Season 2

Talk by Tauheed Ali (https://www.linkedin.com/in/tauheed007/) at Android Seekho Season 2 by GDG Lahore.

GDG Lahore

June 10, 2023
Tweet

More Decks by GDG Lahore

Other Decks in Programming

Transcript

  1. This work is licensed under the Apache 2.0 License What

    is Android Seekho? #Android Seekho is an online self-study program to help developers to learn Android Development using the first-class support of Jetpack Compose * Note: One session will utilize Kotlin Koans material
  2. This work is licensed under the Apache 2.0 License •

    What is Compose ??? Jetpack Compose
  3. This work is licensed under the Apache 2.0 License You

    are not a coder or developer you are problem solver code is just your tool so don't spend all your time only thinking about code instead invest time into understanding the problem and exploring other tools you can add to your tool box then tackle the problem with a fresh perspective ProTip for developers
  4. This work is licensed under the Apache 2.0 License <LinearLayout

    android:orientation=“horizontal” > <ImageView android:id=”@+id/answer_image” ... /> <TextView android:id=”@+id/answer_text” ... /> <RadioButton android:id=”@+id/answer_radio_button” ... /> </LinearLayout> <!-- survey_answer.xml -->
  5. This work is licensed under the Apache 2.0 License class

    SurveyQuestionActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val image = findViewById(R.id.answer_image) val text = findViewById(R.id.answer_text) val radioButton = findViewById(R.id.answer_radio_button) // ... } } // SurveyQuestionActivity.kt
  6. This work is licensed under the Apache 2.0 License class

    SurveyQuestionActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { // ... image.setImage(...) text.setText(...) // ... } } // SurveyQuestionActivity.kt
  7. This work is licensed under the Apache 2.0 License Composable

    functions Composable functions are the basic building block of a UI in Compose. A composable function: • Describes some part of your UI. • Doesn't return anything. • Takes some input and generates what's shown on the screen. • Might emit several UI elements.
  8. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Row { Image(answer.image) Text(answer.text) RadioButton(false, onClick = { /* … */ }) } } // SurveyAnswer.kt
  9. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Row { Image(answer.image) Text(answer.text) RadioButton(false, onClick = { /* … */ }) } } // SurveyAnswer.kt
  10. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Row { Image(answer.image) Text(answer.text) RadioButton(false, onClick = { /* … */ }) } } // SurveyAnswer.kt
  11. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Row { / * ... */ var selected: Boolean = // ... RadioButton(selected, onClick = { /* … */ }) } } // SurveyAnswer.kt
  12. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Row { / * ... */ var selected: Boolean = // ... RadioButton(selected, onClick = { selected = !selected }) } } // SurveyAnswer.kt
  13. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Row { / * ... */ var selected: Boolean = // ... RadioButton(selected, onClick = { selected = !selected }) } } // SurveyAnswer.kt
  14. This work is licensed under the Apache 2.0 License Code

    is the enemy for developers every developer should write as little code as possible here's why more code means more potential bugs more code means more code to maintain more code means more effort to rewrite later strive for less more readable code always ProTip for developers
  15. This work is licensed under the Apache 2.0 License Layout

    Modifiers Modifiers are used to decorate or add behavior to Jetpack Compose UI elements. For example, you can add backgrounds, padding or behavior to rows, text, or buttons. To set them, a composable or a layout needs to accept a modifier as a parameter.
  16. This work is licensed under the Apache 2.0 License MaterialTheme(

    colorScheme = MyAppsColorScheme, typography = MyAppsTypography, shapes = MyAppsShapes ) { // Content goes here }
  17. This work is licensed under the Apache 2.0 License Scaffold(

    topBar = { SmallTopAppBar(/* ... */) }, floatingActionButtonPosition = FabPosition.End, floatingActionButton = { FloatingActionButton(/* ... */) }, content = { /* ... */ } )
  18. This work is licensed under the Apache 2.0 License Surface

    Surface in Jetpack Compose is a composable that represents a visual container or surface on which you can place other UI elements. It serves as a canvas for building and organizing your user interface.
  19. This work is licensed under the Apache 2.0 License Surface

    { Text("Hello Compose") } Hello Compose
  20. This work is licensed under the Apache 2.0 License Surface(

    color = MaterialTheme.colorScheme.primary, ) { Text("Hello Compose") } Hello Compose
  21. This work is licensed under the Apache 2.0 License Surface(

    color = MaterialTheme.colorScheme.primary, shape = RoundedCornerShape(8.dp), ) { Text("Hello Compose") } Hello Compose
  22. This work is licensed under the Apache 2.0 License Surface(

    color = MaterialTheme.colorScheme.surface, shape = RoundedCornerShape(8.dp), border = BorderStroke(2.dp, MaterialTheme.colorScheme.outline ) ) { Text("Hello Compose") } Hello Compose
  23. This work is licensed under the Apache 2.0 License Surface(

    color = MaterialTheme.colorScheme.surface, shape = RoundedCornerShape(8.dp), border = BorderStroke(2.dp, MaterialTheme.colorScheme.surfaceVariant ), shadowElevation = 8.dp, tonalElevation = 8.dp, ) { Text("Hello Compose") } Hello Compose
  24. This work is licensed under the Apache 2.0 License Row

    { Component1() Component2() Component3() } 1 2 3 Row
  25. This work is licensed under the Apache 2.0 License 1

    2 3 Column Column { Component1() Component2() Component3() }
  26. This work is licensed under the Apache 2.0 License 2

    1 3 Box Box { Component1() Component2() Component3() }
  27. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Row { Image(answer.image) Text(answer.text) RadioButton(/* … */) } }
  28. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Row( verticalAlignment = Alignment.CenterVertically ) { /* ... */ } }
  29. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween ) { /* ... */ } }
  30. This work is licensed under the Apache 2.0 License Modifiers

    So far, we’ve covered styling, components, and layouts. But how do we control things like sizes, padding, and other appearances? You do that by using Modifiers. Modifiers allow you to decorate or augment a composable. Every composable function in the compose toolkit accepts a Modifier as a parameter. Modifiers can be chained together until you achieve your desired
  31. This work is licensed under the Apache 2.0 License Text(

    "Hello Compose!", Modifier.background(Color.Magenta) ) Hello Compose
  32. This work is licensed under the Apache 2.0 License Text(

    "Hello Compose!", Modifier.background(Color.Magenta) .size(200.dp, 30.dp) ) Hello Compose
  33. This work is licensed under the Apache 2.0 License Text(

    "Hello Compose!", Modifier.background(Color.Magenta) .size(200.dp, 30.dp) .padding(5.dp) ) Hello Compose
  34. This work is licensed under the Apache 2.0 License Text(

    "Hello Compose!", Modifier.background(Color.Magenta) .size(200.dp, 30.dp) .padding(5.dp) .alpha(0.5f) ) Hello Compose
  35. This work is licensed under the Apache 2.0 License Text(

    "Hello Compose!", Modifier.background(Color.Magenta) .size(200.dp, 30.dp) .padding(5.dp) .alpha(0.5f) .clickable { // Called when Text clicked } ) Hello Compose
  36. This work is licensed under the Apache 2.0 License Box(Modifier.size(150.dp))

    { Text( "Hello Compose!", Modifier.align( Alignment.BottomEnd ) ) } Hello Compose
  37. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Row(...) { Image(answer.image) Text(answer.text) RadioButton(/* … */) } } Desired Current
  38. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Row( Modifier.fillMaxWidth(), /* ... */ ) { Image(answer.image) Text(answer.text) RadioButton(/* ... */) } } Desired Current
  39. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Row( Modifier.fillMaxWidth() .padding(16.dp), /* ... */ ) { Image(answer.image) Text(answer.text) RadioButton(/* ... */) } } Desired Current
  40. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Surface( border = BorderStroke( 1.dp, MaterialTheme.colorScheme.outline ), shape = MaterialTheme.shapes.small ) { Row(/* ... */) { } } } Desired Current
  41. This work is licensed under the Apache 2.0 License @Composable

    fun SurveyAnswer(answer: Answer) { Surface( border = BorderStroke( 1.dp, MaterialTheme.colorScheme.outline ), shape = MaterialTheme.shapes.small ) { Row(Modifier.fillMaxWidth().padding(16.dp)) { Image(answer.image) Text(answer.text) RadioButton(/* … */) } } }