For over a decade, Android user interfaces were built using an imperative XML View hierarchy. Developers defined layouts in XML files, inflated them in Activities, retrieved element references using findViewById(), and manually mutated view properties when data changed. In 2021, Google revolutionized mobile development by releasing Jetpack Compose — Android's modern, declarative UI toolkit written entirely in Kotlin.
Figure 8.1: Jetpack Compose Unidirectional Data Flow (UDF) — State Flows Down into Composables, User Action Events Flow Up to the ViewModel.
To understand the profound shift between legacy Views and Jetpack Compose, compare how both models handle a simple button click that updates a text label:
You tell the framework how to mutate existing views step-by-step:
// 1. Inflate XML layout
setContentView(R.layout.activity_main)
// 2. Find view references
val myTextView = findViewById<TextView>(R.id.txtCount)
val myButton = findViewById<Button>(R.id.btnClick)
// 3. Manually mutate widget state
var count = 0
myButton.setOnClickListener {
count++
myTextView.text = "Count: $count"
myTextView.visibility = View.VISIBLE
}
Weakness: View state and data state easily get out of sync, causing subtle UI bugs.
You declare what the UI should look like for a given state:
@Composable
fun CounterScreen() {
var count by remember { mutableStateOf(0) }
Column(modifier = Modifier.padding(16.dp)) {
Text("Count: $count", style = MaterialTheme.typography.headlineMedium)
Button(onClick = { count++ }) {
Text("Increment")
}
}
}
Strength: Recomposition automatically regenerates the UI whenever count changes.
| Dimension | Legacy XML Views (View System) | Jetpack Compose (Modern Toolkit) |
|---|---|---|
| Language | Dual: XML for layout + Java/Kotlin for logic | 100% Kotlin (UI and logic in the same language) |
| State Synchronization | Manual mutations (e.g., setText(), setVisibility()) |
Automatic Recomposition driven by immutable state |
| View Hierarchy Cost | Heavy class inheritance (View class is 30,000+ lines of code) |
Lightweight Composable Kotlin functions that don't inherit |
| Custom Drawing & Styling | Requires complex custom attributes, custom View subclasses, and onDraw | Trivial: Modifiers, Canvas {}, and declarative theming |
| List Performance | RecyclerView + ViewHolder pattern + Adapter boilerplate | LazyColumn / LazyRow with clean, inline item lambdas |
| Theme System | Legacy XML Styles (styles.xml, themes.xml) |
Material Design 3 composable theme providers |
Yes! You do not need to rewrite an entire existing application overnight. You can embed modern Composables inside an existing XML layout using ComposeView, and you can embed legacy XML Views (or third-party map/graph views) inside Compose using the AndroidView {} wrapper composable.
Broaden your foundational and advanced Android application development skills across our curriculum:
Step-by-step introduction to Composables, Modifiers, Row, and Column.
Master state hoisting, remember, and mutableStateOf in declarative UI.
Apply Material 3 color schemes, typography, and dynamic theming.
How ComponentActivity.setContent hosts your top-level Composable.