Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

setDefaultNightMode not working when using setTheme in main activity

Writer Matthew Barrera

I have an app where I'm forcing "MODE_NIGHT_NO" in my main (and only) activity, I do this right after super.onCreate().

AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO)

Now I'm adding a splash screen following this guide, basically:

  • Create a drawable for the splash screen

  • Create a style in values\themes.xml called SplashScreen with parent AppTheme (AppTheme's parent is Theme.MaterialComponents.DayNight.NoActionBar)

    <style name="SplashTheme" parent="AppTheme"> <item name="android:windowBackground">@drawable/splash_screen</item>
    </style>
  • Assign SplashTheme to the main activity in the Manifest

     android:name=".MainDrawerActivity" android:theme="@style/SplashTheme"...
  • Lastly call setTheme in the main activity, before super.onCreate() to set the correct AppTheme for the whole app

    override fun onCreate(savedInstanceState: Bundle?) { setTheme(R.style.AppTheme) super.onCreate(savedInstanceState) AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO) setContentView(R.layout.activity_main_drawer)

The splash screen works, but the issues is that after the splash screen finishes, the theme is changed to dark mode. Even if the phone IS NOT in dark mode.

I've tried calling it like this, but it doesn't work either, same behavior:

override fun onCreate(savedInstanceState: Bundle?) { AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO) setTheme(R.style.AppTheme) super.onCreate(savedInstanceState) setContentView(R.layout.activity_main_drawer)

I even tried deleting values-night\themes.xml to see what would happen, but same result. (I've restored said folder)

Why is it that setDefaultNightMode(MODE_NIGHT_NO) is not working in the same way when adding setTheme(AppTheme)?

Any guidance is highly appreciated.

2 Answers

Finally I realized I was setting the wrong theme to my activity in the setTheme() method. But even then, I'm not sure why it was showing the dark theme. I can't even recreate the issue now. It will do for now.

This should work but I do not know why. I think this is the problem with the Default material design theme. If you go to your theme files and just change the

 <style name="Theme.ContactForm" parent="Theme.MaterialComponents.Light.DarkActionBar">

to

 <style name="Theme.ContactForm" parent="Theme.AppCompat.Light.DarkActionBar">

This should work Let me know if this can hlep.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy