skip to Main Content

I am working on an Android Project but I am struggling with the dynamic color preivew in the Android Studio.

Here is my Composable function and it’s preview.

CODE

@Preview(wallpaper = Wallpapers.RED_DOMINATED_EXAMPLE)
@Composable
private fun TestPreview() {
    ClockTheme {
        Surface(
            modifier = Modifier.wrapContentSize(),
            color = MaterialTheme.colorScheme.background
        ) {
            Text(
                text = "Hello World!",
                color = MaterialTheme.colorScheme.primary,
                style = MaterialTheme.typography.displayLarge,
                modifier = Modifier
            )
        }
    }
}

PREVIEW
Preview of the Composable

Here is the Expected Preview of the Composable (This Preview is from a brand new Project where dynamic color preview perfecly works):
Expected Preview of the Composable

Here I expect the color to be RED dominated but the color I am getting in the preview is from the static theme.


This my Color.kt file that stores each color value.

val md_theme_light_primary = Color(0xFF3F5AA9)
val md_theme_light_onPrimary = Color(0xFFFFFFFF)
val md_theme_light_primaryContainer = Color(0xFFDBE1FF)
val md_theme_light_onPrimaryContainer = Color(0xFF00174C)
val md_theme_light_secondary = Color(0xFF3E5AA9)
val md_theme_light_onSecondary = Color(0xFFFFFFFF)
val md_theme_light_secondaryContainer = Color(0xFFDBE1FF)
val md_theme_light_onSecondaryContainer = Color(0xFF00174B)
val md_theme_light_tertiary = Color(0xFF84468E)
val md_theme_light_onTertiary = Color(0xFFFFFFFF)
val md_theme_light_tertiaryContainer = Color(0xFFFFD6FF)
val md_theme_light_onTertiaryContainer = Color(0xFF350040)
val md_theme_light_error = Color(0xFFBA1A1A)
val md_theme_light_errorContainer = Color(0xFFFFDAD6)
val md_theme_light_onError = Color(0xFFFFFFFF)
val md_theme_light_onErrorContainer = Color(0xFF410002)
val md_theme_light_background = Color(0xFFFEFBFF)
val md_theme_light_onBackground = Color(0xFF1B1B1F)
val md_theme_light_surface = Color(0xFFFEFBFF)
val md_theme_light_onSurface = Color(0xFF1B1B1F)
val md_theme_light_surfaceVariant = Color(0xFFE2E1EC)
val md_theme_light_onSurfaceVariant = Color(0xFF45464F)
val md_theme_light_outline = Color(0xFF757680)
val md_theme_light_inverseOnSurface = Color(0xFFF2F0F4)
val md_theme_light_inverseSurface = Color(0xFF303034)
val md_theme_light_inversePrimary = Color(0xFFB4C5FF)
val md_theme_light_surfaceTint = Color(0xFF3F5AA9)
val md_theme_light_outlineVariant = Color(0xFFC6C6D0)
val md_theme_light_scrim = Color(0xFF000000)

