kotlinintermediate
Jetpack Compose UI Basics
Build declarative UIs with Jetpack Compose: composable functions, state, layouts, and modifiers.
kotlinPress ⌘/Ctrl + Shift + C to copy
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
// Composable function
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello, $name!",
fontSize = 24.sp,
modifier = modifier.padding(16.dp)
)
}
// State management
@Composable
fun Counter() {
var count by remember { mutableIntStateOf(0) }
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.padding(16.dp)
) {
Text("Count: $count", fontSize = 32.sp)
Spacer(modifier = Modifier.height(16.dp))
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
Button(onClick = { count-- }) {
Text("-")
}
Button(onClick = { count++ }) {
Text("+")
}
Button(onClick = { count = 0 }) {
Text("Reset")
}
}
}
}
// List with items
@Composable
fun TodoList() {
var items by remember { mutableStateOf(listOf("Buy groceries", "Walk the dog")) }
var newItem by remember { mutableStateOf("") }
Column(modifier = Modifier.padding(16.dp)) {
Text("Todo List", fontSize = 24.sp)
Spacer(modifier = Modifier.height(8.dp))
Row(verticalAlignment = Alignment.CenterVertically) {
OutlinedTextField(
value = newItem,
onValueChange = { newItem = it },
label = { Text("New item") },
modifier = Modifier.weight(1f)
)
Spacer(modifier = Modifier.width(8.dp))
Button(
onClick = {
if (newItem.isNotBlank()) {
items = items + newItem
newItem = ""
}
}
) {
Text("Add")
}
}
Spacer(modifier = Modifier.height(16.dp))
items.forEachIndexed { index, item ->
Card(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp)
) {
Row(
modifier = Modifier.padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(item, modifier = Modifier.weight(1f))
TextButton(onClick = {
items = items.filterIndexed { i, _ -> i != index }
}) {
Text("Delete")
}
}
}
}
}
}Use Cases
- Building Android UIs with declarative syntax
- State management in Compose
- Interactive lists and forms
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
kotlinbeginner
Null Safety — Elvis, Safe Call, and let
Master Kotlin null safety: safe calls, Elvis operator, let/also scoping, and smart casts.
Best for: Safe navigation through nullable chains
#kotlin#null-safety
kotlinbeginner
Data Classes — Copy, Destructure, and Equals
Use data classes for immutable models: auto-generated equals, hashCode, copy, and destructuring.
Best for: Immutable domain models and DTOs
#kotlin#data-class
kotlinbeginner
Extension Functions and Properties
Add methods to existing classes without inheritance: extension functions, properties, and generic extensions.
Best for: Adding utility methods to third-party types
#kotlin#extensions
kotlinbeginner
Scope Functions — let, run, apply, also, with
Master Kotlin scope functions: when to use let, run, apply, also, and with for concise code.
Best for: Object initialization and configuration
#kotlin#scope-functions