Blog Infos
Author
Published
Topics
Published

The @Preview annotation has several capabilities to help manage previewing composable functions. This article, going deep dive and covering the power of @Preview! 🔥

Theme preview
@Preview
@Composable
fun MyButtonPreview() {
    MyAppTheme {
        MyButton()
    }
}

This function generate the preview:

@Preview
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun MyButtonPreview() {
    ComposeNewsTheme {
        MyButton()
    }
}

Here we got:

@Preview(name = "Light")
@Preview(
    name = "Dark",
    uiMode = Configuration.UI_MODE_NIGHT_YES,
)
annotation class ThemePreviews

Now, It can be used like this:

@ThemePreviews
@Composable
fun MyButtonPreview() {
    MyAppTheme {
        MyButton()
    }
}
Font scale preview
@Preview(fontScale = 1.0f, name = "Default (100%)")
@Preview(fontScale = 0.85f, name = "Small (85%) ")
@Preview(fontScale = 1.15f, name = "Large (115%)")
@Preview(fontScale = 1.3f, name = "Largest (130%) ")
annotation class FontScalePreview

And the result:

Multi-Language preview
@Preview(locale = "en", name = "English")
@Preview(locale = "fr", name = "French")
@Preview(locale = "ar", name = "Arabic")
@Preview(locale = "ja", name = "Japanese")
annotation class MultiLanguagePreview
Screen sizes preview
@Preview(name = "Phone", device = "spec:width=411dp,height=891dp")
@Preview(name = "Foldable", device = "spec:width=673.5dp,height=841dp,dpi=480")
@Preview(name = "Tablet", device = "spec:width=1280dp,height=800dp,dpi=480")
@Preview(name = "Desktop", device = "spec:width=1920dp,height=1080dp,dpi=480")
annotation class DevicesPreviews

Here the result:

The device attribute also have some predefined phones specifications for testing more easily:

Preview a Screen
@Composable
fun NewsListScreen(
    viewModel: NewsListViewModel,
    onNavigateToDetailScreen: (news: News) -> Unit,
) {

    ...

}

For seeing the preview of this composable, you must provide a viewModel instance that makes previewing difficult. For handling this scenario, you can define a composable wrapper:

@Composable
fun NewsListRoute(
    viewModel: NewsListViewModel,
    onNavigateToDetailScreen: (news: News) -> Unit,
) {

    ...

    NewsListScreen(...)

}

Then change the Screen composable to a stateless version:

@Composable
private fun NewsListScreen(
    newsListState: NewsListState,
    onNavigateToDetailScreen: (news: News) -> Unit,
    onFavoriteClick: (news: News) -> Unit,
    onRefresh: () -> Unit,
) {

    ...

}
Provide parameter for previewing
@Composable
fun NewsItem(
    news: News,
    onFavoriteClick: (news: News) -> Unit,
) {

    ...

}

To prevent hard coding default values for previewing above composable, there is an annotation called @PreviewParameter.

@Preview
@Composable
private fun NewsItemPreview(
    @PreviewParameter(NewsProvider::class)
    news: News,
) {
    NewsItem(news = news, onFavoriteClick = {})
}

This is how the NewsProvider class define:

class NewsProvider: PreviewParameterProvider<News> {
    override val values = sequenceOf(
        News(
            author = "Kaaveh Mohamedi",
            description = "This is a sample description.",
            publishedAt = "2022/02.10",
            ...
        ),
        News(...),
        ...
    )
}

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

,

Reimagining Android Dialogs with Jetpack Compose

Traditional Android dialogs are hard to test, easy to leak, and painful to customize — and in a world of Compose-first apps, they’re overdue for an upgrade.
Watch Video

Reimagining Android Dialogs with Jetpack Compose

Keith Abdulla
Staff Android Engineer, Design Systems
Block

Reimagining Android Dialogs with Jetpack Compose

Keith Abdulla
Staff Android Engine ...
Block

Reimagining Android Dialogs with Jetpack Compose

Keith Abdulla
Staff Android Engineer, D ...
Block

Jobs

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
In the world of Jetpack Compose, where designing reusable and customizable UI components is…
READ MORE
blog
Material3 is the newest iteration of Material Design, with dynamic theming, revised components and…
READ MORE
Menu