A composable function is a Kotlin function annotated with @Composable that describes a piece of user interface instead of returning a view. Compose calls it to build the screen and calls it again whenever the state it reads changes. The @Preview annotation lets Android Studio render a composable function directly in the editor, so you can check the layout without running the app on a device. This guide covers the composable function preview workflow for beginners.
Both ideas are the foundation of Jetpack Compose, the declarative UI toolkit that replaces XML layouts and View classes for new Android screens.
A composable function is a regular Kotlin function with the @Composable annotation. It does not return a View object. It emits UI by calling other composable functions, and the Compose runtime records those calls in a tree.
Greeting takes data in through its parameters and describes what to show. It has no return value because the UI is the side effect of calling Text. By convention, composable functions that emit UI are named with a capital first letter, like a class name, while composables that return a value (for example rememberScrollState()) start with a lowercase letter.
Composable functions can only be called from other composable functions. The entry point is setContent inside an activity:
The lambda passed to setContent is itself a composable lambda, which is why Greeting can be called inside it.
When the app starts, Compose runs your composable functions for the first time. This is called the initial composition. Compose remembers which composables read which state values.
When a state value changes, Compose runs only the composable functions that read that state again. This is called recomposition. Because of this, a composable function must be fast and must not depend on being called in a fixed order or a fixed number of times.
Each click changes count, Compose notices that Text("Clicked $count times") read it, and the affected part of the screen updates. You never call a method like setText. The UI is a function of state.
ViewModel or inside effect handlers, not in the body.@Preview marks a composable function that Android Studio should render in the Design or Split view of the editor. You do not need an emulator, so you see changes in a second or two.
The preview function must have no required parameters (or use a PreviewParameterProvider). It exists only for tooling, so keep it separate from your real screens and make it private if you prefer:
The @Preview annotation lives in the androidx.compose.ui.tooling.preview package. The tooling dependency is added to debug builds only, which is the default in a new Compose project:
In a project that uses the Compose BOM, you leave out the version numbers and let the BOM choose compatible versions.
@Preview accepts parameters that change how the preview renders.
| Parameter | What it does |
|---|---|
name | Label shown above the preview in the editor |
showBackground | Draws a background so white text is visible |
widthDp, heightDp | Fixes the preview size in dp |
uiMode | Switches light and dark mode, for example Configuration.UI_MODE_NIGHT_YES |
fontScale | Tests larger system font sizes |
locale | Previews a language, for example "fr" |
device | Picks a device definition, such as a phone or tablet |
You can stack several @Preview annotations on one function to see light and dark mode side by side. The androidx.compose.ui.tooling.preview package also offers multipreview annotations such as @PreviewLightDark, @PreviewFontScale and @PreviewScreenSizes, which bundle common combinations in a single line.
The preview pane has an interactive mode that lets you click buttons and watch state changes inside the preview. A separate option, deploy preview to device, runs just that composable on a connected device or emulator.
If a preview fails to render, check the following:
@Composable annotation as well as @Preview.PreviewParameterProvider.Application, a ViewModel from Hilt, or a network response. Give the composable plain parameters and pass fake data from the preview instead.A good habit is to split a screen in two. A stateful composable gets the ViewModel and collects state, and a stateless composable takes plain values and lambdas. You preview the stateless one.
Yes. A composable can return a value, for example remember returns the remembered object. Composables that emit UI usually return Unit and are named in PascalCase, while value-returning composables use camelCase.
No. The preview renders inside Android Studio using the layout library, so it is fast but cannot use real hardware, the network or most Android services. Use the deploy option or a real run to check device behaviour.
Recomposition runs it whenever state it reads changes. That is normal. If it feels too frequent, check that you read only the state you need and that you do not create new objects such as lambdas or lists on every call without remember.
The annotation has no effect at runtime, and the tooling library is a debug-only dependency. Keeping previews in the source set costs nothing in release builds.
A composable function is a Kotlin function marked @Composable that describes UI from its parameters and state, and Compose runs it again when that state changes. @Preview renders a composable in Android Studio so you can iterate quickly, with parameters for size, theme, locale and font scale. Keep composables free of side effects, split stateful and stateless parts, and give previews plain sample data.