Jetpack Compose is Android’s modern toolkit for building native UI using a declarative approach. Unlike the traditional XML-based UI, Compose allows you to define your entire interface in Kotlin code, making it concise, reactive, and easier to manage as your app scales.
21 (Android 5.0)The easiest way to start is by using the built-in template in Android Studio:
// MainActivity.kt
package com.example.mycomposeapp
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.Text
import androidx.compose.material3.MaterialTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
Text(text = "Hello Compose!")
}
}
}
}
If you want to add Compose to an existing Android project, follow these steps:
// build.gradle (Module-level)
android {
namespace "com.example.mycomposeapp"
compileSdk 34
defaultConfig {
minSdk 21
targetSdk 34
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.3"
}
}
dependencies {
implementation platform('androidx.compose:compose-bom:2024.10.00')
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.material3:material3'
implementation 'androidx.activity:activity-compose'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx'
implementation 'androidx.compose.ui:ui-tooling-preview'
debugImplementation 'androidx.compose.ui:ui-tooling'
}
Once Gradle syncs successfully, create your first composable:
@Composable
fun Greeting(name: String) {
Text(text = "Hello, $name!")
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
Greeting("Talha")
}
Click the Preview tab or the ▶️ Run Preview button to see your UI instantly rendered inside Android Studio — no emulator needed.
buildFeatures.compose = true is enabled.@Preview import from androidx.compose.ui.tooling.preview.
app/
├── java/com/example/mycomposeapp/
│ ├── MainActivity.kt
│ └── ui/
│ ├── theme/ (Color.kt, Typography.kt, Theme.kt)
│ ├── components/ (Reusable composables)
│ └── screens/ (HomeScreen.kt, DetailsScreen.kt)
└── res/
└── values/ (colors.xml, strings.xml)
Keeping a clear folder structure helps organize composables as your app grows — separating themes, components, and screens.
Now that your project is ready, the next article will explore how Composable functions work — the building blocks of all UI in Jetpack Compose.