Slide 1

Slide 1 text

By Karan Dhillon JETPACK COMPOSE FOR BEGINNERS

Slide 2

Slide 2 text

Jetpack compose is Android’s modern toolkit for building native UI. Simpli f ies and accelerates UI development on Android by writing less code. As app state changes, your UI automatically changes. Written in Kotlin. WHAT IS JETPACK COMPOSE?

Slide 3

Slide 3 text

Building block of jetpack compose. Just like a regular function, but annotated with @Composable. This enables your function to call other @Composable functions within it. Able to make generic containers, that can take another @Composable functions as parameters. COMPOSABLE

Slide 4

Slide 4 text

COMPOSABLE @Composabl e fun Greeting(name: String) { Text(text = "Hello $name!" ) }

Slide 5

Slide 5 text

COMPOSABLE @Composabl e fun Greeting(name: String) { Text(text = "Hello $name!" ) } class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState ) setContent { AndDev360Theme { Surface(color = MaterialTheme.colors.background) { Greeting("360|AnDev" ) } } } } } Instead of passing XML as you’d do in traditional View system, you call Composable functions within it.

Slide 6

Slide 6 text

SURFACE @Composabl e fun Greeting(name: String) { Surface(color = Color.Yellow) { Text(text = "Hello $name!" ) } }

Slide 7

Slide 7 text

MODIFIERS @Composabl e fun Greeting(name: String) { Surface(color = Color.Yellow) { Text ( text = "Hello $name!" , modi fi er = Modi fi er.padding(24.dp ) ) } }

Slide 8

Slide 8 text

MODIFIERS Some common Modi f iers are: clickable Padding Background Alpha f illMaxSize onKeyEvent Scale rotate Chaining Modi f iers.

Slide 9

Slide 9 text

MODIFIERS @Composabl e fun Greeting(name: String) { Surface(color = Color.Yellow) { Text ( text = "Hello $name!" , modi fi er = Modi fi e r .padding(24.dp ) .background(color = Color.Yellow, shape = RectangleShape ) .clickable { Toast.makeText ( this , "Click event" , Toast.LENGTH_SHORT ).show( ) } . fi llMaxSize( ) ) } }

Slide 10

Slide 10 text

MODIFIERS @Composabl e fun Greeting(name: String) { Surface(color = Color.Yellow) { Text ( text = "Hello $name!" , modi fi er = Modi fi e r .padding(24.dp ) .background(color = Color.Yellow, shape = RectangleShape ) .clickable { Toast.makeText ( this , "Click event" , Toast.LENGTH_SHORT ).show( ) } . fi llMaxSize( ) ) } }

Slide 11

Slide 11 text

MODIFIERS @Composabl e fun Greeting(name: String) { Surface(color = Color.Yellow) { Text ( text = "Hello $name!" , modi fi er = Modi fi e r .padding(24.dp ) .background(color = Color.Yellow, shape = RectangleShape ) .clickable { Toast.makeText ( this , "Click event" , Toast.LENGTH_SHORT ).show( ) } . fi llMaxSize( ) ) } }

Slide 12

Slide 12 text

MODIFIERS @Composabl e fun Greeting(name: String) { Surface(color = Color.Yellow) { Text ( text = "Hello $name!" , modi fi er = Modi fi e r .padding(24.dp ) .background(color = Color.Yellow, shape = RectangleShape ) .clickable { Toast.makeText ( this , "Click event" , Toast.LENGTH_SHORT ).show( ) } . fi llMaxSize( ) ) } }

Slide 13

Slide 13 text

COLUMN @Composabl e fun Greeting() { Surface(color = Color.Yellow) { Column { Text(text = "Android" ) Divider(color = Color.Black ) Text(text = "loves compose" ) } } }

Slide 14

Slide 14 text

ROW @Composabl e fun Greeting() { Surface(color = Color.Yellow) { Row(modi fi er = Modi fi er. fi llMaxWidth()) { Text ( text = "Android" , modi fi er = Modi fi er.background(color = Color.Red ) ) Text(text = "loves compose" ) } } }

Slide 15

Slide 15 text

