admin管理员组

文章数量:1122832

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?

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?

Share Improve this question asked Nov 22, 2024 at 2:20 Pierre VieiraPierre Vieira 3,2508 gold badges28 silver badges52 bronze badges 3
  • Have you tried using the suggestions? – tomerpacific Commented Nov 22, 2024 at 8:49
  • This question is similar to: Android API 35+ Status/Navigation bar color/height from Java. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – tomerpacific Commented Nov 22, 2024 at 8:49
  • @tomerparcifc it's not the same, I'm using compose and in this case if I use this passing my main activity as parameter I'll have problems with the app bottom bar height. – Pierre Vieira Commented Nov 22, 2024 at 19:34
Add a comment  | 

1 Answer 1

Reset to default 1

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 (isAppInDarkTheme()) {
        dark(color)
    } else {
        light(color, color)
    }
}

本文标签: How to solve deprecation sets for statusBarColor and navigationBarColor in Android api 35Stack Overflow