question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Changing light/dark mode not works for color resources

See original GitHub issue

AppIntro Version: current master (SNAPSHOT)

Device/Android Version: Emulator / Android 11 (API level 30)

Issue details / Repro steps / Use case background: If the intro is open and the dark mode configuration changes not all colors are changed.

  1. Open intro in light mode
  2. Change theme to dark mode by control center

The drawables (set by resource ID) will be changed like expected. All background & font colors (that set directly) does not change.

After the design mode changed the activity will recreated. The slides will added again with new (dark mode) parameters. The colors are set directly and I found that the onActivityCreated AppIntroBaseFragment.kt#L73 method is called while recreating with the “old” parameters and the explicit set colors will be reused.

The simplest solution is to give a color resource ID instead of the color itself. As more cleaner solution we should improve the instance saving flow to keep them per fragment itself instead to referencing to the activity lifecycle.

Your Code:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // Have default + night variants
    // Works fine
    setIndicatorColor(
            selectedIndicatorColor = ContextCompat.getColor(this, R.color.intro_pagination_indicator),
            unselectedIndicatorColor = ContextCompat.getColor(this, R.color.meta_grey)
    )

    // Have default + night variants
    // Works fine
    setBackArrowColor(ContextCompat.getColor(this, R.color.accent))
    setNextArrowColor(ContextCompat.getColor(this, R.color.accent))
    setColorDoneText(ContextCompat.getColor(this, R.color.paper))

    addSlide(AppIntroFragment.newInstance(
            // ...

            // Has default + night variant
            // Works fine
            imageDrawable = R.drawable.ic_logo, 

            // Has default + night variant
            // Does NOT work
            titleColor = ContextCompat.getColor(this, R.color.text),

            // Has default + night variant
            // Does NOT work
            descriptionColor = ContextCompat.getColor(this, R.color.text),

            // Has default + night variant
            // Does NOT work
            backgroundColor = ContextCompat.getColor(this, R.color.paper),
            
            // ...
    ))
}

Working light:

Failed dark:

Expected result:

I will playing around a little bit and make a PR next days.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
cortinicocommented, Feb 3, 2021

Thanks for the reproducer 👍 That’s definitely a bug/improvement for the future.

The simplest solution is to give a color resource ID instead of the color itself.

+1 for this. We should add 3 new parameters to the SliderPage:

    @ColorRes var backgroundColorRes: Int = 0,
    @ColorRes var titleColorRes: Int = 0,
    @ColorRes var descriptionColorRes: Int = 0,

And potentially start a deprecation cycle for the former @ColorInt parameters

As more cleaner solution we should improve the instance saving flow to keep them per fragment itself nstead to referencing to the activity lifecycle.

That’s also a plus but unrelated to this bug.

1reaction
cortinicocommented, May 26, 2021

Hi, is there a potential update on the fix? Thanks!

Not really, but @Fabi755 might have a fix incoming? 😁

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changing between Light/Dark modes doesn't update colors of ...
I added values-night folder where I added new colors.xml with different hex for dark mode. Then I extended from Theme.MaterialComponents.
Read more >
Change to dark or color mode on your Android device
On your phone, open the Settings app. · Tap Battery and then Battery Saver and then Set a schedule. · Choose No schedule....
Read more >
Dark theme - Android Developers
Dark theme applies to both the Android system UI and apps running on the device. ... avoid hard-coded colors or icons intended for...
Read more >
Supporting Dark Mode in Your Interface - Apple Developer
Before you change your code, turn on Dark Mode and see how your app responds. ... Colors that work well in a light...
Read more >
The Developer's Guide to Dark Mode in Email
Dark Mode inverts the colors on your device to decrease the amount of light on your screen, most commonly, inverting a white background...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found