LAZYCOLUMN(LAZYROW) class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState ) setContent { AndDev360Theme { NameList ( listOf ( "Hello Android 1" , "Hello Android 2” , … ) ) } } } @Composabl e fun NameList(dataSet: List) { LazyColumn { items(dataSet) { item - > Greeting(name = item ) } } } }

Slide 16

Slide 16 text

LAZYCOLUMN(LAZYROW) class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState ) setContent { AndDev360Theme { NameList ( listOf ( "Hello Android 1" , "Hello Android 2” , … ) ) } } } @Composabl e fun NameList(dataSet: List) { LazyColumn { items(dataSet) { item - > Greeting(name = item ) } } } }

Slide 17

Slide 17 text

LAZYCOLUMN(LAZYROW) class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState ) setContent { AndDev360Theme { NameList ( listOf ( "Hello Android 1" , "Hello Android 2” , … ) ) } } } @Composabl e fun NameList(dataSet: List) { LazyColumn { items(dataSet) { item - > Greeting(name = item ) } } } }

Slide 18

Slide 18 text

STATE @Composabl e fun Counter() { val count = remember { mutableStateOf(0) } Button(onClick = { count.value++ }) { Text("I've been clicked ${count.value} times" ) } }

Slide 19

Slide 19 text

STATE HOISTING @Composabl e fun Counter(count: Int, updateCount: (Int) -> Unit) { Button(onClick = { updateCount(count+1) }) { Text("I've been clicked $count times" ) } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState ) setContent { AndDev360Theme { Surface(color = MaterialTheme.colors.background) { val counterState = remember { mutableStateOf(0) } Counter ( count = counterState.value , updateCount = { newCount - > counterState.value = newCoun t } ) } } } }

Slide 20

Slide 20 text

STATE HOISTING @Composabl e fun Counter(count: Int, updateCount: (Int) -> Unit) { Button(onClick = { updateCount(count+1) }) { Text("I've been clicked $count times" ) } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState ) setContent { AndDev360Theme { Surface(color = MaterialTheme.colors.background) { val counterState = remember { mutableStateOf(0) } Counter ( count = counterState.value , updateCount = { newCount - > counterState.value = newCoun t } ) } } } }

Slide 21

Slide 21 text

ANIMATION override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState ) setContent { AndDev360Theme { NameList(listOf ( "Hello Android 1" , "Hello Android 2” , … ) ) } } } @Composabl e fun Greeting(name: String) { var isSelected by remember { mutableStateOf(false) } val backgroundColor by animateColorAsState(if (isSelected) Color.Yellow else Color.Transparent ) Text ( text = "Hello $name!" , modi fi er = Modi fi e r .padding(24.dp ) .background(color = backgroundColor ) .clickable(onClick = { isSelected = !isSelected } ) ) }

Slide 22

Slide 22 text

ANIMATION If animating appearance and disappearance, use AnimationVisibility. If crossfading, use Crossfade. If you want to animate multiple values, then use updateTransition, otherwise use animate*AsState.

Slide 23

Slide 23 text

THEMING @Composabl e fun AndDev360Theme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable() () -> Unit) { val colors = if (darkTheme) { DarkColorPalett e } else { LightColorPalett e } MaterialTheme ( colors = colors , typography = Typography , shapes = Shapes , content = conten t ) }

Slide 24

Slide 24 text

THEMING override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState ) setContent { AndDev360Theme { Greeting(name = "360|AnDev" ) } } } @Composabl e fun Greeting(name: String) { Text ( text = "Hello $name!" , modi fi er = Modi fi er.padding(24.dp) , style = MaterialTheme.typography.h 1 ) }

Slide 25

Slide 25 text

ComposeView - To use jetpack compose in traditional UI system. AndroidView - To use traditional UI system in jetpack compose. INTEROPERABILITY

Slide 26

Slide 26 text

COMPOSEVIEW class ExampleFragment : Fragment() { override fun onCreateView ( in fl ater: LayoutIn fl ater , container: ViewGroup? , savedInstanceState: Bundle ? ): View { // In fl ate the layout for this fragmen t return in fl ater.in fl ate ( R.layout.fragment_example, container, fals e ).apply { fi ndViewById(R.id.compose_view).setContent { // In Compose worl d MaterialTheme { Text("Hello Compose!" ) } } } } }

Slide 27

Slide 27 text

COMPOSEVIEW class ExampleFragment : Fragment() { override fun onCreateView ( in fl ater: LayoutIn fl ater , container: ViewGroup? , savedInstanceState: Bundle ? ): View { // In fl ate the layout for this fragmen t return in fl ater.in fl ate ( R.layout.fragment_example, container, fals e ).apply { fi ndViewById(R.id.compose_view).setContent { // In Compose worl d MaterialTheme { Text("Hello Compose!" ) } } } } }

Slide 28

Slide 28 text

ANDROIDVIEW @Composabl e fun CustomView() { val selectedItem = remember { mutableStateOf(0) } // Adds view to Compose AndroidView ( modi fi er = Modi fi er. fi llMaxSize(), // Occupy the max size in the Compose UI tree factory = { context - > // Creates custom view CustomView(context).apply { // Sets up listeners for View -> Compose communication myView.setOnClickListener { selectedItem.value = 1 } } } , update = { view - > // View's been in fl ated or state read in this block has been updated // Add logic here if necessary } ) }

Slide 29

Slide 29 text

Jetpack compose docs (https://developer.android.com/jetpack/compose) API guidelines for Jetpack compose (https://github.com/androidx/androidx/blob/ androidx-main/compose/docs/compose-api-guidelines.md) Adopting Jetpack compose in your app(https://developer.android.com/jetpack/ compose/interop) RESOURCES

Slide 30

Slide 30 text

THE END