NibQuest is a math and reading app for kids from Pre-K to 7th grade. Every screen is written once in Compose Multiplatform and runs on Android phones, Android tablets, iPhones and iPads. I wrote about how the shared code is split in an earlier piece. This one is only about Compose: how the tablet layouts in version 1.0.4 were added on top of a UI that phones already used every day.
The rule
Phones were fine. Parents had been using the app on them for weeks, the screenshots in both stores showed phone layouts, and every bug I had fixed was a phone bug. Tablets, on the other hand, showed the phone layout stretched out: a puzzle the size of a postcard in the middle of a 13-inch iPad, with a lot of empty cream around it.
So the rule for this release was simple to say. Tablets get layouts designed for them. Phones, in either orientation, render exactly what they rendered before. If a change touched a phone, it was a bug, even if it looked better.
That rule decided almost everything below.
The width class answers a different question
The app already had a window class, the usual three buckets based on width:
enum class WindowClass { COMPACT, MEDIUM, EXPANDED }
val window = when {
w >= 840.dp -> WindowClass.EXPANDED
w >= 600.dp -> WindowClass.MEDIUM
else -> WindowClass.COMPACT
}
The obvious move was to treat EXPANDED as "tablet". It fails straight away. A large phone held sideways is around 930 to 960 dp wide and about 430 to 440 dp tall. That's past 840, so it's EXPANDED, and it would have picked up every tablet layout: the navigation rail, the tablet home screen, the three-pane lesson. Those were designed for a screen with room above and below. A phone in landscape has about 430 dp of height to work with.
EXPANDED. The phone picks up the rail and the tablet home screen, which is exactly the kind of change the rule forbids.Worse, the app already had a branch for wide landscape phones, and phones depended on it looking the way it did. Changing what "wide" meant would have changed those phones.
The width class is answering "how much horizontal room is there right now?" That's a useful question, and the app keeps asking it. "Is this device a tablet?" is a separate question, and it needs its own answer.
The answer I settled on is the window's shortest side. No phone in either orientation has a shortest side of 600 dp or more. Every iPad and every Android tablet I tested does. Rotation doesn't change it. That gives a flag that is stable for the device and never true on a phone.
One Layout value for the whole tree
Everything a screen needs to know about its size lives in one data class, provided once near the root:
data class Layout(
val window: WindowClass,
val width: Dp,
val height: Dp,
val whiteboard: Boolean,
val tablet: Boolean = false,
) {
val landscape: Boolean get() = width > height
val wide: Boolean get() = window == WindowClass.EXPANDED || whiteboard
val short: Boolean get() = height < 520.dp
val target: Dp get() = when {
whiteboard -> 96.dp
tablet -> 72.dp
window == WindowClass.COMPACT -> 60.dp
else -> 68.dp
}
val gutter: Dp get() = when {
tablet -> 28.dp
window == WindowClass.COMPACT -> 16.dp
else -> 24.dp
}
}
val LocalLayout = staticCompositionLocalOf {
Layout(WindowClass.COMPACT, 400.dp, 800.dp, false)
}
(whiteboard is a classroom mode for big touch screens. It predates the tablet work and comes up again below.)
The theme measures the window and provides it:
BoxWithConstraints(Modifier.fillMaxSize()) {
val w = maxWidth
val h = maxHeight
val window = /* the when above */
CompositionLocalProvider(
LocalLayout provides Layout(window, w, h, whiteboard, tablet = minOf(w, h) >= 600.dp),
// ...
) {
MaterialTheme(colorScheme = colors, typography = type, content = content)
}
}
A few details turned out to matter.
Measure the full window, then keep the flag. Further down, the app shell takes away room for the navigation rail and the safe area, and gives screens the size they actually get:
val layout = LocalLayout.current.copy(width = maxWidth, height = maxHeight)
CompositionLocalProvider(LocalLayout provides layout) { /* screen */ }
copy updates width and height but carries tablet over from the full window. If the flag were recomputed from the content area, an iPad in portrait with a rail down one side could drop under 600 dp of width and flip back to phone layouts on some screens but not others. Deciding once at the root avoids that.
staticCompositionLocalOf, on purpose. The value changes on rotation and window resize, and nowhere else. When it does change, almost everything on screen depends on it anyway, so the cheaper reads of a static local are worth the full recomposition.
Split view comes out right without extra code. When an iPad runs the app in Split View or Slide Over at phone width, the window's shortest side drops below 600 dp and the app gets its phone layout, which is exactly what that window needs. I didn't write anything for this. It falls out of measuring the window rather than asking what the device is.
Size tokens come from the same place. Touch targets go 60, 68, 72 and 96 dp from compact phones up to the whiteboard, and gutters follow the same pattern. Because tablet is its own branch in target and gutter, the phone values didn't move.
Scaling every puzzle with one LocalDensity
This was the part I expected to take weeks.
The puzzles are the heart of the app: counting pictures, dragging tiles onto a number line, building words from letter cards, shading fractions, and many more. Every one of them was designed for a card of about 520 by 480 dp. On a 13-inch iPad in portrait, the card is close to 1,000 dp each way, and the puzzle sat in the middle of it at phone size.
The app already had one way to make things bigger. Whiteboard mode sets roomy, and puzzles that check roomy use larger tiles, keys and cards. But that meant a size decision inside each puzzle, and not every puzzle had one. Doing the same for tablets would have meant touching every puzzle and then checking every one of them on phones again.
Instead, tablets change the density for the puzzle card and nothing else:
BoxWithConstraints(cardModifier) {
// Puzzles are designed for about 520 × 480 dp. The zoom stops at 1.4× so the
// puzzle stays in proportion with the prompt beside it, and the card still
// scrolls if a tall puzzle needs more room.
val zoom = if (layout.tablet) {
minOf(maxWidth / 520.dp, maxHeight / 480.dp).coerceIn(1f, 1.4f)
} else 1f
val viewport = maxHeight / zoom
val density = LocalDensity.current
CompositionLocalProvider(
LocalDensity provides Density(density.density * zoom, density.fontScale)
) {
Column(
Modifier
.fillMaxWidth()
.verticalScroll(rememberScrollState())
.heightIn(min = viewport)
.padding(12.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(14.dp, Alignment.CenterVertically),
) {
VisualView(item.visual, /* ... */)
TaskView(item, /* ... */)
}
}
}
Inside that provider, 1 dp is worth more pixels. Every dp and sp in every puzzle grows by the same factor: pictures, tiles, padding, corner radii, stroke widths, drag targets and text. The puzzle doesn't know it's on a tablet. It lays itself out at its usual size in a space that's been scaled up, and comes out larger with every proportion kept.
On phones zoom is exactly 1f, so the new Density has the same values as the old one and nothing changes.
Three things to know if you try this.
The constraints were measured before the zoom. maxHeight comes from BoxWithConstraints, outside the provider, in the old density. Inside the provider, the same number of pixels is fewer dp. If you pass maxHeight into the zoomed content as is, the content thinks it has 1.4 times more room than it does, and the bottom of a tall puzzle gets cut off. Dividing by the zoom (viewport = maxHeight / zoom) gives the height in the zoomed space. I use it as a minimum height, so short puzzles sit centered and tall ones scroll.
Keep fontScale as it was. Passing density.fontScale through means text grows with the zoom, since sp converts through density too, and the user's own text-size setting still applies on top. Scaling only density keeps text and pictures in the same proportion they had on a phone.
Cap it. Without a ceiling, the zoom on a 13-inch iPad in portrait would come out near 1.9×. (In landscape the side panels take enough width that it only reaches about 1.3×, so the cap matters most in portrait.) At that size the puzzle looks huge next to the prompt and buttons around it, which aren't zoomed, and the screen stops looking like one design. I tried a few values on real devices. 1.4× was the point where the puzzle filled the card without overpowering the rest of the screen. The lower bound of 1 means a small tablet never shrinks a puzzle below its phone size.
The zoom wraps the puzzle card and nothing else. The prompt, the helpers and the progress track around it are laid out at normal density by the tablet branches in the next section. Zooming the whole screen would have been one line, but every panel would have grown together, and the tablet would have looked like a big phone again.
Keep the tablet branches few
With sizes coming from Layout and puzzles scaled by density, the tablet-only code that's left is mostly arrangement: putting things side by side that a phone has to stack. Here's all of it. A search for layout.tablet finds a dozen lines in five files.
- Navigation. Tablets get a rail on the leading edge instead of the bottom bar, and the window insets change to match.
- Home. A separate
TabletHomecomposable, because the home screen on a tablet has room for things a phone shows one at a time. - Lesson, landscape. Three panes: prompt and host on the left, the zoomed puzzle in the middle, progress, helpers and feedback on the right.
- Lesson, portrait. The puzzle fills the middle. Feedback docks under it, and the Hint and Hear it buttons sit along the bottom edge, where the thumbs of a child holding the tablet already are.
- Answer choices. On the wide tablet card, a short last row is centered instead of left-aligned.
- Summary. "Today you explored" and "Next time" sit side by side on tablets and stay stacked on phones.
- Parent area. The tab buttons fit on one row instead of two.
The lesson screen shows the pattern. The tablet cases come first and the phone cases that existed before follow them, untouched:
if (layout.tablet && layout.landscape) {
Row(Modifier.fillMaxSize().padding(layout.gutter),
horizontalArrangement = Arrangement.spacedBy(layout.gutter)) {
Column(Modifier.weight(0.24f).fillMaxHeight()) { /* exit, host, prompt */ }
activity(Modifier.weight(0.54f).fillMaxHeight())
Column(Modifier.weight(0.22f).fillMaxHeight()) { /* track, helpers, feedback */ }
}
} else if (layout.tablet) {
Column(/* ... */) { /* top bar, track, prompt, activity, feedback, helper buttons */ }
} else if (layout.wide && layout.landscape) {
// The existing phone-landscape layout, as it was before 1.0.4.
} else {
// The existing phone-portrait layout.
}
Because tablet is checked before wide, a big phone in landscape still lands on its own branch, the one it has always used. Putting new cases in front of old ones, and leaving the old ones alone, is most of what kept phones unchanged.
The summary pairing is the smallest example of the "side by side or stacked" idea, and it uses IntrinsicSize.Min so the two panels end up the same height:
val paired = tablet && r.practiced.isNotEmpty() && r.next != null
if (paired) {
Row(Modifier.fillMaxWidth().height(IntrinsicSize.Min),
horizontalArrangement = Arrangement.spacedBy(14.dp)) {
explored(Modifier.weight(1f).fillMaxHeight())
nextTime(Modifier.weight(1f).fillMaxHeight(), r.next!!.kidReason)
}
} else if (r.practiced.isNotEmpty()) {
explored(Modifier.fillMaxWidth())
}
// ...
if (!paired) r.next?.let { nextTime(Modifier.fillMaxWidth(), it.kidReason) }
The panels are declared once as composable lambdas that take a Modifier, and the branch decides where they go. Neither layout has its own copy of the panel.
One thing didn't go through this code at all. On Android, the plans screen is RevenueCat's hosted paywall, so its tablet layout is set in their dashboard, not in Compose. On iOS the plans screen is my own Compose code, and it gets two columns on a tablet in landscape. If you use a hosted paywall, check what it looks like on a tablet before you assume your layout code covers it.
Proving the phones didn't change
Compose previews are good for building a tablet layout. They're weak at proving a phone didn't move by a few dp, because you have to remember to look at every phone screen in every state.
What worked better was turning a real Android tablet into a phone:
adb shell wm size 1080x2400
adb shell wm density 420
# run through the app, capture each screen
adb shell wm size reset
adb shell wm density reset
At 1080 by 2400 pixels and density 420, the window is about 411 by 914 dp. Its shortest side is under 600, so tablet is false and the app takes every phone branch, on real hardware, with real touch, fonts and system bars. I took captures of every screen this way before starting the tablet work, then took them again at the end and compared them screen by screen. Any difference was a bug by the rule above.
The check works because the tablet decision lives in one place. If tablet behavior were spread across many size checks, a fake phone window would only prove that the checks I remembered were right. With a single flag at the root, the fake phone either takes the phone branches everywhere or it doesn't.
The same commands work in the other direction too. Shrinking the density on a tablet makes the window bigger in dp, which is a quick way to see a layout on a larger screen than the one you own.
What's still rough
On iOS, Compose Multiplatform 1.9.3 occasionally crashes during rotation, inside its own layout code ("List is empty" from DepthSortedSet). It looks like JetBrains issue CMP-8935. Tablet layouts made it easier to hit, because rotating an iPad now swaps between two quite different layouts instead of stretching one. I can reproduce it now and then on an iPad simulator, never on demand. The fix has to come from a framework upgrade, so that's next on my list.
What I'd tell you to copy
- Ask "is this a tablet?" with the shortest side of the window. Keep width classes for "how much room is there right now?". Decide the tablet flag once at the root and carry it down.
- Scale content that was designed at one size with
LocalDensity, not with new sizes in every component. Divide your constraints by the zoom, keepfontScale, and cap the factor. - Prove that phones didn't change with
adb shell wm sizeon a real tablet, and compare against captures you took before the change.
Questions, or spotted something wrong? Email [email protected]. I read everything.