val md_theme_dark_primary = Color(0xFFB4C5FF)
val md_theme_dark_onPrimary = Color(0xFF012978)
val md_theme_dark_primaryContainer = Color(0xFF244290)
val md_theme_dark_onPrimaryContainer = Color(0xFFDBE1FF)
val md_theme_dark_secondary = Color(0xFFB4C5FF)
val md_theme_dark_onSecondary = Color(0xFF002A77)
val md_theme_dark_secondaryContainer = Color(0xFF234290)
val md_theme_dark_onSecondaryContainer = Color(0xFFDBE1FF)
val md_theme_dark_tertiary = Color(0xFFF6ADFE)
val md_theme_dark_onTertiary = Color(0xFF50155C)
val md_theme_dark_tertiaryContainer = Color(0xFF692E74)
val md_theme_dark_onTertiaryContainer = Color(0xFFFFD6FF)
val md_theme_dark_error = Color(0xFFFFB4AB)
val md_theme_dark_errorContainer = Color(0xFF93000A)
val md_theme_dark_onError = Color(0xFF690005)
val md_theme_dark_onErrorContainer = Color(0xFFFFDAD6)
val md_theme_dark_background = Color(0xFF1B1B1F)
val md_theme_dark_onBackground = Color(0xFFE4E2E6)
val md_theme_dark_surface = Color(0xFF1B1B1F)
val md_theme_dark_onSurface = Color(0xFFE4E2E6)
val md_theme_dark_surfaceVariant = Color(0xFF45464F)
val md_theme_dark_onSurfaceVariant = Color(0xFFC6C6D0)
val md_theme_dark_outline = Color(0xFF8F909A)
val md_theme_dark_inverseOnSurface = Color(0xFF1B1B1F)
val md_theme_dark_inverseSurface = Color(0xFFE4E2E6)
val md_theme_dark_inversePrimary = Color(0xFF3F5AA9)
val md_theme_dark_surfaceTint = Color(0xFFB4C5FF)
val md_theme_dark_outlineVariant = Color(0xFF45464F)
val md_theme_dark_scrim = Color(0xFF000000)

val seed = Color(0xFFB4C3FA)

This is Theme.kt file that defines the theme for the app.

private val LightColorScheme = lightColorScheme(
    primary = md_theme_light_primary,
    onPrimary = md_theme_light_onPrimary,
    primaryContainer = md_theme_light_primaryContainer,
    onPrimaryContainer = md_theme_light_onPrimaryContainer,
    secondary = md_theme_light_secondary,
    onSecondary = md_theme_light_onSecondary,
    secondaryContainer = md_theme_light_secondaryContainer,
    onSecondaryContainer = md_theme_light_onSecondaryContainer,
    tertiary = md_theme_light_tertiary,
    onTertiary = md_theme_light_onTertiary,
    tertiaryContainer = md_theme_light_tertiaryContainer,
    onTertiaryContainer = md_theme_light_onTertiaryContainer,
    error = md_theme_light_error,
    errorContainer = md_theme_light_errorContainer,
    onError = md_theme_light_onError,
    onErrorContainer = md_theme_light_onErrorContainer,
    background = md_theme_light_background,
    onBackground = md_theme_light_onBackground,
    surface = md_theme_light_surface,
    onSurface = md_theme_light_onSurface,
    surfaceVariant = md_theme_light_surfaceVariant,
    onSurfaceVariant = md_theme_light_onSurfaceVariant,
    outline = md_theme_light_outline,
    inverseOnSurface = md_theme_light_inverseOnSurface,
    inverseSurface = md_theme_light_inverseSurface,
    inversePrimary = md_theme_light_inversePrimary,
    surfaceTint = md_theme_light_surfaceTint,
    outlineVariant = md_theme_light_outlineVariant,
    scrim = md_theme_light_scrim,
)


val DarkColorScheme = darkColorScheme(
    primary = md_theme_dark_primary,
    onPrimary = md_theme_dark_onPrimary,
    primaryContainer = md_theme_dark_primaryContainer,
    onPrimaryContainer = md_theme_dark_onPrimaryContainer,
    secondary = md_theme_dark_secondary,
    onSecondary = md_theme_dark_onSecondary,
    secondaryContainer = md_theme_dark_secondaryContainer,
    onSecondaryContainer = md_theme_dark_onSecondaryContainer,
    tertiary = md_theme_dark_tertiary,
    onTertiary = md_theme_dark_onTertiary,
    tertiaryContainer = md_theme_dark_tertiaryContainer,
    onTertiaryContainer = md_theme_dark_onTertiaryContainer,
    error = md_theme_dark_error,
    errorContainer = md_theme_dark_errorContainer,
    onError = md_theme_dark_onError,
    onErrorContainer = md_theme_dark_onErrorContainer,
    background = md_theme_dark_background,
    onBackground = md_theme_dark_onBackground,
    surface = md_theme_dark_surface,
    onSurface = md_theme_dark_onSurface,
    surfaceVariant = md_theme_dark_surfaceVariant,
    onSurfaceVariant = md_theme_dark_onSurfaceVariant,
    outline = md_theme_dark_outline,
    inverseOnSurface = md_theme_dark_inverseOnSurface,
    inverseSurface = md_theme_dark_inverseSurface,
    inversePrimary = md_theme_dark_inversePrimary,
    surfaceTint = md_theme_dark_surfaceTint,
    outlineVariant = md_theme_dark_outlineVariant,
    scrim = md_theme_dark_scrim,
)

