I'm trying to develop an app that would be able to change the System dark mode setting (device wide). It would enable us to quickly access that "dark mode" setting and easely toggle it.
As I'm getting frustrated, I'm now basicaly throwing everything I find about dark mode (even though some solutions are not even supposed to work).
Here is the first part (where I setup "everything I need")
case "uimode":
uiModeManager = (UiModeManager) this.getSystemService(Context.UI_MODE_SERVICE);
appCompatDelegate = AppCompatDelegate.create(this,null);
resources = this.getResources();
configuration = new Configuration(resources.getConfiguration());
UiMode uiMode = new UiMode(this,"ui_cycle", appCompatDelegate);
uiMode.toggle();
break;
And here is the piece of script where I try everything I can to update the setting.
private void setMode(Integer mode) {
Log.d(TAG, "step 1" );
appCompatDelegate.setDefaultNightMode(mode);
Log.d(TAG, "step 2" );
appCompatDelegate.setLocalNightMode(mode);
Log.d(TAG, "step 3" );
uiModeManager.setNightMode(mode);
Log.d(TAG, "step 4" );
configuration.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
configuration.uiMode = mode;
resources.updateConfiguration(configuration, null);
Log.d(TAG, "step 5" );
}
public void toggle() {
switch(newMode) {
case "light":
setMode(2);
job.done("Night Mode");
break;
case "dark":
setMode(1);
job.done("Light Mode");
break;
case "auto":
setMode(0);
job.done("Auto UI Mode");
break;
default:
job.done("Something is wrong! Please go to Preferences (hold app icon)");
}
}
While I think most of them are supposed to define "app wide" configuration, the documentation for some of them makes it's not clear what it can and can not do.
But the documentation well establishes that at least one of them is supposed to update the system setting (UiModeManager.setNightMode();
)
https://developer.android.com/reference/android/app/UiModeManager#setNightMode(int)
Sets the system-wide night mode.
Changes to night mode take effect globally and will result in a configuration change (and potentially an Activity lifecycle event) being applied to all running apps. Developers interested in an app-local implementation of night mode should consider using setApplicationNightMode(int) to set and persist the -night qualifier locally or AppCompatDelegate.setDefaultNightMode(int) for the backward compatible implementation.
But it doesn't seem to work.
What surprises me is that there is no mention of Night/Dark mode in Settings.System
's which is where I expected the setting to be.
Note that the manifest specifies these permissions need and that they were granted.
<uses-permission android:name="android.permission.WRITE_SETTINGS" tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" tools:ignore="ProtectedPermissions" />
And in the <activity>
android:configChanges="uiMode"
Tested on a true Samsung S8 (android 9) and Pixel 3a (android 10) and other android 10 virtual devices.
If anyone knows how to change system dark mode setting or knows that it is in fact not possible, please let me know !
See Question&Answers more detail:
os