Cheat sheetFree, no sign-upPDF
Android & Kotlin Cheat Sheet: Syntax and Essential Components
A clear cheat sheet covering essential Kotlin syntax, coroutines, and Jetpack Compose for Android development.
Updated on November 24, 2026
Variables and Null Safety
val name: String = "Dakar"Declares an immutable variable. Use val by default to prevent unintended state mutations in your app.var balance: Double = 1500.0Declares a mutable variable. Watch out for race conditions if modified across multiple threads.val city: String? = nullDefines a nullable type using ?. Accessing it directly without a null check triggers a compile error.val len = city?.length ?: 0Elvis operator returns the default value if left side is null. Prevents NullPointerException safely.
Control Flow and Smart Casts
when (status) { "PAID" -> 1; else -> 0 }Replaces classic switch. Must be exhaustive when used as an expression returning a value.if (item is String) item.lengthAutomatic smart cast. Checking type with is automatically casts the variable inside the block.for (i in 1..5) { print(i) }Loops over an inclusive range. Use until if you need to exclude the last boundary index.list.forEach { println(it) }Iterates a collection using implicit argument it. Avoid nesting forEach to prevent scope confusion.
Functions and Classes
fun sendMoney(to: String, amount: Double)Standard function declaration. Parameters are immutable val by default inside the function body.fun add(a: Int, b: Int) = a + bSingle-expression function. Return type is automatically inferred by the Kotlin compiler.data class User(val id: Int, val name: String)Auto-generates equals, hashCode, toString, and copy. Requires at least one primary constructor parameter.class Repo private constructor()Makes primary constructor private. Useful for implementing singletons or factory patterns.
Fundamental Android Components
class MainActivity : ComponentActivity()Main entry point for Android UI. Always declare the activity in AndroidManifest.xml to avoid crashing.Intent(context, DetailActivity::class.java)Explicit intent to start another activity. Avoid passing large complex objects in intent extras.Intent(Intent.ACTION_VIEW, Uri.parse(url))Implicit intent to open an external URL. Check if a compatible app exists before launching it.val prefs = context.getSharedPreferences("app", 0)Simple key-value storage. Do not store sensitive security data without encryption.
Jetpack Compose UI
@Composable fun UserCard(name: String)Defines a reusable UI component. Must be annotated with @Composable and returns no explicit value.var count by remember { mutableStateOf(0) }Preserves local state across recompositions. Omitting remember resets state on every redraw.Column(modifier = Modifier.fillMaxSize().padding(16.dp))Stacks UI elements vertically. Modifiers are applied in the exact order they are chained.LazyColumn { items(users) { user -> Text(user.name) } }High-performance scrolling list for large datasets. Replaces traditional RecyclerView in Compose.
Coroutines and Asynchrony
viewModelScope.launch { fetchData() }Launches a coroutine tied to ViewModel lifecycle. Automatically cancels jobs when ViewModel is cleared.suspend fun fetchUser(): UserMarks a function that can be paused and resumed inside a coroutine without blocking the UI thread.withContext(Dispatchers.IO) { readDb() }Switches execution to thread pool dedicated to disk and network I/O operations.val flow = flow { emit(1) }Asynchronous data stream emitting multiple values sequentially. Only executes when collected.
Other resources
Need help with your website?
See the services I build your custom website.
Showcase site, online shop, management tool: let’s talk about your project.