On National Teacher Day, meet the 2024-25 Kenan Fellows
Ā
Compose Camp Day 2.pptx
1. This work is licensed under the Apache 2.0 License
SETTING UP
THE TENT
2. Letās start the Camp
This work is licensed under the Apache 2.0 License
3. This work is licensed under the Apache 2.0 License
ā Creating composable activity
ā Row and column sizing
ā Modifiers
ā Image cards
ā Styling text
ā State Management
ā Buttons, Textfields and Snackbars
ā Lists
Overview
4. This work is licensed under the Apache 2.0 License
Creating Composable Activity
Traditionally, each screen (or navigation unit) in
the application would be either an activity or a
fragment, each with its own lifecycle bindings ,
but with Jetpack Compose and the Compose
Navigation library, inbuilt templates are
available.
5. This work is licensed under the Apache 2.0 License
Creating Composable Activity
6. This work is licensed under the Apache 2.0 License
Annotations
in
EMPTY COMPOSABLE
ACTIVITY
7. This work is licensed under the Apache 2.0 License
@Preview
@Composable
This annotation is used to
show preview of a composable
function in the Android Studio
preview.
showBackground(=true)
parameter in the code snippet
in the previous slide will make
@Composable use a default
background.
setContent()
This method is
called and
composable
functions are
passed to create a
compose based
screen.
@Composable
These functions let
you define your appās
UI programmatically
by describing how it
should look and
providing data
dependencies,rather
than focusing on the
process of UIās
construction.
Letās discuss some annotations
9. This work is licensed under the Apache 2.0 License
c
Column is like a
linear layout with
vertical orientation
and shows each
child below the
previous children.
COLUMN LAYOUT
10. This work is licensed under the Apache 2.0 License
c
Without use of Column With use of Column
11. This work is licensed under the Apache 2.0 License
c
ROW LAYOUT
Row is like a linear
layout with a
horizontal orientation
and shows each child
next to the previous
children.
12. This work is licensed under the Apache 2.0 License
Without use of Rows With use of Rows
13. This work is licensed under the Apache 2.0 License
For Column For Row
14. This work is licensed under the Apache 2.0 License
c
15. This work is licensed under the Apache 2.0 License
c
16. This work is licensed under the Apache 2.0 License
c
17. This work is licensed under the Apache 2.0 License
c
19. This work is licensed under the Apache 2.0 License
Modifiers are standard kotlin objects which can be
used to modify certain aspects of a Composable.
To set them , a Composable needs to accept a
modifier as a parameter. We can use common
attribute or appearance like background, padding,
clip, border etc.
What are Modifiers ?
20. This work is licensed under the Apache 2.0 License
Modifiers let you do these sorts of things:
ā Change the composableās size, layout ,behavior and appearance.
ā Add information ,like accessibility labels.
ā Process user input.
ā Add high-level interactions, like making an element clickable,
scrollable, draggable, or zoomable.
21. This work is licensed under the Apache 2.0 License
c
width() requiredWidth()
It abides size modifiers before it or
max parent dimensions.
It doesnāt take layout Constraints
into account and forces it.
22. This work is licensed under the Apache 2.0 License
How it looks in the emulator
:
23. This work is licensed under the Apache 2.0 License
offset()
It can be represented as a point in
cartesian space at a specified
distance from a separately-
maintained origin or as a vector that
can be applied to coordinates.
Spacer()
Spacer component is used to display an
empty space. Width and (or) height can be
set for Spacer using Modifier object. This
function accepts Modifier object as
parameter, through which we may set the
Spacerās width or (and) height.
24. This work is licensed under the Apache 2.0 License
Modify element to add border with
appearance specified with a width, a
color and a shape and clip it. It is
simply annotated with border().
Border
Used to provide extra white space
according to the requirement
around the specific view. It is simply
annotated with padding().
Padding
25. This work is licensed under the Apache 2.0 License
Image card
26. This work is licensed under the Apache 2.0 License
Box
A widget that is used to positioned elements one over
another.it will position its children relative to its edges.
The stack is used to draw the children which will overlap
in the order that they are specified. It is annotated with
Box().
27. This work is licensed under the Apache 2.0 License
28. This work is licensed under the Apache 2.0 License
A card is a container that can
be used as a parent view for
declaring multiple UI elements
inside it. It has an elevation
property that applies a shadow
effect around it. In jetpack
compose, it is equivalent for
Cardview in android.
Card
29. This work is licensed under the Apache 2.0 License
Adding an image element
30. This work is licensed under the Apache 2.0 License
31. This work is licensed under the Apache 2.0 License
Text Styling
32. This work is licensed under the Apache 2.0 License
Text is a central piece of any UI, and Jetpack
Compose makes it easier to display or write text.
Compose leverages composition of its building blocks,
meaning you donāt need to overwrite properties and
methods or extend big classes to have a specific
composable design and logic working the way you
want.
What is text styling ?
33. This work is licensed under the Apache 2.0 License
Sample code:
34. This work is licensed under the Apache 2.0 License
The text
design
changed
35. This work is licensed under the Apache 2.0 License
State
Management
36. This work is licensed under the Apache 2.0 License
What is State Management ?
ā State in an app is any value that can change over time.
ā Any time a state is updated , a recomposition takes place
ā Composable functions can use the ārememberā API to store an object in
memory.
37. This work is licensed under the Apache 2.0 License
Recomposition is when Jetpack Compose re-executes the
composables that may have changed in response to state
changes, and then updates the Composition to reflect any
changes.
What is Recomposition ?
38. This work is licensed under the Apache 2.0 License
ā remember {mutableStateOf()} stores the state
value.
ā ārememberā and āmutableStateOfā are completely
independent concepts.
ā ārememberā keeps a value (any value) consistent across
recompositions. āmutableStateOfā returns a
MutableState.
What is meant by ārememberā keyword ?
39. This work is licensed under the Apache 2.0 License
Understanding the
code snippet
40. This work is licensed under the Apache 2.0 License
On click
On click
On click
41. This work is licensed under the Apache 2.0 License
Buttons, Textfields
and Snackbars
42. This work is licensed under the Apache 2.0 License
Buttons
In Jetpack Compose buttons, you need to give two arguments for buttons. The first
argument as onClick callback and another one is your button text element. You can
add a Text-Composable or any other Composable as child elements of the Button.
43. This work is licensed under the Apache 2.0 License
Textfield without icon Textfield with icon
Textfield
s
TextField is a user interface control that is used to allow the user to enter the text.
This widget is used to get the data from the user as numbers or text.
44. This work is licensed under the Apache 2.0 License
Snackbar is a lightweight widget and they are used to show messages at the
bottom of the application. It was introduced with the Material Design library as
a replacement for a Toast.
An example of snackbar :
Snackbars
45. This work is licensed under the Apache 2.0 License
LISTS
46. This work is licensed under the Apache 2.0 License
It is a common requirement when designing user
interface layouts to present information in either
scrollable list or grid configurations. For basic list
requirements,the Row and Column components can
be re-purposed to provide vertical and horizontal
lists of child composable.
What are Lists ?
47. This work is licensed under the Apache 2.0 License
By using column
48. This work is licensed under the Apache 2.0 License
By using LazyColumn
49. This work is licensed under the Apache 2.0 License
LazyColumn Column
Vs
ā No impact on performance
ā No need to declare
scrollState
ā Favourable to use when
there are more number of
elements
ā Impact on performance
ā Requires explicit declaration
of scrollState
ā Efficient to use when there are
less number of elements
50. This work is licensed under the Apache 2.0 License
THANK YOU
Have fun encouraging your community in becoming
Android Developers!
VOILA HAPPY CAMPING !!!