Featured image of post Jetpack Compose in a Nutshell

Jetpack Compose in a Nutshell


A Brief History (Or, “Why Did Google Do This to Us?”)

Back in the Dark Ages of Android (pre-2020), UI development was a mix of XML files, findViewById(), and frustration. Every change meant navigating a labyrinth of RecyclerView.Adapter, ViewHolders, and onClickListeners. It was painful.

Then, around 2019, Google said, “You know what? Forget XML. Let’s steal an idea from the cool kids over at React and Flutter.” And thus, Jetpack Compose was born—a declarative UI toolkit built entirely in Kotlin.

It’s been gaining steam ever since, and now it’s the recommended way to build Android UIs. XML? Dead. Long live Compose!


Why Jetpack Compose Rocks

1. Bye-Bye XML

No more endless ConstraintLayout struggles. You just write UI directly in Kotlin.

1
2
3
4
@Composable
fun Greeting(name: String) {
    Text(text = "Hello, $name!")
}

That’s it! No need to define a separate XML file and inflate it in an Activity. Just pure Kotlin.

2. State Management is Built-In

Remember how you had to manually update UI elements in XML? Compose takes care of that with State Hoisting.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@Composable
fun Counter() {
    var count by remember { mutableStateOf(0) }
    Column {
        Text(text = "Count: $count")
        Button(onClick = { count++ }) {
            Text("Increment")
        }
    }
}

Click the button, and the UI updates automatically. No more calling notifyDataSetChanged() and praying.

3. Animations Without Tears

Want to animate something? It’s ridiculously easy.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@Composable
fun AnimatedBox() {
    var expanded by remember { mutableStateOf(false) }
    val size by animateDpAsState(if (expanded) 200.dp else 100.dp)
    Box(
        modifier = Modifier
            .size(size)
            .background(Color.Blue)
            .clickable { expanded = !expanded }
    )
}

Tap the box, and it smoothly expands. No XML animators, no ObjectAnimators, just Kotlin magic.

4. Reusability is King

Compose lets you build small, reusable UI components, just like Lego bricks.

1
2
3
4
5
6
7
@Composable
fun UserProfile(name: String, profilePic: Painter) {
    Row(verticalAlignment = Alignment.CenterVertically) {
        Image(painter = profilePic, contentDescription = "Profile Picture")
        Text(text = name, modifier = Modifier.padding(start = 8.dp))
    }
}

Compose is all about keeping things modular. You can mix and match components however you like.

5. Better Performance

Compose only recomposes the parts of the UI that actually change, making it way more efficient than XML-based layouts.


Jetpack Compose in Action

Let’s say you want to build a simple screen with a text field, a button, and a greeting message that updates when you type.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
@Composable
fun HelloScreen() {
    var name by remember { mutableStateOf("") }
    Column(
        modifier = Modifier.padding(16.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        TextField(
            value = name,
            onValueChange = { name = it },
            label = { Text("Enter your name") }
        )
        Spacer(modifier = Modifier.height(8.dp))
        Button(onClick = { /* Do something */ }) {
            Text("Say Hello")
        }
        Spacer(modifier = Modifier.height(8.dp))
        Text("Hello, $name!")
    }
}

Boom. You’ve got an interactive UI with just a few lines of Kotlin.



Key Ideas

Key ConceptSummary
Jetpack ComposeModern Android UI toolkit using Kotlin.
Declarative UINo more XML, just functions in Kotlin.
State ManagementUses remember and mutableStateOf() for reactive UI.
AnimationsSimple animations with animateDpAsState.
ReusabilityBuild small, composable UI components.
PerformanceMore efficient rendering compared to XML.

References