SlideShare a Scribd company logo
Declarative UIs with
Jetpack @Compose
Ramon Ribeiro Rabello
about.me/ramonrabello
Ramon Rabello
Android Developer
Motivation
The reasons behind Compose
Past mistakes - “API design is
building future regret”
~Chet Haase
View.java - The “God View” legacy
Weird inheritance
public class Button extends TextView {
Old View namings
public class Spinner extends AbsSpinner {
Old View namings
public class Spinner extends AbsSpinner {
DropDown
Old View namings
public class Spinner extends AbsSpinner {
ComboBox
Simplify building UI
Building UI is boilerplate
MyCustomView.[kt | java]
my_custom_view.xml
attrs.xml
style.xml
Building UI is boilerplate
MyCustomView.[kt | java]
my_custom_view.xml
attrs.xml
style.xml
Building UI is boilerplate
MyCustomView.[kt | java]
my_custom_view.xml
attrs.xml
style.xml
Building UI is boilerplate
MyCustomView.[kt | java]
my_custom_view.xml
attrs.xml
style.xml
Presentation architectures
Presentation Architectures
MVC
MVP
MVVM
MVI
Presentation Architectures Data Flow questions
- What is the source of truth?
Presentation Architectures Data Flow questions
- What is the source of truth?
- Who is the owner?
Presentation Architectures Data Flow questions
- What is the source of truth?
- Who is the owner?
- Who can change it?
Spinner: the weirdest case
public class MainActivity extends AppCompatActivity
implements Spinner.OnItemChangedListener {
What is the source of truth?
Who is the owner?
Who can change it?
onItemStateChanged() will be called after the event has happened
It turns out that android.widget
mixes state ownership and event
handling concepts
New UI toolkit goals
- Unbundled from platform releases (AndroidX)
- Fewer technology stack flowcharts (Custom View? Fragment?)
- Distinguish state ownership and event handling
- Write less code
What about building UI as simple as printing lines?
fun main(){
println("Hello Jetpack Compose")
}
Jetpack Compose
goes on stage
Introducing Jetpack Compose
What is Compose?
- Unbundled new set of UI widgets
- Inspired by React, Litho, Vue.js, Flutter
- A Kotlin compiler plugin
- Fully compatible with existing app code
- Experimental (0.1.0-dev02) - ** not ready for production yet **
Jetpack Compose Principles
UI as a function
@Composable
fun Hello(name: String){
Text(“Hello $name”)
}
Qualifier for Composables
Intercept and recompose the UI
tree
UI as a function (List of Composables)
@Composable
fun Trendings(newsList: List<News>){
for (news in newsList) {
NewsCard(news)
}
}
UI as a function (Dynamic composables)
@Composable
fun Trendings(news: List<News>){
if (news.isEmpty()) {
Text("You have no news today.")
} else {
NewsCard(news)
}
}
Composable building blocks
@Composable
fun Counter(model: CounterModel){
CounterTitle(model)
CounterContent(model)
CounterButtons(model)
}
@Composable
fun CounterHeader(model: CounterModel)
@Composable
fun CounterContent(model: CounterModel)
@Composable
fun CounterButtons(model: CounterModel)
Reactive UI upon model changes
@Composable
fun Counter(){
val model = CounterModel()
Column { Button(“Count”, onClick = { model.increment() }) }
Text(“Counter: ${model.value}”)
}
@Model
data class CounterModel(var value: Int = 0) {
fun increment() = value++
}
Indicates that composable updates
upon model changes
Top-down Data Flow
DataEvent
Top-down Data Flow
@Composable
fun Counter(model: CounterModel){
Column { Button(“Count”, onClick = { model.increment() }) }
Text(“Counter: ${model.value}”)
}
@Model
data class CounterModel(var value: Int = 0) {
fun increment() = value++
}
Data
Top-down Data Flow
@Composable
fun Counter(model: CounterModel){
Column { Button(“Count”, onClick = { model.increment() }) }
Text(“Counter: ${model.value}”)
}
@Model
data class CounterModel(var value: Int = 0) {
fun increment() = value++
}
Data
Top-down Data Flow
@Composable
fun Counter(model: CounterModel){
Column { Button(“Count”, onClick = { model.increment() }) }
Text(“Counter: ${model.value}”)
}
@Model
data class CounterModel(var value: Int = 0) {
fun increment() = value++
}
Data
Top-down Data Flow
@Composable
fun Counter(model: CounterModel){
Column { Button(“Count”, onClick = { model.increment() }) }
Text(“Counter: ${model.value}”)
}
@Model
data class CounterModel(var value: Int = 0) {
fun increment() = value++
}
Event
Getting Started
Download Android Studio
3.5+
(Gradle dependencies)
AS 4.0 Canary 1
(Built-in Compose Support)
UPDATE:
Add compose dev preview dependencies
def compose_version = '0.1.0-dev02'
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.50"
implementation "androidx.compose:compose-runtime:$compose_version"
kapt "androidx.compose:compose-compiler:$compose_version"
implementation "androidx.ui:ui-layout:$compose_version"
implementation "androidx.ui:ui-android-text:$compose_version"
Add compose dev preview dependencies
def compose_version = '0.1.0-dev02'
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.50"
implementation "androidx.compose:compose-runtime:$compose_version"
kapt "androidx.compose:compose-compiler:$compose_version"
implementation "androidx.ui:ui-layout:$compose_version"
implementation "androidx.ui:ui-android-text:$compose_version"
AS 4.0 Canary 1 - Create Composable Activity
Create a @Composable
@Composable
fun Counter() = MaterialTheme {
val model = CounterModel()
Row { Button(“Count”, onClick = { model.increment() }) }
Text(“Counter: ${model.value}”)
}
Add a @Model to represent the composable state
@Model
data class CounterModel(var value: Int = 0) {
fun hitCount() = value++
}
Enclose root composable inside setContent{ }
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent { Counter() }
}
}
Demo
What’s Next?
Learn more about Jetpack Compose
Learn more about Jetpack Compose
Jetpack Compose | Android Developers
Playing with Jetpack Compose dev preview
Diving into Jetpack Compose - Q42 Engineering - Medium
Declarative UI Patterns - Google I/O 2019
Android Jetpack Compose Review - Karumi Blog
https://www.github.com/ramonrabello/Compose-Counter
Thank you!
about.me/ramonrabello

More Related Content

What's hot (20)

PPTX
Kotlin Basics & Introduction to Jetpack Compose.pptx
takshilkunadia
 
PDF
Try Jetpack Compose
LutasLin
 
PDF
Jetpack compose
LutasLin
 
PDF
Jetpack Compose.pdf
SumirVats
 
PDF
Compose Camp - Jetpack Compose for Android Developers Introduction Session De...
JassGroup TICS
 
PPTX
Kotlin Jetpack Tutorial
Simplilearn
 
PDF
Jetpack Compose - A Lightning Tour
Matthew Clarke
 
PDF
Jetpack Compose beginner.pdf
AayushmaAgrawal
 
PPTX
Android Navigation Component
Łukasz Ciupa
 
PDF
Android Jetpack
Tudor Sirbu
 
PPT
React js
Jai Santhosh
 
PPTX
Android UI
nationalmobileapps
 
PPTX
Spring Boot and REST API
07.pallav
 
PDF
An introduction to React.js
Emanuele DelBono
 
ODP
Introduction to ReactJS
Knoldus Inc.
 
PDF
Spring Boot
Jaran Flaath
 
PPTX
Jetpack Compose - Android’s modern toolkit for building native UI
Gilang Ramadhan
 
PPSX
Junit
FAROOK Samath
 
PDF
React js
Rajesh Kolla
 
PDF
Basics of React Hooks.pptx.pdf
Knoldus Inc.
 
Kotlin Basics & Introduction to Jetpack Compose.pptx
takshilkunadia
 
Try Jetpack Compose
LutasLin
 
Jetpack compose
LutasLin
 
Jetpack Compose.pdf
SumirVats
 
Compose Camp - Jetpack Compose for Android Developers Introduction Session De...
JassGroup TICS
 
Kotlin Jetpack Tutorial
Simplilearn
 
Jetpack Compose - A Lightning Tour
Matthew Clarke
 
Jetpack Compose beginner.pdf
AayushmaAgrawal
 
Android Navigation Component
Łukasz Ciupa
 
Android Jetpack
Tudor Sirbu
 
React js
Jai Santhosh
 
Android UI
nationalmobileapps
 
Spring Boot and REST API
07.pallav
 
An introduction to React.js
Emanuele DelBono
 
Introduction to ReactJS
Knoldus Inc.
 
Spring Boot
Jaran Flaath
 
Jetpack Compose - Android’s modern toolkit for building native UI
Gilang Ramadhan
 
React js
Rajesh Kolla
 
Basics of React Hooks.pptx.pdf
Knoldus Inc.
 

Similar to Declarative UIs with Jetpack Compose (20)

PDF
Reactive UI in android - Gil Goldzweig Goldbaum, 10bis
DroidConTLV
 
PPTX
Compose UI
Ulvi Jabbarli
 
PPTX
Modern app development with Jetpack Compose.pptx
Md Shamsul Arafin Mahtab
 
PDF
Compose Camp Day 3 PPT.pptx.pdf
METDSC
 
PDF
compose_speaker_session.pdf
AnkurAgarwal151093
 
PDF
What's new in android: jetpack compose 2024
Toru Wonyoung Choi
 
PPTX
compose 3.pptx
SumirVats
 
PDF
Mobile Programming - 2 Jetpack Compose
AndiNurkholis1
 
PPTX
Compose in Theory
Garth Gilmour
 
PPTX
Compose camp 2.pptx
MadheswarKonidela
 
PPTX
Compose Camp Day 2.pptx
RajatKumarNayak5
 
PPTX
Compose Camp.pptx
YASHKUMARIIITDharwad
 
PPTX
Session-1 edited.pptx
scienceTech11
 
PDF
Compose Camp 1.pdf
AbhishekS325285
 
PDF
Compose Camp 1.pdf
AbhishekS325285
 
PDF
Andriod Blueprint @ AISSMS Institute Of Information Techonology
tecopo6080
 
PDF
Mobile Programming - 6 Textfields, Button, Showing Snackbars and Lists
AndiNurkholis1
 
PDF
Era of declarative ui
Sadman Samee
 
PPTX
Kotlin With JetPack Compose Presentation
Knoldus Inc.
 
PDF
Jetpack Compose a nova forma de implementar UI no Android
Nelson Glauber Leal
 
Reactive UI in android - Gil Goldzweig Goldbaum, 10bis
DroidConTLV
 
Compose UI
Ulvi Jabbarli
 
Modern app development with Jetpack Compose.pptx
Md Shamsul Arafin Mahtab
 
Compose Camp Day 3 PPT.pptx.pdf
METDSC
 
compose_speaker_session.pdf
AnkurAgarwal151093
 
What's new in android: jetpack compose 2024
Toru Wonyoung Choi
 
compose 3.pptx
SumirVats
 
Mobile Programming - 2 Jetpack Compose
AndiNurkholis1
 
Compose in Theory
Garth Gilmour
 
Compose camp 2.pptx
MadheswarKonidela
 
Compose Camp Day 2.pptx
RajatKumarNayak5
 
Compose Camp.pptx
YASHKUMARIIITDharwad
 
Session-1 edited.pptx
scienceTech11
 
Compose Camp 1.pdf
AbhishekS325285
 
Compose Camp 1.pdf
AbhishekS325285
 
Andriod Blueprint @ AISSMS Institute Of Information Techonology
tecopo6080
 
Mobile Programming - 6 Textfields, Button, Showing Snackbars and Lists
AndiNurkholis1
 
Era of declarative ui
Sadman Samee
 
Kotlin With JetPack Compose Presentation
Knoldus Inc.
 
Jetpack Compose a nova forma de implementar UI no Android
Nelson Glauber Leal
 
Ad

More from Ramon Ribeiro Rabello (20)

PDF
Android Jetpack + Coroutines: To infinity and beyond
Ramon Ribeiro Rabello
 
PDF
Create Modern Apps with Android Jetpack
Ramon Ribeiro Rabello
 
PDF
Cultura de testes em times mobile
Ramon Ribeiro Rabello
 
PDF
Ninja Productivity in Android Studio
Ramon Ribeiro Rabello
 
PDF
Produtividade ninja com android studio
Ramon Ribeiro Rabello
 
PDF
Automatize seus testes de UI com a Espresso!
Ramon Ribeiro Rabello
 
PDF
Os caminhos da Agilidade em Empresa Pública
Ramon Ribeiro Rabello
 
PDF
Making your app see with Mobile Vision API
Ramon Ribeiro Rabello
 
PDF
Inovar em tempos de crise? Yes, We Can!
Ramon Ribeiro Rabello
 
PDF
O ecossistema android
Ramon Ribeiro Rabello
 
PDF
Android Marshmallow na prática
Ramon Ribeiro Rabello
 
PDF
Android Wear: Estendendo sua app para relógios inteligentes
Ramon Ribeiro Rabello
 
PDF
Introdução ao Android Studio
Ramon Ribeiro Rabello
 
PDF
O caminho de um desenvolvedor android
Ramon Ribeiro Rabello
 
PDF
Criando Apps Sociais em Android
Ramon Ribeiro Rabello
 
PDF
Porque Aprender Android
Ramon Ribeiro Rabello
 
PDF
Workshop Android em Ambientes de Integração
Ramon Ribeiro Rabello
 
PDF
De idealista à empreendedor - como desenvolver aplicações em android que conq...
Ramon Ribeiro Rabello
 
PDF
Desenvolvimento Web para Android
Ramon Ribeiro Rabello
 
PDF
Agora é Android, Tá Safo? - #tasafoemacaocastanhal
Ramon Ribeiro Rabello
 
Android Jetpack + Coroutines: To infinity and beyond
Ramon Ribeiro Rabello
 
Create Modern Apps with Android Jetpack
Ramon Ribeiro Rabello
 
Cultura de testes em times mobile
Ramon Ribeiro Rabello
 
Ninja Productivity in Android Studio
Ramon Ribeiro Rabello
 
Produtividade ninja com android studio
Ramon Ribeiro Rabello
 
Automatize seus testes de UI com a Espresso!
Ramon Ribeiro Rabello
 
Os caminhos da Agilidade em Empresa Pública
Ramon Ribeiro Rabello
 
Making your app see with Mobile Vision API
Ramon Ribeiro Rabello
 
Inovar em tempos de crise? Yes, We Can!
Ramon Ribeiro Rabello
 
O ecossistema android
Ramon Ribeiro Rabello
 
Android Marshmallow na prática
Ramon Ribeiro Rabello
 
Android Wear: Estendendo sua app para relógios inteligentes
Ramon Ribeiro Rabello
 
Introdução ao Android Studio
Ramon Ribeiro Rabello
 
O caminho de um desenvolvedor android
Ramon Ribeiro Rabello
 
Criando Apps Sociais em Android
Ramon Ribeiro Rabello
 
Porque Aprender Android
Ramon Ribeiro Rabello
 
Workshop Android em Ambientes de Integração
Ramon Ribeiro Rabello
 
De idealista à empreendedor - como desenvolver aplicações em android que conq...
Ramon Ribeiro Rabello
 
Desenvolvimento Web para Android
Ramon Ribeiro Rabello
 
Agora é Android, Tá Safo? - #tasafoemacaocastanhal
Ramon Ribeiro Rabello
 
Ad

Declarative UIs with Jetpack Compose

