Maybe you can have a look at AppCompatDelegate.setDefaultNightMode()
you simply define your theme with the parent of DayNight:
<style name="MyTheme" parent="Theme.AppCompat.DayNight">
<!-- Blah blah -->
</style>
and each style with:
<style name="Theme.AppCompat.DayNight"
parent="Theme.AppCompat.Light" />
or
<style name="Theme.AppCompat.DayNight"
parent="Theme.AppCompat" />
and then you can call : AppCompatDelegate.setDefaultNightMode()
with one of these:
MODE_NIGHT_NO. Always use the day (light) theme.
MODE_NIGHT_YES. Always use the night (dark) theme.
MODE_NIGHT_FOLLOW_SYSTEM (default). This setting follows the system’s setting, which on Android Q and above is a system setting (more on this below).
MODE_NIGHT_AUTO_BATTERY. Changes to dark when the device has its ‘Battery Saver’ feature enabled, light otherwise.
MODE_NIGHT_AUTO_TIME & MODE_NIGHT_AUTO. Changes between day/night based on the time of day.
you would typically do this in your own custom application class:
public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
}
}
more info here