androidandroid-jetpack-compose

How to solve deprecation sets for statusBarColor and navigationBarColor in Android api 35+?


I have the following composable theme:

@Composable
fun AppTheme(
    themeOption: ThemeOption = ThemeOption.SYSTEM,
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit,
) {
    val isDarkTheme = isAppInDarkTheme(themeOption)
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (isDarkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }

        isDarkTheme -> darkScheme
        else -> lightScheme
    }
    val view = LocalView.current
    if (!view.isInEditMode) {
        SideEffect {
            (view.context as? Activity)?.window?.apply {
                statusBarColor = colorScheme.background.toArgb()
                navigationBarColor = colorScheme.surfaceContainer.toArgb()
            }?.also {
                WindowCompat.getInsetsController(it, view).apply {
                    isAppearanceLightStatusBars = !isDarkTheme
                    isAppearanceLightNavigationBars = !isDarkTheme
                }
            }
        }
    }

    MaterialTheme(
        colorScheme = colorScheme,
        content = content
    )
}

The piece of code that has the deprecation is:

WindowCompat.getInsetsController(it, view).apply {
    isAppearanceLightStatusBars = !isDarkTheme
    isAppearanceLightNavigationBars = !isDarkTheme
}

The deprecation messages are:

Deprecated
Draw proper background behind WindowInsets.Type.navigationBars() or WindowInsets. Type. tappableElement() instead.

Deprecated
Draw proper background behind WindowInsets.Type.statusBars()} instead.

I saw that is related to API 35. How can I solve it?


Solution

  • I solved this problem with the following:

    1. Remove the code:
    val view = LocalView.current
    if (!view.isInEditMode) {
        SideEffect {
            (view.context as? Activity)?.window?.apply {
                statusBarColor = colorScheme.background.toArgb()
                navigationBarColor = colorScheme.surfaceContainer.toArgb()
            }?.also {
                WindowCompat.getInsetsController(it, view).apply {
                    isAppearanceLightStatusBars = !isDarkTheme
                    isAppearanceLightNavigationBars = !isDarkTheme
                }
            }
        }
    }
    
    1. In your build gradle add the following dependency:
    dependencies {
        val activity_version = 1.9.3
        // Java language implementation
        implementation("androidx.activity:activity:$activity_version")
        // Kotlin
        implementation("androidx.activity:activity-ktx:$activity_version")
    }
    
    1. In your onCreate of your single activity use the following:
    override fun onCreate(savedInstanceState: Bundle?) {
        // ...
        enableEdgeToEdge(statusBarStyle = getStatusBarStyle())
    }
    
    @Composable
    private fun getStatusBarStyle(): SystemBarStyle = SystemBarStyle.run {
        val color = Color.Transparent.toArgb()
        if (isSystemInDarkTheme()) { // Or other validation to verify if the app is in dark theme
            dark(color)
        } else {
            light(color, color)
        }
    }