User interfaces with Compose
Building Declarative Android UIs using Jetpack Compose and Kotlin
Updated: 03 September 2023
Jetpack Compose
Based on the Jetpack Compose Learning Path
Composable Functions
Using a Composable
Compose makes use of composable functions that let us define our UI programmatically
Composable functions make use of the@Composable
annotation
We can render a composable Text
element like so:
Compose uses the Kotlin compiler to turn these functions into UI elements
The Text
element above will therefore render some text to the screen
Defining a Composable
We can define a composable function like so:
Composable Previews
Additionally, we can create a preview of a component using the @Preview
annotation before the @Composable
So to preview our component above we can use the following:
Additionally, we can include a background to our preview using the showBackground = true
on the Preview
annotation as can be seen below
We can make use of Android Studio’s Design Preview to view Preview components
Composable Data
We can also make our component take more complex data, e.g. using a Data class like so:
Builtin Composables
As we’ve seen, there’s the Text
composable component that’s defined by compose, in addition, we have a few other useful components that come pre-defined for us
First, there’s the Column
component
And the Row
There’s also the Image
And the Spacer
Modifiers
Modifiers allow us to change the styles of a specific component.
For example, we can add a modifier to a Column
like so:
We can further update the above example by adding some modifiers to the text:
And, modifiers can also be chained to create more complex styles:
Text Styles
In addition to the above modifiers, the Text
composable also takes a few additional properties that we can use to modify the style of the text. Here’s a simple way that we can add some styles to the Text
element
We can also define our app’s text styles in a central location and reuse it from there, or we can use the MaterialTheme
ones as such:
Applying this to our composable above:
Lists
List views can be done using the LazyRow
and LazyRow
composable. These composables have an items
lambda which can be used to render a given item
Note that we can use the sample data from
androidx.compose.foundation.lazy.items
State Management
Composable functions can store local state by using remember
, and can track changes to the value passed to mutableStateOf
. State will then be redrawn automatically when the value is updated
Using this method of state management we use the by
keyword. This keyword delegates the getter and setter value for a specific variable to a function (in this case, remember
)
Using the above, we can create an ExpandableGreeting
which manages the state required by our FancyGreeting
And we can also update our FancyGreeting
to take some expand-related options
Animations
Compose provides us with a modifier for handling animation that we can apply to the Text
above, you can see this below:
This will make it automatically animate when expanded