Skip to main content

Command Palette

Search for a command to run...

Exploring How Composables and Recompositions Work : Zero to One ( 02 )

Updated
•2 min read•View as Markdown
Exploring How Composables and Recompositions Work : Zero to One ( 02 )
S

Native Android Developer and content creator

Composables

In Jetpack Compose, a Composable is a fundamental building block used to define UI components in a declarative way. It’s a special type of function that can be annotated with the @Composable annotation. Composables describe how the UI should look based on the current state of the app, and they can be combined to build more complex UI structures.

For example, a simple Composable function might look like this:

@Composable
fun Greeting(name: String) {
    Text(text = "Hello, $name!") // Text is a prerdefined composable 
}

Preview

The @Preview annotation in Jetpack Compose is used to show a preview of a Composable function in Android Studio's design editor. It allows you to see how a UI component will look without needing to run the app on an emulator or device. This helps speed up UI development by providing an instant visual feedback loop.

Remember : To see the preview the app needs to build at least once

@Preview // this will show only the composable preview
@Composable
fun Greeting(name: String) {
    Text(text = "Hello, $name!") // Text is a prerdefined composable 
}

if you want to see the preview along with the background you need to write it like this:

@Preview(showBackground = true)
@Composable
fun Greeting(name: String) {
    Text(text = "Hello, $name!") // Text is a prerdefined composable 
}
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun Greeting(name: String) {
    Text(text = "Hello, $name!") // Text is a prerdefined composable 
}

Recomposition

Recomposition in Jetpack Compose means updating parts of the screen when something changes. For example, if you have a button that shows a number, and the number changes, Jetpack Compose will automatically update that number on the screen without you having to do anything.

Think of it like this: when the data or state (like a number, text, or color) changes, Jetpack Compose will "repaint" the parts of the screen that need to reflect those changes, making sure everything looks up to date. It only updates what’s necessary, so it’s fast and efficient.

Mastering Jetpack Compose : Zero to One

Part 2 of 2

Mastering Jetpack Compose: Zero to One is your go-to resource for mastering Android's cutting-edge UI toolkit. Designed for beginners and aspiring developers, this series takes you from zero knowledge to a strong foundational understanding.

Start from the beginning

Introduction to Jetpack Compose : Zero to One ( 01 )

Okay, I'm not going to waste any time defining what Compose is, as I think everyone is familiar with it. The question is, why this blog instead of Google's official documentation? As a beginner, I found Google's docs very difficult to understand. The...