Skip to content

Jetpack Compose Preview — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Jetpack Compose Preview. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Your @Preview shows a blank screen, crashes with DefaultPreviewFactory, or doesn't reflect the actual runtime state.

Wrong Approach ❌

// Preview with no parameters — shows empty/default state only
@Preview(showBackground = true)
@Composable
fun BadPreview() {
    // This composable requires a ViewModel parameter
    UserProfileScreen(viewModel = ViewModel()) // NPE!
}
// Single preview for all screen sizes
@Preview
@Composable
fun PhonePreview() {
    // Tested only on one phone size
    MyComposable()
}

Output: Preview crashes due to missing dependencies. No multi-device testing.

Right Approach ✅

// Multiple previews for different configurations
@Preview(
    name = "Phone",
    device = Devices.PHONE,
    showBackground = true,
    showSystemUi = true
)
@Preview(
    name = "Tablet",
    device = Devices.TABLET,
    showBackground = true
)
@Preview(
    name = "Dark Mode",
    uiMode = Configuration.UI_MODE_NIGHT_YES
)
@Composable
fun MyComposablePreview() {
    // Use a preview-safe approach
    DodaTechTheme {
        Surface {
            MyComposable(
                title = "Preview Title",
                subtitle = "This renders in the IDE"
            )
        }
    }
}

// For ViewModel-dependent composables, use a wrapper
@Composable
fun MyComposablePreview() {
    DodaTechTheme {
        MyComposable(
            state = UiState.Success("Preview data"),
            onAction = {}
        )
    }
}

Output: Multiple preview variants rendering correctly across devices and themes.

Prevention

  • Decouple UI from ViewModel — accept plain state in composables.
  • Create wrapper preview composables with dummy data.
  • Use @Preview with device, uiMode, and fontScale parameters.
  • Always wrap previews in your app's Theme.

Common Mistakes with compose preview

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does my preview show a blank screen?

Common causes: missing @Preview annotation, exception during composition, or the composable depends on a ViewModel or Context that isn't available in preview.

### Can I preview different font scales?

Yes. Add fontScale = 1.5f to the @Preview annotation to test Accessibility. Combine with showBackground = true for better visibility.

### How do I preview a LazyColumn with many items?

Create a preview function that passes a large sample list to your composable. Use rememberLazyListState() with an initial position to preview scrolled state.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro