Kotlin · Module 7 · Lesson 11 min read

Android & Jetpack Compose Basics

How an Android app is organised

PartRole
Activityan entry point with a window, e.g. MainActivity
ComposablesUI functions marked @Composable
ViewModelholds screen state; survives screen rotation
Repositorygets data from the network or database
AndroidManifest.xmldeclares the app, its permissions and activities
Gradlebuilds the app and manages libraries

Jetpack Compose: declarative UI

You describe what the UI looks like for the current state; Compose redraws it when the state changes. No XML layouts are needed.

kotlin
@Composable
fun Greeting(name: String) {
    Text("Hello, $name!")
}

Layout building blocks

  • Column { } stacks children vertically, Row { } horizontally, and Box { } layers them.
  • LazyColumn { items(list) { … } } is an efficient scrolling list.
  • Spacer, Card, Button, Image, Icon, TextField, Scaffold (top bar, FAB, content).

Modifiers: size, spacing, look and behaviour

kotlin
Text(
    "Hi",
    modifier = Modifier.padding(16.dp).fillMaxWidth().clickable { … }
)

The order matters: padding before a background differs from padding after it.

Theming

MaterialTheme provides colours, typography and shapes (Material 3), with dynamic colours on Android 12+.

Previews

@Preview @Composable fun GreetingPreview() { Greeting("Android") } shows the UI in Android Studio without running the app.

💻 Compose needs an Android project. In Android Studio, choose New Project → Empty Activity.

Example

kotlin
// MainActivity.kt — an Android Studio "Empty Activity" project (Jetpack Compose)
package com.example.hello

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MaterialTheme { CourseCard(title = "Kotlin", lessons = 16) }
        }
    }
}

@Composable
fun CourseCard(title: String, lessons: Int) {
    Card(modifier = Modifier.padding(16.dp).fillMaxWidth()) {
        Row(
            modifier = Modifier.padding(16.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Text("🟣", fontSize = 32.sp)
            Spacer(Modifier.width(12.dp))
            Column {
                Text(title, fontSize = 20.sp, fontWeight = FontWeight.Bold)
                Text("$lessons lessons", style = MaterialTheme.typography.bodyMedium)
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun CourseCardPreview() {
    MaterialTheme { CourseCard("Kotlin", 16) }
}
// 💻 Run in Android Studio (New Project → Empty Activity)