Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable night mode in my application even if night mode is enable in android 9.0 (pie)?

Tags:

android

People also ask

How do you disable night mode in my application even if night mode is enable?

Turn Dark theme on or off in your phone's settingsOn your phone, open the Settings app. Tap Display. Turn Dark theme on or off.

How do I turn off dark mode for certain apps on Android?

Tap the hamburger menu in the top-right (Android) or bottom-right (iOS) corner, scroll down and select Settings & Privacy > Dark Mode. You can then turn it on or off, or make the app dependent on your phone's system-wide theme.


You can put this, at the first line in the onCreate method of your launcher activity.

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

Tested using Xiaomi Note 8 Pro MIUI 12.0.2 Android 10

Adding AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); to MainActivity.onCreate(); doesn't seems to be working

The working solution was to add <item name="android:forceDarkAllowed">false</item> inside our main AppTheme block in styles.xml

Example:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>

Update for InputMethodService :

For InputMethodService / Maybe if we inflate layout in any Service
I found when in Dark Mode - Xiaomi MIUI 12.0.3 there's a Black color which gets inverted into White.
To prevent the inversion, so Black color will still be Black be sure to set a theme in InputMethodService.

At onCreate() before super.onCreate() call these methods

  • getApplication().setTheme()
  • setTheme()

For your information, I'm passing Light Theme (Theme.MaterialComponents.Light.NoActionBar) which have android:forceDarkAllowed=false.
In my case for InputMethodService just calling getApplication().setTheme() is not enough to prevent the inversion.


React Native, Ionic (cordova, capacitor)

Actually, this answer is especially for React Native developers/apps:

Just like all of us know, the dark mode is fully available on Android 10

Dark theme is available in Android 10 (API level 29) and higher

And most likely this feature ruined your app design implementation, like SVG, font, and background colors. if you decided to fully disable force dark mode you should follow this approach:

  1. Append the following code inside your <style> tag in the styles.xml file:

    <item name="android:forceDarkAllowed">false</item>
    

    Note: file path: android/app/src/main/res/values/styles.xml

  2. After step 1 you shoule have something like below:

    <resources>
      <!-- Base application theme. -->
      <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
          ...
          <item name="android:forceDarkAllowed">false</item>
      </style>
    </resources>
    
  3. Maybe the above stpes don't help you but don't worry, just like is said in the top of this post, this feature is released on API level 29, this means you should change the compiledSdkVersion to 29 or higher.

  4. To Change the compiledSdkVersion you should edit the the compiledSdkVersion in the app gradle properites existed in the android/app/build.gradle file:

    android {
        compileSdkVersion 29
    
  5. Maybe you face compileSdkVersion rootProject.ext.compileSdkVersion, don't worry you should change this in the android gradle properites existed in the android/build.gradle file:

    buildscript {
        ext {
            buildToolsVersion = "SomeVersion"
            minSdkVersion = SomeVersion
            compileSdkVersion = 29 // <======= this should be 29 or higher
            targetSdkVersion = SomeVersion
    

Hint: For being sure that you have a new build, completely delete your last build by using the rm -rf android/app/build command and uninstall the app on your Emulator/Device and then run yarn android again.


In themes.xml change:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

to

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

If you want to avoid Activity recreations you could set the flag on the Application class as mentioned here

I recommend setting it in your application class (if you have one) like so:

public class MyApplication extends Application {
    
    public void onCreate() {
        super.onCreate();
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
}

What suggested by Ali Rezaiyan is the correct way to proceed if you want disable the night mode all over your app.

Sometimes you just need to disable the night mode on some activities or fragment only (maybe because of legacy stuff).

So you can choose what to do:

  • Disable all over the app:

    • AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
  • Disable for a single Activity:

    • getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    • After calling this you probably need to call recreate() for this to take effect