Share
Embed
A Composable New World
As our Twitter streams become flooded with the release of Jetpack Compose 1.0, it's time to jump into the UI declarative world and reform the XML and all of the `findViewById` calls that exist scattered throughout the code.
Join me in this talk to see the first steps into this composable new world and build your first app (with Compose)!
cmota
August 11, 2021
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
135 slides
-
👨💻 Android GDE 🗣 Founder @GDGCoimbra and co-founder @Kotlin_Knights ✍
Author @rwenderlich 🎙 Podcaster wannabe 🗺 Loves travel, photography and running @cafonsomota -
Android * Declarative UI Patterns (I/O’19) - https://www.youtube.com/watch?v=VsStyq4Lzxo 2008 2009
2010 1.0 A lot of Android version in between Android Studio 2013 2014 2015 2016 2017 2018 ART RecyclerView Constraint Layout Arch Components Kotlin -
1. Git Clone • github.com/cmota/unsplash-compose 2. Download Android Studio Arctic
Fox* • developer.android.com/studio 3. Create your Unsplash API account • unsplash.com/developers Materials * ⚠ There’s a Specific version for Mac m1 -
What’s Compose? verb 1. write or create (a work
of art, especially music or poetry). "he composed the First Violin Sonata four years earlier" Definitions from Oxford Languages -
Jetpack Compose MODERN FRAMEWORK Declarative UI Less code Smaller APK’s
Kotlin Accelerate development Intuitive Open-source Unbundled from OS -
Jetpack Compose MODERN FRAMEWORK Declarative UI Less code Kotlin Accelerate
development Intuitive Open-source Smaller APK’s Unbundled from OS -
Imperative UI Exercise: Implementing a List This is item 1
This is item 2 This is item 3 This is item 4 This is item 5 This is item 6 -
Imperative UI Exercise: Implementing a List 1. Create an activity
2. Create the correspondent xml activity_main.xml -
Imperative UI Exercise: Implementing a List 1. Create an activity
2. Create the correspondent xml 3. Create a fragment MainFragment.kt -
Imperative UI Exercise: Implementing a List 1. Create an activity
2. Create the correspondent xml 3. Create a fragment 4. Create the correspondent xml fragment_main.xml -
Imperative UI Exercise: Implementing a List 1. Create an activity
2. Create the correspondent xml 3. Create a fragment 4. Create the correspondent xml 5. Create a recycler view RecyclerView -
Imperative UI Exercise: Implementing a List 1. Create an activity
2. Create the correspondent xml 3. Create a fragment 4. Create the correspondent xml 5. Create a recycler view 6. Create the adapter Adapter -
Imperative UI Exercise: Implementing a List 1. Create an activity
2. Create the correspondent xml 3. Create a fragment 4. Create the correspondent xml 5. Create a recycler view 6. Create the adapter 7. Create the correspondent xml for the items item_adapter.xml -
Imperative UI Exercise: Implementing a List item_adapter.xml files created 5
(It can be more if we want to add further customization) Lines of code written +200 (Between declarations and xml attributes) -
Imperative UI Exercise: Implementing a List item_adapter.xml • Time consuming
• Error prone • Several files created • Increasing APK size • Coupled components -
How to change the access and change the state of
a view Imperative UI Exercise: Changing the state of a view Hello world. A brief history on or -
Imperative UI Exercise: Changing the state of a view Hello
world. android:Id = “@+id/tv_greeting” Text TextView greetings = (TextView) findViewById(R.id.tv_greeting) greetings.text = “Hello world.” -
Imperative UI Exercise: Changing the state of a view Hello
world. android:Id = “@+id/tv_greeting” Text @BindView(R.id.tv_greeting) TextView greetings; greetings.text = “Hello world.” github.com/JakeWharton/butterknife -
Imperative UI Exercise: Changing the state of a view Hello
world. android:Id = “@+id/tv_greeting” Text TextView greetings = (TextView) findViewById(R.id.tv_greeting) greetings.text = “Hello world.” -
Imperative UI Exercise: Changing the state of a view Hello
world. android:Id = “@+id/tv_greeting” Text TextView greetings = findViewById(R.id.tv_greeting) greetings.text = “Hello world.” -
Imperative UI Exercise: Changing the state of a view Hello
world. android:Id = “@+id/tv_greeting” Text import kotlinx.android.synthetic.activity_main.* tv_greeting.text = “Hello world.” -
Imperative UI Exercise: Changing the state of a view Hello
world. android:Id = “@+id/tv_greeting” Text import kotlinx.android.synthetic.activity_main.* tv_greeting.text = “Hello world.” DEPRECATED -
Imperative UI Exercise: Changing the state of a view Hello
world. android:Id = “@+id/tv_greeting” Text buildFeatures.viewBinding true private lateinit var binding: FragmentMainBinding override fun onCreateView( infltr: LayoutInflater, container: ViewGroup?, state: Bundle?): View { binding = FragmentMainBinding.inflate(infltr, container, false) return binding.root } binding.tvGreeting.text = “Hello world.” build.gradle -
Imperative UI android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/View.java View.java 30407 Lines of code TextView extends
ImageView ViewGroup extends LinearLayout RelativeLayout … -
Imperative UI With 30407 lines of code and several classes
that extend it - Difficult to scale - Hard to maintain - Every change might reflect on a lot of classes - Bundled to the OS and OEM’s implementation of these components -
Goals Unbundle from platform releases Faster to implement new designs
(views/components) Clarify state ownership and event handling * Adapted from declarative UI patterns (Google I/O’19) - https://www.youtube.com/watch?v=VsStyq4Lzxo&t -
Shifting paradigms Imperative UI Ui as a function | Kotlin
code XML java | kotlin code Layouts Attrs Styles … Declarative ui -
Shifting paradigms Imperative UI What should happen How it should
happen Declarative ui -
Shifting paradigms medium.com/androiddevelopers/understanding-jetpack-compose-part-1-of-2-ca316fe39050 fun updateCount(count: Int) { if (count >
0 && !hasBadge()) { addBadge() } else if (count == 0 && hasBadge()) { removeBadge() } if (count > 99 && !hasFire()) { addFire() setBadgeText("99+") } else if (count <= 99 && hasFire()) { removeFire() } if (count > 0 && !hasPaper()) { addPaper() } else if (count == 0 && hasPaper()) { removePaper() } if (count <= 99) { setBadgeText("$count") } } Imperative UI -
Shifting paradigms medium.com/androiddevelopers/understanding-jetpack-compose-part-1-of-2-ca316fe39050 fun updateCount(count: Int) { if (count >
0 && !hasBadge()) { addBadge() } else if (count == 0 && hasBadge()) { removeBadge() } if (count > 99 && !hasFire()) { addFire() setBadgeText("99+") } else if (count <= 99 && hasFire()) { removeFire() } if (count > 0 && !hasPaper()) { addPaper() } else if (count == 0 && hasPaper()) { removePaper() } if (count <= 99) { setBadgeText("$count") } } Imperative UI Showing/hiding badge -
Shifting paradigms medium.com/androiddevelopers/understanding-jetpack-compose-part-1-of-2-ca316fe39050 fun updateCount(count: Int) { if (count >
0 && !hasBadge()) { addBadge() } else if (count == 0 && hasBadge()) { removeBadge() } if (count > 99 && !hasFire()) { addFire() setBadgeText("99+") } else if (count <= 99 && hasFire()) { removeFire() } if (count > 0 && !hasPaper()) { addPaper() } else if (count == 0 && hasPaper()) { removePaper() } if (count <= 99) { setBadgeText("$count") } } Imperative UI Showing/hiding badge -
Shifting paradigms medium.com/androiddevelopers/understanding-jetpack-compose-part-1-of-2-ca316fe39050 fun updateCount(count: Int) { if (count >
0 && !hasBadge()) { addBadge() } else if (count == 0 && hasBadge()) { removeBadge() } if (count > 99 && !hasFire()) { addFire() setBadgeText("99+") } else if (count <= 99 && hasFire()) { removeFire() } if (count > 0 && !hasPaper()) { addPaper() } else if (count == 0 && hasPaper()) { removePaper() } if (count <= 99) { setBadgeText("$count") } } Imperative UI Showing/hiding Fire -
Shifting paradigms medium.com/androiddevelopers/understanding-jetpack-compose-part-1-of-2-ca316fe39050 fun updateCount(count: Int) { if (count >
0 && !hasBadge()) { addBadge() } else if (count == 0 && hasBadge()) { removeBadge() } if (count > 99 && !hasFire()) { addFire() setBadgeText("99+") } else if (count <= 99 && hasFire()) { removeFire() } if (count > 0 && !hasPaper()) { addPaper() } else if (count == 0 && hasPaper()) { removePaper() } if (count <= 99) { setBadgeText("$count") } } Imperative UI Showing/hiding Fire -
Shifting paradigms medium.com/androiddevelopers/understanding-jetpack-compose-part-1-of-2-ca316fe39050 fun updateCount(count: Int) { if (count >
0 && !hasBadge()) { addBadge() } else if (count == 0 && hasBadge()) { removeBadge() } if (count > 99 && !hasFire()) { addFire() setBadgeText("99+") } else if (count <= 99 && hasFire()) { removeFire() } if (count > 0 && !hasPaper()) { addPaper() } else if (count == 0 && hasPaper()) { removePaper() } if (count <= 99) { setBadgeText("$count") } } Imperative UI Showing/hiding Paper -
Shifting paradigms medium.com/androiddevelopers/understanding-jetpack-compose-part-1-of-2-ca316fe39050 fun updateCount(count: Int) { if (count >
0 && !hasBadge()) { addBadge() } else if (count == 0 && hasBadge()) { removeBadge() } if (count > 99 && !hasFire()) { addFire() setBadgeText("99+") } else if (count <= 99 && hasFire()) { removeFire() } if (count > 0 && !hasPaper()) { addPaper() } else if (count == 0 && hasPaper()) { removePaper() } if (count <= 99) { setBadgeText("$count") } } Imperative UI Showing/hiding Paper -
Shifting paradigms medium.com/androiddevelopers/understanding-jetpack-compose-part-1-of-2-ca316fe39050 fun updateCount(count: Int) { if (count >
0 && !hasBadge()) { addBadge() } else if (count == 0 && hasBadge()) { removeBadge() } if (count > 99 && !hasFire()) { addFire() setBadgeText("99+") } else if (count <= 99 && hasFire()) { removeFire() } if (count > 0 && !hasPaper()) { addPaper() } else if (count == 0 && hasPaper()) { removePaper() } if (count <= 99) { setBadgeText("$count") } } Imperative UI Badge number -
Shifting paradigms medium.com/androiddevelopers/understanding-jetpack-compose-part-1-of-2-ca316fe39050 @Composable fun BadgedEnvelope(count: Int) { Envelope(fire=count >
99, paper=count > 0) { if (count > 0) { Badge(text="$count") } } } Declarative UI -
• compose.animation • Animations library to enrich user experience •
compose.material • Ready to use material design components • compose.foundation • Building blocks • compose.ui • Ui components: layout, drawing, and input • Compose.runtime • Model, state management and core runtime • compose.compiler • Transform @composable functions and enable optimizations Jetpack compose -
• compose.animation • Animations library to enrich user experience •
compose.material • Ready to use material design components • compose.foundation • Building blocks • compose.ui • Ui components: layout, drawing, and input • Compose.runtime • Model, state management and core runtime • compose.compiler • Transform @composable functions and enable optimizations Jetpack compose Compose compiler Compose Runtime Compose UI Toolkit (ANdroid) Compose animation Compose UI Compose Foundation Compose Material -
• compose.animation • Animations library to enrich user experience •
compose.material • Ready to use material design components • compose.foundation • Building blocks • compose.ui • Ui components: layout, drawing, and input • Compose.runtime • Model, state management and core runtime • compose.compiler • Transform @composable functions and enable optimizations Jetpack compose Compose compiler Compose UI Toolkit (ANdroid) Compose animation Compose UI Compose Foundation Compose Material Compose Runtime -
Compose compiler • Written in Kotlin • Transforms @Composable functions
into UI • Doesn’t use the Annotation Processor • The plugin works at system/code generation level • Doesn’t impact build times • Open source • Available in AOSP Compose compiler Compose UI Toolkit (ANdroid) Compose animation Compose UI Compose Foundation Compose Material Compose Runtime android.googlesource.com/platform/frameworks/support/+/HEAD/compose/compiler -
Compose Runtime • Platform agnostic • DOESN’T KNOW WHAT ANDROID
OR UI ARE • TrEE MANAGEMENT SOLUTION Compose compiler Compose Runtime Compose UI Toolkit (ANdroid) Compose animation Compose UI Compose Foundation Compose Material -
Compose COMPILER/Runtime What this means is that Compose is, at
its core, a general-purpose tool for managing a tree of nodes of any type. Well a “tree of nodes” describes just about anything, and as a result Compose can target just about anything. Jake Wharton - a Jetpack Compose by Any other name jakewharton.com/a-jetpack-compose-by-any-other-name/ -
• compose.animation • Animations library to enrich user experience •
compose.material • Ready to use material design components • compose.foundation • Building blocks • compose.ui • Ui components: layout, drawing, and input • Compose.runtime • Model, state management and core runtime • compose.compiler • Transform @composable functions and enable optimizations Jetpack compose Compose compiler Compose Runtime Compose UI Toolkit (ANdroid) Compose animation Compose UI Compose Foundation Compose Material -
Compose UI toolkit • compose.ui • Handles Input Management, Drawing,
Layouts, etc. • Compose.foundation • Contains Basic building: Row, Column, Text, iMage, etc. • Compose.material • Material design system to use on your view components • Compose.animation • Animations to use easily and out side of the box Compose compiler Compose Runtime Compose UI Toolkit (ANdroid) Compose animation Compose Foundation Compose Material Compose UI -
Compose UI Raspberry pi compose Compose compiler Compose Runtime Compose
UI ANdroid Compose UI Desktop Compose UI Web Compose UI Console -
medium.com/androiddevelopers/jetpack-compose-before-and-after-8b43ba0b7d4f Jetpack compose - before and after By Chris Banes
APK Size 1250 2500 3750 5000 Pre-Compose (4.49 MB) Compose (2.39 MB) 4.14 MB 2.32 MB 69 KB 347 KB 4.49MB 2.32 MB -46% -
medium.com/androiddevelopers/jetpack-compose-before-and-after-8b43ba0b7d4f Jetpack compose - before and after By Chris Banes
Method 12500 25000 37500 50000 Pre-Compose (40029) Compose (23689) 40029 23689 40029 methods 23689 methods -17% Count -
medium.com/androiddevelopers/jetpack-compose-before-and-after-8b43ba0b7d4f Jetpack compose - before and after By Chris Banes
Lines of 5000 10000 15000 20000 Pre-Compose (19478) Compose (15407) 15827 14606 19478 Lines of code 15407 lines of code Source Code 3651 801 -
medium.com/androiddevelopers/jetpack-compose-before-and-after-8b43ba0b7d4f Jetpack compose - before and after By Chris Banes
Build 30 60 90 120 Pre-Compose (108,71 s) Compose (76,96 s) 108,71 76,96 108,71 seconds 76,96 seconds -29% Speed -
github.com/google/accompanist Accompanist 📐 Insets 🍫 System UI Controller 🎨 AppCompat
Theme Adapter 🧭✨Navigation-Animation 🧭🎨 Navigation-Material 🖌 Drawable Painter ⬇ Swipe to Refresh 📖 Pager 📫 Permissions ⏳ Placeholder 🌊 Flow Layouts -
github.com/google/accompanist Accompanist 📐 Insets 🍫 System UI Controller 🎨 AppCompat
Theme Adapter 🧭✨Navigation-Animation 🧭🎨 Navigation-Material 🖌 Drawable Painter ⬇ Swipe to Refresh 📖 Pager 📫 Permissions ⏳ Placeholder 🌊 Flow Layouts Keyboard IME animations are now available starting on minSDK 21 (through Jetpack Compose) -
Configuration android { defaultConfig { minSdk 21 … } kotlinOptions
{ jvmTarget = '1.8' useIR = true } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion compose_version kotlinCompilerVersion '1.5.10' } } build.gradle -
Configuration android { defaultConfig { minSdk 21 … } kotlinOptions
{ jvmTarget = '1.8' useIR = true } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion compose_version kotlinCompilerVersion '1.5.10' } } build.gradle -
Configuration android { defaultConfig { minSdk 21 … } kotlinOptions
{ jvmTarget = '1.8' useIR = true } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion '1.0.0' kotlinCompilerVersion '1.5.10' } } build.gradle -
Configuration android { defaultConfig { minSdk 21 … } kotlinOptions
{ jvmTarget = '1.8' useIR = true } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion '1.0.0' kotlinCompilerVersion '1.5.10' } } build.gradle -
Configuration dependencies { implementation "androidx.compose.ui:ui:1.0.0" implementation "androidx.compose.material:material:1.0.0" implementation "androidx.compose.ui:ui-tooling-preview:1.0.0" implementation
"androidx.activity:activity-compose:1.3.1" } build.gradle -
Adding text <TextView android:id="@+id/tv_greetings" android:layout_width="wrap_content" android:layout_height=“wrap_content" android:text="Hello world!" /> XML
Hello Android! Without Compose val greetings = findViewById(R.id.tv_greetings) greetings.text = "Hello Android!” Kt -
Adding text Hello world! With compose @Composable fun Greeting(name: String)
{ Text(text = "Hello $name!") } Kt -
Adding text With compose @Composable fun Greeting(name: String) { Text(text
= "Hello $name!") } Kt -
Adding text With compose @Composable fun Greeting(name: String) { Text(text
= "Hello $name!") } Kt -
Adding text With compose @Composable fun Greeting(name: String) { Text(text
= "Hello $name!") } Kt -
Adding text With compose @Composable fun Greeting(name: String) { Text(text
= "Hello $name!") } Kt -
View Groups Hello world! Without compose <?xml version="1.0" encoding="utf-8"?> <LinearLayout
xmlns:android="http: // schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_greetings" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello world!" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Number of coffees: 0" /> </ LinearLayout> XML Number of co ff ees: 0 -
View Groups Hello world! With compose @Composable fun Greeting(name: String)
{ Text(text = "Hello $name!") } Kt -
View Groups Hello world! With compose @Composable fun Greeting(name: String)
{ Text(text = "Hello $name!”) Text(text = "Number of coffees: 0") } Kt Number of co ff ees: 0 -
View Groups Hello world! With compose @Composable fun Greeting(name: String)
{ Column { Text(text = "Hello $name!") Text(text = "Number of coffees: 0") } } Kt Number of co ff ees: 0 -
View Groups Hello world! With compose @Composable fun Greeting(name: String)
{ Column { Text(text = "Hello $name!") Text(text = "Number of coffees: 0") } } Kt Number of co ff ees: 0 -
View Groups Hello world! With compose @Composable fun Greeting(name: String)
{ Row { Text(text = "Hello $name!") Text(text = "Number of coffees: 0") } } Kt Number of co ff ees: 0 -
Number of co ff ees: 0 View Groups With compose
@Composable fun Greeting(name: String) { Column( modifier = Modifier.background(Color.Green) ){ Text(text = "Hello $name!") Text(text = "Number of coffees: 0") } } Kt Hello world! -
View Groups With compose @Composable fun Greeting(name: String) { Column(
modifier = Modifier.background(Color.Green) ){ Text(text = "Hello $name!") Text(text = "Number of coffees: 0") } } Kt Hello world! Number of co ff ees: 0 -
Hello world! Number of co ff ees: 0 View Groups
With compose @Composable fun Greeting(name: String) { Column( modifier = Modifier.fillMaxSize() .background(Color.Green) ){ Text(text = "Hello $name!") Text(text = "Number of coffees: 0") } } Kt -
Hello world! Number of co ff ees: 0 View Groups
With compose @Composable fun Greeting(name: String) { Column( modifier = Modifier.fillMaxSize() .background(Color.Green) .padding(16.dp) ){ Text(text = "Hello $name!") Text(text = "Number of coffees: 0") } } Kt -
Hello world! Number of co ff ees: 0 View Groups
With compose @Composable fun Greeting(name: String) { Column( modifier = Modifier.fillMaxSize() .background(Color.Green) .padding(16.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center ){ Text(text = "Hello $name!") Text(text = "Number of coffees: 0") } } Kt -
Hello world! View Groups With compose @Composable fun Greeting(name: String)
{ Column( modifier = Modifier.fillMaxSize() .background(Color.Green) .padding(16.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center ){ Text( text = "Hello $name!”, color = Color.White ) Text(text = "Number of coffees: 0") } } Kt Number of co ff ees: 0 -
Hello world! Images Without compose <?xml version="1.0" encoding="utf-8"?> <LinearLayout …
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:contentDescription="@string/app_name" /> </ LinearLayout> XML Number of co ff ees: 0 -
Hello world! Images With compose @Composable fun Greeting(name: String) {
Column(…) { Text( text = "Hello $name!", color = Color.White ) Text(text = "Number of coffees: 0") Image( painter = painterResource(id = R.drawable.ic_launcher), contentDescription = stringResource(id = R.string.app_name) ) } } Kt Number of co ff ees: 0 -
Hello world! Images With compose @Composable fun Greeting(name: String) {
Column(…) { Text( text = "Hello $name!", color = Color.White ) Text(text = "Number of coffees: 0") Image( painter = painterResource(id = R.drawable.ic_launcher), contentDescription = stringResource(id = R.string.app_name) ) } } Kt Number of co ff ees: 0 -
Hello world! Button Without compose <?xml version="1.0" encoding="utf-8"?> <LinearLayout …
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" /> </ LinearLayout> XML Number of co ff ees: 0 + -
Hello world! Button With compose @Composable fun Greeting(name: String) {
Column(…) { … Text(text = "Number of coffees: 0") … Button(onClick = { /* TODO */ }) { Text("+") } } } Kt Number of co ff ees: 0 + -
Hello world! Button With compose @Composable fun Greeting(name: String) {
Column(…) { … Text(text = "Number of coffees: 0") … Button(onClick = { /* TODO */ }) { Text("+") } } } Kt Number of co ff ees: 0 + -
Hello world! State Without compose var count = 0 binding.tvMessage
= getString(R.string.number_coffee, count) binding.btn_increment.setOnClickListener { count ++ } Kt Number of co ff ees: 0 + -
Hello world! State With compose @Composable fun Greeting(name: String) {
Column(…) { … val count = remember { mutableStateOf(0) } Text(text = "Number of coffees taken: ${count.value}") … Button(onClick = { count.value = count.value + 1 }) { Text("+") } } } Kt Number of co ff ees: 0 + -
Hello world! State With compose @Composable fun Greeting(name: String) {
Column(…) { … val count = remember { mutableStateOf(0) } Text(text = "Number of coffees taken: ${count.value}") … Button(onClick = { count.value = count.value + 1 }) { Text("+") } } } Kt Number of co ff ees: 0 + -
Hello world! State With compose @Composable fun Greeting(name: String) {
Column(…) { … val count = remember { mutableStateOf(0) } Text(text = "Number of coffees taken: ${count.value}") … Button(onClick = { count.value = count.value + 1 }) { Text("+") } } } Kt Number of co ff ees: 0 + -
• Jetpack Compose samples (Google) • github.com/android/compose-samples • A curated
list of Jetpack Compose libraries, projects, articles and resources • github.com/jetpack-compose/jetpack-compose-awesomeAnimations library to enrich user experience • Jetpack Compose Catalog • jetpackcompose.app • Jetpack/JetBrains Compose Playground • foso.github.io/Jetpack-Compose-Playground/ • Community • twitch.tv/intelligibabble • joebirch.co/tag/jetpack-compose/ • jorgecastillo.dev/ Resources