Status Update
Comments
nj...@google.com <nj...@google.com>
sh...@gmail.com <sh...@gmail.com> #2
We could add an API to make the weighted distance customizable, aosp/3271691 has more info.
ph...@gmail.com <ph...@gmail.com> #3
Here's my temporary solution on the problem. I provide a custom context that returns the correct resources:
@Composable
fun rememberThemeAwareContext(isDarkTheme: Boolean): Context {
val context = LocalContext.current
return remember(context, isDarkTheme) {
ThemeAwareContext(context, isDarkTheme)
}
}
private class ThemeAwareContext constructor(
context: Context,
isDarkTheme: Boolean,
) : ContextWrapper(context) {
private val configurationContext = Configuration(context.resources.configuration)
.let { conf ->
conf.setNightMode(isNight = isDarkTheme)
context.createConfigurationContext(conf)
}
override fun getResources(): Resources = configurationContext.resources
override fun getTheme(): Resources.Theme = configurationContext.theme
}
private fun Configuration.setNightMode(isNight: Boolean) {
val notUiNightMask = Configuration.UI_MODE_NIGHT_MASK.inv()
val uiModeWithoutNight = uiMode and notUiNightMask
val flag = if (isNight) Configuration.UI_MODE_NIGHT_YES else Configuration.UI_MODE_NIGHT_NO
uiMode = uiModeWithoutNight or flag
}
Which I embed into my theme like this:
CompositionLocalProvider(
LocalContext provides rememberThemeAwareContext(isDarkTheme = isDarkTheme),
) {
MaterialTheme(
I've only faced a single problem and only on API 33: LocalImageVectorCache
sometimes caches the images. This is especially irritating since I not only use this technique for overriding system theme, but also for a couple of dark-only screens - they look the same both in dark and light modes.
The only thing I seem to be missing is that LocalImageVectorCache
and obtainImageVectorCache
would be public, then I could provide one of the two cache objects depending on the needed schema in each case.
de...@dyson.com <de...@dyson.com> #4
Any update on this? Looking at this ticket, it's been a year since the last update and it looks like a solution hasn't been started yet? Would be great to see this - it's a key feature that's missing from the Android developer experience!
an...@gmail.com <an...@gmail.com> #5
One workaround that we have noticed to be effective is to use non-compose methods to switch the app's appearance
UiModeManager#setApplicationNightMode
(API level 31 and above), AppCompatDelegate.setDefaultNightMode()
(API level 30 and below). But AppCompatDelegate
requires extending AppCompatActivity
and using AppCompat theme.
zs...@gmail.com <zs...@gmail.com> #6
of...@sebastianmarschall.com <of...@sebastianmarschall.com> #7
Please someone give us some hope.
nj...@google.com <nj...@google.com>
je...@bethinklabs.com <je...@bethinklabs.com> #8
lp...@google.com <lp...@google.com> #9
I'm not sure how the issue as described can be solved, the 'app theme' is just a selection of properties used inside Compose. Part of the intent behind the design of APIs such as MaterialTheme is that it's valid to have more than just two themes for light and dark, and these shouldn't be based on the system definition of dark (night) theme.
What is the motivation for using the -night qualifier, if you don't rely on the system theme, and you allow user overrides for light / dark? Instead you probably want these to be tied to your theme object, similar to how MaterialTheme provides colors / shapes / typography. That way you can support more than two configurations here, and icons can be unique per theme as well
Description
Jetpack Compose version: 1.2.0-alpha08
Jetpack Compose component used:
Image
-> painterResource functionAndroid Studio Build: Android Studio Bumblebee | 2021.1.1 Patch 2 Build #AI-211.7628.21.2111.8193401, built on February 17, 2022 Runtime version: 11.0.11+0-b60-7590822 x86_64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. macOS 12.3.1 GC: G1 Young Generation, G1 Old Generation Memory: 2048M Cores: 16 Registry: external.system.auto.import.disabled=true Non-Bundled Plugins: org.jetbrains.kotlin (211-1.6.10-release-923-AS7442.40), com.squareup.sqldelight (1.5.3), org.intellij.plugins.markdown (211.7142.37)
Kotlin version: 1.6.20
Problem statement
Drawable resources held in
drawable-night
resource folder only get used if system is in dark mode - meaning it only respects the system settings. Developers are encouraged to provide user overrides for the theme, so that it's possible to set app theme irrespective of the system. The images however, do not comply then and Android only shows the image corresponding to the system theme not the app themeSteps to Reproduce or Code Sample to Reproduce:
drawable
anddrawable-night
resource folders - for light and dark themes respectivelyImage
composable withpainterResource
, referencing the above drawabletheme
for thedarkMode
propertySee the attached project to reproduce