Jetpack Compose is the modern, recommended way to build Android UI. Instead of writing XML layouts and updating views manually, you describe your UI as Kotlin functions that automatically re-draw when your data changes.
Declarative vs the old XML way
Old way (imperative): create a TextView in XML, then in code call textView.text = "Hi" whenever it changes. Compose way (declarative): you write Text(message) and when message changes, Compose redraws it for you. Less code, fewer bugs.
Your first composable
@Composable
fun Greeting(name: String) {
Text(text = "Hello, $name!")
}
// Host it in an Activity
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme { Greeting("Android") }
}
}
}
- A composable is a function marked
@Composablethat emits UI. setContent { }replacessetContentView(R.layout...).- No XML, no findViewById, no view binding.
Tip: Enable Compose in Gradle (
buildFeatures { compose = true }) and add the Compose BOM dependency. New Android Studio templates do this for you.Summary
Compose is declarative UI in pure Kotlin. You describe the UI for your data and Compose keeps the screen in sync. You wrote your first composable and hosted it with setContent.