  • 1. Declarative UIs with Jetpack @Compose Ramon Ribeiro Rabello
  • 4. Past mistakes - “API design is building future regret” ~Chet Haase
  • 5. View.java - The “God View” legacy
  • 6. Weird inheritance public class Button extends TextView {
  • 7. Old View namings public class Spinner extends AbsSpinner {
  • 8. Old View namings public class Spinner extends AbsSpinner { DropDown
  • 9. Old View namings public class Spinner extends AbsSpinner { ComboBox
  • 11. Building UI is boilerplate MyCustomView.[kt | java] my_custom_view.xml attrs.xml style.xml
  • 12. Building UI is boilerplate MyCustomView.[kt | java] my_custom_view.xml attrs.xml style.xml
  • 13. Building UI is boilerplate MyCustomView.[kt | java] my_custom_view.xml attrs.xml style.xml
  • 14. Building UI is boilerplate MyCustomView.[kt | java] my_custom_view.xml attrs.xml style.xml
  • 17. Presentation Architectures Data Flow questions - What is the source of truth?
  • 18. Presentation Architectures Data Flow questions - What is the source of truth? - Who is the owner?
  • 19. Presentation Architectures Data Flow questions - What is the source of truth? - Who is the owner? - Who can change it?
  • 20. Spinner: the weirdest case public class MainActivity extends AppCompatActivity implements Spinner.OnItemChangedListener { What is the source of truth? Who is the owner? Who can change it? onItemStateChanged() will be called after the event has happened
  • 21. It turns out that android.widget mixes state ownership and event handling concepts
  • 22. New UI toolkit goals - Unbundled from platform releases (AndroidX) - Fewer technology stack flowcharts (Custom View? Fragment?) - Distinguish state ownership and event handling - Write less code
  • 23. What about building UI as simple as printing lines? fun main(){ println("Hello Jetpack Compose") }
  • 24. Jetpack Compose goes on stage Introducing Jetpack Compose
  • 25. What is Compose? - Unbundled new set of UI widgets - Inspired by React, Litho, Vue.js, Flutter - A Kotlin compiler plugin - Fully compatible with existing app code - Experimental (0.1.0-dev02) - ** not ready for production yet **
  • 27. UI as a function @Composable fun Hello(name: String){ Text(“Hello $name”) } Qualifier for Composables Intercept and recompose the UI tree
  • 28. UI as a function (List of Composables) @Composable fun Trendings(newsList: List<News>){ for (news in newsList) { NewsCard(news) } }
  • 29. UI as a function (Dynamic composables) @Composable fun Trendings(news: List<News>){ if (news.isEmpty()) { Text("You have no news today.") } else { NewsCard(news) } }
  • 30. Composable building blocks @Composable fun Counter(model: CounterModel){ CounterTitle(model) CounterContent(model) CounterButtons(model) } @Composable fun CounterHeader(model: CounterModel) @Composable fun CounterContent(model: CounterModel) @Composable fun CounterButtons(model: CounterModel)
  • 31. Reactive UI upon model changes @Composable fun Counter(){ val model = CounterModel() Column { Button(“Count”, onClick = { model.increment() }) } Text(“Counter: ${model.value}”) } @Model data class CounterModel(var value: Int = 0) { fun increment() = value++ } Indicates that composable updates upon model changes
  • 33. Top-down Data Flow @Composable fun Counter(model: CounterModel){ Column { Button(“Count”, onClick = { model.increment() }) } Text(“Counter: ${model.value}”) } @Model data class CounterModel(var value: Int = 0) { fun increment() = value++ } Data
  • 34. Top-down Data Flow @Composable fun Counter(model: CounterModel){ Column { Button(“Count”, onClick = { model.increment() }) } Text(“Counter: ${model.value}”) } @Model data class CounterModel(var value: Int = 0) { fun increment() = value++ } Data
  • 35. Top-down Data Flow @Composable fun Counter(model: CounterModel){ Column { Button(“Count”, onClick = { model.increment() }) } Text(“Counter: ${model.value}”) } @Model data class CounterModel(var value: Int = 0) { fun increment() = value++ } Data
  • 36. Top-down Data Flow @Composable fun Counter(model: CounterModel){ Column { Button(“Count”, onClick = { model.increment() }) } Text(“Counter: ${model.value}”) } @Model data class CounterModel(var value: Int = 0) { fun increment() = value++ } Event
  • 38. Download Android Studio 3.5+ (Gradle dependencies) AS 4.0 Canary 1 (Built-in Compose Support) UPDATE:
  • 39. Add compose dev preview dependencies def compose_version = '0.1.0-dev02' implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.50" implementation "androidx.compose:compose-runtime:$compose_version" kapt "androidx.compose:compose-compiler:$compose_version" implementation "androidx.ui:ui-layout:$compose_version" implementation "androidx.ui:ui-android-text:$compose_version"
  • 40. Add compose dev preview dependencies def compose_version = '0.1.0-dev02' implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.50" implementation "androidx.compose:compose-runtime:$compose_version" kapt "androidx.compose:compose-compiler:$compose_version" implementation "androidx.ui:ui-layout:$compose_version" implementation "androidx.ui:ui-android-text:$compose_version"
  • 41. AS 4.0 Canary 1 - Create Composable Activity
  • 42. Create a @Composable @Composable fun Counter() = MaterialTheme { val model = CounterModel() Row { Button(“Count”, onClick = { model.increment() }) } Text(“Counter: ${model.value}”) }
  • 43. Add a @Model to represent the composable state @Model data class CounterModel(var value: Int = 0) { fun hitCount() = value++ }
  • 44. Enclose root composable inside setContent{ } class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { Counter() } } }
  • 45. Demo
  • 46. What’s Next? Learn more about Jetpack Compose
  • 47. Learn more about Jetpack Compose Jetpack Compose | Android Developers Playing with Jetpack Compose dev preview Diving into Jetpack Compose - Q42 Engineering - Medium Declarative UI Patterns - Google I/O 2019 Android Jetpack Compose Review - Karumi Blog https://www.github.com/ramonrabello/Compose-Counter