Kotlin Compose Basics
Jetpack Compose is the modern way to build Android screens. Older apps describe screens in XML files and connect them to Kotlin code. Compose uses Kotlin alone. You write functions that describe what the screen should show, and Compose draws it.
The Recipe Idea
A recipe lists ingredients and steps. The cook follows the recipe to produce a dish. When an ingredient changes, the cook adjusts only the affected step. A Compose function acts like a recipe for a piece of screen. When the data changes, Compose redraws only the parts that depend on that data.
Composable Functions
The label @Composable turns a function into a screen piece. Names start with a capital letter, because each function represents a visible element.
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
@Composable
fun Greeting(name: String) {
Text(text = "Hello, $name")
}Showing a Composable
The function setContent inside an Activity hosts the Compose tree.
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Greeting("Asha")
}
}
}Layout Building Blocks
Three containers arrange the elements:
Columnstacks children from top to bottom.Rowplaces children from left to right.Boxlayers children on top of each other.
Column Row Box +--------+ +----+----+----+ +-----------+ | Item 1 | | A | B | C | | +-----+ | +--------+ +----+----+----+ | |Top | | | Item 2 | | +-----+ | +--------+ | Bottom | | Item 3 | +-----------+ +--------+
@Composable
fun ProfileCard() {
Row(modifier = Modifier.padding(16.dp)) {
Text("Asha")
Spacer(modifier = Modifier.width(8.dp))
Text("Android Developer")
}
}Modifiers
A modifier adjusts size, spacing, color, and behavior. Chain modifiers with dots. The order matters, because each step applies to the result of the previous one.
Text(
text = "Welcome",
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
)Buttons and Events
Button(onClick = { println("Tapped") }) {
Text("Tap me")
}The onClick parameter receives a function. Compose calls it each time the user taps.
State Makes the Screen Alive
State is a value that can change over time. The function mutableIntStateOf creates a state holder, and remember keeps it alive between redraws. Compose watches the holder. Each change triggers a redraw of the parts that read it.
import androidx.compose.runtime.*
@Composable
fun CounterScreen() {
var count by remember { mutableIntStateOf(0) }
Column(modifier = Modifier.padding(16.dp)) {
Text("Taps: $count")
Button(onClick = { count++ }) {
Text("Tap me")
}
}
}The by keyword needs the imports getValue and setValue, which the wildcard import above covers.
The State Loop
+-------+ draws +--------+
| State | --------------> | UI |
+-------+ +--------+
^ |
| user taps |
+------ event updates -----+
The state flows down into the screen. Events flow up and change the state. The cycle repeats on every tap.
Lists on Screen
A LazyColumn draws only the rows visible on screen, which saves memory.
@Composable
fun NameList(names: List<String>) {
LazyColumn {
items(names) { name ->
Text(text = name, modifier = Modifier.padding(8.dp))
}
}
}Previewing Without a Phone
The label @Preview shows a composable inside Android Studio, without running the app.
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
Greeting("Preview")
}Good Habits
- Keep each composable small and focused on one job.
- Pass data down as parameters and send events up as functions.
- Avoid heavy work inside a composable. Redraws happen often.
- Name composables with nouns, such as
ProfileCard.
