react-native: [android] PlatformColor does not respond to dark | light mode changes on android ?

Description

PlatformColor on android does not pickup the correct color on theme changes as it does on IOS.

it’s worth mentioning that useColorScheme does pick up the device theme correctly.

https://user-images.githubusercontent.com/19273413/147874550-5c5650e5-4c5e-465a-b15a-897726f93a78.mp4

Version

66.4

Output of npx react-native info

System:
    OS: macOS 11.6.1
    CPU: (8) x64 Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
    Memory: 295.98 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.1 - /usr/local/bin/node
    Yarn: 1.22.15 - ~/.yarn/bin/yarn
    npm: 8.1.2 - ~/n/bin/npm
    Watchman: 2021.12.20.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.11.2 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.0.1, iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0
    Android SDK:
      API Levels: 28, 29, 30, 31
      Build Tools: 28.0.3, 29.0.2, 29.0.3, 30.0.2, 31.0.0
      System Images: android-29 | Google APIs Intel x86 Atom, android-29 | Google Play Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: Arctic Fox 2020.3.1 Patch 3 Arctic Fox 2020.3.1 Patch 3
    Xcode: 13.1/13A1030d - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_292 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.2 => 17.0.2
    react-native: 0.66.4 => 0.66.4
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps to reproduce

after creating a react native project.

[1] create a values-night/colors.xml and values/colors.xml inside android/app/src/main/res

[2] add both dark mode and light mode colors to both colors.xml files accordingly.

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="text">#000000</color>
  <color name="background">#FFFFFF</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="text">#FFFFFF</color>
  <color name="background">#000000</color>
</resources>

[3] rebuild the app.

[4] change your device theme from settings

observation: colors don’t react to the device theme.

[5] try changing your device theme then reloading from dev menu.

observation: colors change & respect the device theme after reload.

[6] removing uiMode from android:configChanges inside inside AndroidManifest.xml does resolve the issue however it’s performant ( the application would then be torn down and recreated with the theme change ).

Snack, code example, screenshot, or link to a repository

https://github.com/a-eid/platformColorAndroidIssue

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 13
  • Comments: 17 (3 by maintainers)

Most upvoted comments

As a temporary fix, you can add this to your MainActivity.kt:

override fun onConfigurationChanged(newConfig: Configuration) {
    super.onConfigurationChanged(newConfig)
    this.resources.updateConfiguration(newConfig,this.resources.displayMetrics)
    reactInstanceManager.onConfigurationChanged(this,newConfig)
}

Note that the updateConfiguration used here is a deprecated Android API


Internally, the Appearance module reads the color scheme from the context of the activity https://github.com/facebook/react-native/blob/d86412dcc6c592a9db2165935c7320a822579bfa/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.kt#L52-L58

https://github.com/facebook/react-native/blob/d86412dcc6c592a9db2165935c7320a822579bfa/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.kt#L38-L50

However, this is not the most up-to-date source of truth (the comment in the codes above is wrong). Changing the system-wide color scheme setting only gives you an event handled in onConfigurationChanged, without changing the context of the activity.

You can fix this by simply changing the context on activity in onConfigurationChanged and notifying the Appearance module again, but a better fix should be to use the uiMode in the param of onConfigurationChanged, only use the value on the activity as a fallback, or using some kind of ContextWrapper to set the content on activity, or using something else and I don’t know in Android API as the source of truth of the appearance.


This problem only happens when you call Appearance.setColorScheme with unspecified, or something equivalent.

Would really love to see this resolved! 🙏

I can’t really get @benpigchu 's fix to work on RN 0.76.7. I’ve also tried a number of variants, such as

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        UiThreadUtil.runOnUiThread {
//            resources.updateConfiguration(newConfig, resources.displayMetrics)
            val newContext = this.createConfigurationContext(newConfig)
            val rn = this.reactNativeHost.reactInstanceManager // Other ways to obtain the instance manager result in null reference exceptions
            rn.onConfigurationChanged(newContext, newConfig)
            rn.currentReactContext?.UIManager?.invalidate() // tried, but has no effect
        }
    }

As well as forcing a rerender of the components in question, nothing seems to refresh the values other than relaunching the app.

Sadly, with higher target sdk version the deprecated Android API used in my temporary fix do not works anymore. My workaround only works for rn 0.75 or earlier where target adk version is lower.

Hello. You just need to do a few things for those who use the Expo to make it works automatically.

  1. Added in your app.json configuration { "expo": { "userInterfaceStyle": "automatic" } }

  2. added expo-system-ui dependence

After that, I could use any code as

1. const scheme = useColorScheme();
2. const colorScheme = Appearance.getColorScheme();

that worked normally.

Sources

  1. https://docs.expo.dev/guides/color-schemes/
  2. https://docs.expo.dev/versions/latest/sdk/system-ui/

issue still exists, would love to contribute a fix with minimal assistance.

PlatformColor seems to be working with wix/react-native-navigations some how but not with react native.

https://user-images.githubusercontent.com/19273413/148926040-921686bf-3a62-42c0-9dff-f3d88f11b9eb.mp4

Keep in mind that in the vid above I use only text from react native. All colors change correctly but not for the text. It requires an app reload