val LightBackgroundTheme = BackgroundTheme(color = md_theme_light_inverseOnSurface)

val DarkBackgroundTheme = BackgroundTheme(color = md_theme_dark_inverseOnSurface)


@Composable
fun ClockTheme(
    useDarkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val supportsDynamicTheming = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S

    val colorScheme = when {
        supportsDynamicTheming -> {
            val context = LocalContext.current
            if (useDarkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }
        else -> if (useDarkTheme) DarkColorScheme else LightColorScheme
    }

    val defaultBackgroundTheme = BackgroundTheme(
        color = colorScheme.surface,
        tonalElevation = 2.dp,
    )

    val backgroundTheme = when {
        supportsDynamicTheming -> defaultBackgroundTheme
        else -> if (useDarkTheme) DarkBackgroundTheme else  LightBackgroundTheme
    }

    val tintTheme = when {
        supportsDynamicTheming -> TintTheme(colorScheme.primary)
        else -> TintTheme()
    }

    val view = LocalView.current
    if (!view.isInEditMode) {
        SideEffect {
            val window = (view.context as Activity).window
            window.statusBarColor = colorScheme.background.toArgb()
            WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = !useDarkTheme
        }
    }

    CompositionLocalProvider(
        LocalBackgroundTheme provides backgroundTheme,
        LocalTintTheme provides tintTheme,
    ){
        MaterialTheme(
            colorScheme = colorScheme,
            typography = Typography,
            content = content
        )
    }
}

These are the dependencies in the module level gradle file for compose:

// compose
    implementation(platform("androidx.compose:compose-bom:2024.03.00"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")
    implementation("androidx.compose.material3:material3-window-size-class")

I have spent couple of days in sorting this issue but I am unable to do it. Kindly help. Feel free to ask for more information regarding the issue.

I expect to get the previews with dynamic colors in android studio.

2

Answers


  1. If you see the implementation of @PreviewDynamicColors by tapping Ctrl+B, there is name assigned to the @Preview annotation not color.

    /**
     * A MultiPreview annotation for desplaying a @[Composable] method using four different wallpaper colors.
     *
     * Note that the app should use a dynamic theme for these previews to be different.
     */
    @Retention(AnnotationRetention.BINARY)
    @Target(
            AnnotationTarget.ANNOTATION_CLASS,
            AnnotationTarget.FUNCTION
    )
    @Preview(name = "Red", wallpaper = RED_DOMINATED_EXAMPLE)
    @Preview(name = "Blue", wallpaper = BLUE_DOMINATED_EXAMPLE)
    @Preview(name = "Green", wallpaper = GREEN_DOMINATED_EXAMPLE)
    @Preview(name = "Yellow", wallpaper = YELLOW_DOMINATED_EXAMPLE)
    annotation class PreviewDynamicColors
    

    You’ve applied color = MaterialTheme.colorScheme.primary to Text() which results in same color in each Preview of the Text() widget.

    In order to get the preview of each coloured Text(), try writing individual Preview function for each color.

    Login or Signup to reply.
  2. Following preview successfully renders the dynamic colors by mentioning the api level 31 (Android 12.0), 33 (Android 13.0) but not for 34 (Android 14.0).

    I don’t why it not work for 34.

    @Preview(
        wallpaper = Wallpapers.RED_DOMINATED_EXAMPLE, 
        apiLevel = 31
    )
    @Composable
    private fun TestPreview() {
        ClockTheme {
            // stuff
    
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search