Kotlin + Jetpack Compose is the new standard for modern Android UI development. With Compose, XML layouts are gone—you write UIs entirely in Kotlin.
Jetpack Compose Basics
Compose uses @Composable
functions to define UI elements:
@Composable
fun Greeting(name: String) {
Text(text = "Hello $name")
}
Managing State
Compose encourages reactive programming:
var count by remember { mutableStateOf(0) }
Button(onClick = { count++ }) {
Text("Clicked $count times")
}
UI Layouts
Compose uses Row
, Column
, Box
, and other layout primitives.
Column(modifier = Modifier.padding(16.dp)) {
Text("Welcome")
Button(onClick = { /* ... */ }) {
Text("Continue")
}
}
Theming
Themes are managed via MaterialTheme
:
MaterialTheme(
colors = lightColors(primary = Color.Blue),
typography = Typography(),
shapes = Shapes()
) {
// Content here
}
Navigation
Compose Navigation simplifies routing:
NavHost(navController, startDestination = "home") {
composable("home") { HomeScreen() }
composable("details/{id}") { backStack ->
val id = backStack.arguments?.getString("id")
DetailsScreen(id)
}
}
Conclusion
Kotlin and Compose offer a cleaner, more powerful way to build Android UIs. Learn the core building blocks, and you'll be writing fluid, maintainable UIs in no time.