0

When the Android device set to dark mode. But the user wants to see Light mode only on this app. Is there any idea to handle this?

This code does not work for me

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

none of these codes is working

val config: Configuration = resources.getConfiguration()
        config.uiMode = Configuration.UI_MODE_NIGHT_NO
        resources.configuration.uiMode= Configuration.UI_MODE_NIGHT_NO
        applicationContext.createConfigurationContext(config)
        resources.updateConfiguration(config, getResources().getDisplayMetrics())
UmAnusorn
  • 10,420
  • 10
  • 72
  • 100

1 Answers1

0
I would like to see the method too, where you set once for all your activities. But as far I know you have to set in each activity before showing any views.

For reference check this:

http://www.anddev.org/applying_a_theme_to_your_application-t817.html

Edit (copied from that forum):

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Call setTheme before creation of any(!) View.
         setTheme(android.R.style.Theme_Dark);

        // ...
        setContentView(R.layout.main);
    }


Edit
If you call setTheme after super.onCreate(savedInstanceState); your activity recreated but if you call setTheme before super.onCreate(savedInstanceState); your theme will set and activity does not recreate anymore

  protected void onCreate(Bundle savedInstanceState) {
     setTheme(android.R.style.Theme_Dark);
     super.onCreate(savedInstanceState);


    // ...
    setContentView(R.layout.main);
}
Amit Joshi
  • 36
  • 4