KtKotlin · Lesson 7 of 8

Your First Android Screen

Modern Android UI is Jetpack Compose: describe the screen as Kotlin functions, and it redraws automatically when state changes. Here's a complete working counter app.

Kotlin
// In Android Studio: New Project -> Empty Activity (Compose)
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            CounterScreen()
        }
    }
}

@Composable
fun CounterScreen() {
    // remember + mutableStateOf = state that survives redraws.
    // When 'count' changes, Compose re-runs this function.
    var count by remember { mutableStateOf(0) }

    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        Text("Count: $count", fontSize = 32.sp)
        Spacer(Modifier.height(16.dp))
        Button(onClick = { count++ }) {
            Text("Tap me")
        }
    }
}

This is declarative UI: you never say 'find the text view and update it' — you describe what the screen looks like for a given state, change the state, and the framework handles the rest. Same mental model as React, in pure Kotlin.

Kotlin
// Lists — the RecyclerView replacement:
@Composable
fun StudentList(students: List<Student>) {
    LazyColumn {
        items(students) { student ->
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp),
                horizontalArrangement = Arrangement.SpaceBetween,
            ) {
                Text(student.name)
                Text("${student.grade}%")
            }
        }
    }
}

// LazyColumn only composes the rows on screen —
// a 10,000-item list scrolls smoothly.
✦ Tip
Run it: Android Studio's device manager gives you an emulator, or plug in your own phone with USB debugging enabled. From here, the official Compose pathway (developer.android.com/courses) is excellent and free.