Compose Preview
Android Studio 2: The New UI
Course 2 ยท Chapter 5 ยท Seeing Your UI Without Running It
In a Compose project there is no res/layout folder (Chapter 3), so there is no XML file to open in the old Layout Editor. The screen is Kotlin code, and the way you see it without installing the
app on a phone is Compose Preview: you mark a function with @Preview and Android Studio draws it beside your code. This chapter explains how to write previews, the tools that go with them
(several previews from one line, interactive mode, running a preview on a device, UI Check) and how this differs from the Layout Editor you may have read about.
@Preview import in a generated MainActivity.kt
and the existence of a Layout Validation tool window in the View menu.Checked in the docs: two web searches against Google's Preview your UI with composable previews page and its debugging page. What they said is marked (docs) below.
General knowledge, not checked: marked (general knowledge). When you reach a screenshot-based version of this chapter, trust the screenshots over these notes.
Writing a Preview
A preview is a Kotlin function with two annotations: @Composable (it draws UI) and @Preview (draw it in the IDE). It takes no parameters, and it normally wraps your screen in your app's theme
so the colours and fonts match. A generated project contains one:
showBackground = true paints a white background behind the preview; without it the preview is transparent, which can make light text invisible. MyAppTheme is whatever theme function the
template created for your project (in the project checked it was in ui/theme, next to Color.kt, Theme.kt and Type.kt). Two details connect back to Chapter 4:
- The
Previewannotation lives inandroidx.compose.ui.tooling.preview, which comes from theui-tooling-previewlibrary, declared asimplementation(libs.androidx.compose.ui.tooling.preview). - The
ui-toolinglibrary is declared asdebugImplementation. My understanding is that this is the library the IDE uses to render and inspect previews and that it is kept out of release builds (general knowledge).
You can have as many @Preview functions as you like in a file, and several annotations on one function to see it in different set-ups. Common @Preview parameters include name, group,
widthDp, heightDp, uiMode (for dark mode), fontScale and device (general knowledge: only showBackground appears in the project I looked at).
The Preview Pane
When a file contains an @Preview, the editor can show the rendered preview next to the code. Android Studio normally gives you a way to switch between code only, preview only and both side by side, and to build
and refresh the preview when it goes out of date (general knowledge: I could not check the names or places of those controls on Rabbit 2). Two practical points hold whatever the buttons are called:
- The preview needs the project to build successfully. If the file has a compile error (a missing import, a function that no longer exists) the preview cannot draw, so fix the errors in the editor first, or the preview shows an error instead of your UI (general knowledge).
- A preview is a picture of one function with fixed inputs. It does not run your app, your database or your network code. Give a screen's composable its data as plain parameters so a preview can pass in sample values, and keep database or view-model work outside the composable you preview (general knowledge).
Several Previews from One Line
(docs) Compose's tooling provides ready-made multipreview annotations that show one composable in several configurations at once, from UI tooling 1.6.0-alpha01 onward:
| Annotation | What it adds |
|---|---|
@PreviewScreenSizes | The same UI at several screen sizes. |
@PreviewFontScales | The same UI at several font scales (small to large text). |
@PreviewLightDark | The same UI in light and dark mode. |
@PreviewDynamicColors | The same UI with different dynamic colour schemes. |
You can also define your own: an annotation class that carries several @Preview annotations with different settings, which then renders every variant wherever you apply it. (docs) One catch noted by
the docs: stacking several multipreview annotations does not produce every combination (for example every screen size in both light and dark); each annotation renders only its own variants.
Interactive Mode and Running a Preview on a Device
A normal preview is a still picture. (docs) Two tools make it behave more like the real app:
| Tool | What the docs say |
|---|---|
| Interactive mode | Runs a preview much as it would run on a phone or tablet, in its own isolated sandbox, so you can click elements and type input without affecting other previews. |
| Run Preview (deploy to device) | Click the Run Preview icon next to the @Preview annotation, or at the top of the preview, and Android Studio installs that preview on a connected device or emulator. It launches as a new activity in the same app, so it shares the app's manifest and permissions. |
Use interactive mode when you want to try a button or a text field; use the device route when the thing you are checking depends on the real device (a permission, a camera, real hardware behaviour). Chapter 6 covers devices and emulators.
UI Check, and What Happened to Layout Validation
(docs) UI Check automatically previews your UI in different configurations and highlights issues found, such as text that is cut off at a large font size or an element that does not fit a small screen. You turn it on from the UI Check icon on the preview. It is the quickest way to find accessibility and layout problems before you run anything.
Chapter 1's View > Tool Windows list also contains Layout Validation. The docs I found describe that tool for View-based (XML) layouts: it shows a layout in many device sizes at once. I found no Compose-specific description of it, so I cannot say what it does for a Compose project. For Compose, UI Check is the documented equivalent. The Problems panel (the Problems tool window, Alt+6) also reports design-tool issues (docs).
What Replaced the Layout Editor
| Layout Editor (XML Views) | Compose Preview |
|---|---|
| You open a layout XML file in a design surface. | You open a Kotlin file containing @Preview functions. There is no layout file. |
| You can drag widgets from a palette onto the screen (general knowledge). | You write or edit code; the preview redraws. The picture is output, not the thing you edit (general knowledge). |
| One layout file, one screen. | Any composable can have a preview, including small pieces such as a single card or button. |
| Resizing for different devices through the editor. | Different sizes, font scales and modes through parameters and multipreview annotations (docs). |
| Still available for XML projects (Chapter 3's Views templates). | The default for the Empty Activity template. |
Try It Yourself
These exercises are written from the docs, not from screenshots, so each asks you to report what you actually see. Your answers are the evidence this chapter lacks.
In the scratch project open MainActivity.kt and find the generated @Preview. Open the preview pane (write down the name or icon of every control you use to show code, preview or both, and to refresh the preview). Change the greeting text and say how the preview updates. Then remove showBackground = true and describe the difference.
Replace @Preview(showBackground = true) with @PreviewLightDark, then try @PreviewFontScales. Count how many previews each one produces and say what differs between them. Then add both annotations to the same function and check whether you get every combination.
Add a small composable with a Button that counts clicks (a remember { mutableIntStateOf(0) } counter), give it an @Preview, then try interactive mode. Afterwards run the same preview on an emulator or device with Run Preview. Say what each route showed and when you would choose one over the other.
ui-tooling-preview (implementation) and ui-tooling (debugImplementation) dependencies; the
androidx.compose.ui.tooling.preview.Preview import and a generated @Preview(showBackground = true) function in MainActivity.kt; the ui/theme files; the Layout Validation and
Problems entries in the View menu list.Checked in the docs (two web searches): Google's Preview your UI with composable previews page says multipreview annotations (
@PreviewScreenSizes, @PreviewFontScales,
@PreviewLightDark, @PreviewDynamicColors) exist from UI tooling 1.6.0-alpha01, that custom multipreviews are annotation classes carrying several previews, that stacked multipreview annotations do not combine,
that interactive mode runs in an isolated sandbox, that Run Preview installs the preview on a device or emulator as a new activity in the same app, and that UI Check previews the UI in different configurations and highlights
issues. The Problems-panel page describes Layout Validation for View-based layouts. I read search summaries, not the full pages.Not verified at all: the names, icons and positions of the preview pane's controls (code, split and design modes, build and refresh, interactive, run preview, UI Check) on Rabbit 2; the Gallery mode; the full list of
@Preview parameters; what Layout Validation does in a Compose project; what ui-tooling is used for in detail; the "general knowledge" advice on building previews with plain data; and the exercises
(not run by me). A screenshot of a @Preview with the preview pane open, and of the preview toolbar, would turn this chapter into a verified one.Sources: Preview your UI with composable previews (developer.android.com), Debug your Compose UI (developer.android.com), View issues for your design tools in the Problems panel (developer.android.com).