Lesson 3 of 9 · 24m
Adaptive UI, Performance, and Compose Testing
Build interfaces for small phones, tablets, foldables, accessibility users, and imperfect hardware — then prove behavior with tests.
Most Android screens are designed on a large monitor and judged on a flagship phone. Real users arrive on a cracked budget device with 160 percent font scale, one hand, system dark mode, patchy connectivity, and a navigation bar eating the bottom edge. Modern Android UI is adaptive because the ecosystem is hostile, not because tablets are fashionable again.
Design for constraints before pixels
Use material windows and content padding rather than hard-coded dimensions:
Scaffold { padding ->
LazyColumn(
modifier = Modifier.fillMaxSize(),
contentPadding = padding,
) {
items(uiState.orders) { OrderRow(it) }
}
}Modifier.fillMaxWidth().weight() inside panes, lazy layouts for long content, and constraint-aware max widths keep the same screen usable at phone and tablet sizes. A wide screen does not mean "stretch every button to 1200 dp."
Accessibility is behavior, not a compliance checkbox
Touch targets should be at least 48 dp. TalkBack needs meaningful semantics:
IconButton(
onClick = onDelete,
modifier = Modifier.semantics { contentDescription = "Delete order" }
) { Icon(Icons.Default.Delete, contentDescription = null) }Do not duplicate descriptions parent and child. Test dynamic type, color contrast, focus order, and whether state changes are announced. If your custom canvas cannot explain itself to semantics, visually impaired users experience a blank hole.
Recomposition performance has boring causes
Common offenders are expensive work during composition, unstable list keys, huge state objects, and repeated object allocation:
val formatter = remember { DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM) }
Text(order.createdAt.format(formatter))Composition should be fast and side-effect free. Move formatting caches, sorting, filtering, and business transformations out of the innermost item composable when profiles show cost. Baseline Profiles matter for release builds: they tell ART which code paths to precompile, materially improving cold-start and first-scroll behavior.
Test the UI as a state machine
Compose UI tests run semantics, not pixels:
composeRule.setContent {
SearchField(query = "abc", onQueryChange = {})
}
composeRule.onNodeWithText("abc").assertIsDisplayed()Prefer semantics over brittle coordinates. Screenshot tests catch visual regressions, but they do not replace behavioral assertions. A test that clicks a hard-coded pixel will fail on the next font-scale change and teach the team to ignore the suite.
Field notes
- Test small width, large font, dark mode, landscape, and split-screen before calling a screen done.
- Shadows, blur, and large animated gradients are expensive on low-end GPUs; measure on hardware that resembles users.
- Lazy lists are not free: expensive item composition still costs every bind.
- Keep previews honest by injecting fake state; previews that only show happy-path empty data become marketing screenshots, not engineering tools.
Was this lesson helpful?