androidandroid-jetpack-composeandroid-dark-theme

How to correctly set the ForceDarkAllowed to false in jetpack compose app


I want to set the ForceDarkAllowed to false in jetpack compose app. I've found the information about the flag in the answer to this question. There are 2 ways mentioned how this flag can be set. None of them seems to be straight forward when jetpack compose is used. I have jetpack compose android app where I use Material3 design and I don't have the styles.xml file at all. So I can't add the

<style name="AppTheme" parent="...">
    <!-- Customize your theme here. -->
    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>

there.

Second method allows me to set it on the View object using the setForceDarkAllowed method. I'm not advanced in jetpack compose, but I don't see how I could get the View object in my project.

So how to set that flag properly in project that is fully written using jetpack compose and MaterialTheme3?


Solution

  • In your Theme.kt use:

    LocalView.current.isForceDarkAllowed = true // or false

    or you can use it with other views:

    val view = remember { View(this) }
    DisposableEffect(Unit) {
        view.isForceDarkAllowed = true // or false
        onDispose { }
    }