nayan dhabarde
nayan dhabarde

Reputation: 2346

How to disable Darktheme when using Jetpack compose in Android app?

I have an Android app and it is currently in MVP state some of the users use their phone in Dark mode. There is no time to optimize for Dark theme.

I already tried passing same light theme colors to the Material theme does not work.

Also tried this but didn't work,

AppTheme(false) {
  content()
}

because theme composable says,

fun AppTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable() () -> Unit) {
}

Upvotes: 2

Views: 5538

Answers (5)

mikolaj-jalocha
mikolaj-jalocha

Reputation: 728

For me solution mentioned in other Stack Overflow's question worked:

The working solution for me was to add <item name="android:forceDarkAllowed" tools:targetApi="q">false</item> inside AppTheme block in styles.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>

Upvotes: 0

Gustav
Gustav

Reputation: 1

Set useDarkTheme to false instead of isSystemInDarkTheme() in the Theme.kt file. This ensures that the application doesn't check whether or not the phone has dark theme enabled.

@Composable
fun AppNameTheme(
    useDarkTheme: Boolean = false,
    content: @Composable () -> Unit
) {
    val colors = if (!useDarkTheme) {
        LightColors
    } else {
        DarkColors
    }
    MaterialTheme(
        colorScheme = colors,
        content = content, typography = Typography
    )
}

Upvotes: -1

Muhammad Ammar
Muhammad Ammar

Reputation: 556

Go to MainActivity add darkTheme = false just like it.

class MainActivity : ComponentActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        installSplashScreen()
        setContent {
           DemoAppTheme(darkTheme = false) {
}
}
}

Upvotes: 6

mas
mas

Reputation: 85

if you use material theme builder that is also recommended by google team at the end of the generated file Theme.kt, you will have such a code:

@Composable
fun AppNameTheme(
    useDarkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val colors = if (!useDarkTheme) {
        LightColors
    } else {
        DarkColors
    }
    MaterialTheme(
        colorScheme = colors,
        content = content, typography = Typography
    )
}

so if you just set val colors = LightColors the app will remain in light mode eveen if user change its device mode to dark

Upvotes: 0

nayan dhabarde
nayan dhabarde

Reputation: 2346

Thanks Philip Dukhov for reference, basically need to use a descendant of

Theme.MaterialComponents.Light

in themes.xml

Upvotes: 1

Related Questions