sai ram
sai ram

Reputation: 2139

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

I created my application before the android pie has been released, in every layout I put android: background = "white" it works fine in every device, but when my brother installed the application and enabled night mode my application becomes a disaster with one single move, everything turns into back and white movie.

<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/COLOR_PRIMARY</item>
    <item name="colorPrimaryDark">@color/COLOR_PRIMARY</item>
    <item name="colorAccent">@color/COLOR_PRIMARY</item>
    <item name="cardViewStyle">@style/CardView</item>
    <item name="android:fontFamily">@font/helvetica</item>
</style>

my color primary is red.

Upvotes: 213

Views: 217349

Answers (30)

Arash Torkaman
Arash Torkaman

Reputation: 67

in your themes folder themes.xml (night) change this :

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

to this

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

and add this in block style if your using api 29 or above

<item name="android:forceDarkAllowed" tools:target="Q">false</item>

Upvotes: 0

M Azam Khan
M Azam Khan

Reputation: 380

I'm using the kotline. In my case, I've added the following lines in the below of oncreate method

in an activity class and

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

 }

and in the value->styles->Themes/style class, I add the following code

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

Upvotes: 3

Shahnazi2002
Shahnazi2002

Reputation: 89

This is an easy way:

Copy the content of the first file located at res>values>themes and paste it into the second file in the same directory.

values

Upvotes: -2

Amos Banda
Amos Banda

Reputation: 21

simply deleting the darkmode theme from themes folder worked for me.

Upvotes: 2

OrenDinur
OrenDinur

Reputation: 131

Change the DayNight to Light in AppTheme in the styles.xml file.

Upvotes: 9

zimmy9537
zimmy9537

Reputation: 153

create a base application class which inherits Application class and inside the override oncreate method put this line:-

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

also don't forget to add this under name tag in android manifest.

Upvotes: 11

Divyesh Kevadiya
Divyesh Kevadiya

Reputation: 51

I have this force night mode problem in redmi note 7 pro. But, when I use

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

this method in my application level class and the whole app theme was changed as light theme but actually I wanted the theme as night too.

And I found solution for particular view as:

android:forceDarkAllowed="false"

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white_black"
    android:elevation="@dimen/dim4"
    android:forceDarkAllowed="false"
    android:forceHasOverlappingRendering="false"
    app:itemIconTint="@drawable/highlighted_bottom_nav_item"
    app:itemTextAppearanceActive="@style/BottomNavigationView.Active"
    app:itemTextAppearanceInactive="@style/BottomNavigationView"
    app:itemTextColor="@drawable/highlighted_bottom_nav_item"
    app:labelVisibilityMode="labeled"
    app:menu="@menu/bottom_nav_menu" />

And just fixed it.

Upvotes: 0

Bhavin Solanki
Bhavin Solanki

Reputation: 510

Do not Use below code.

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

This will call your activity twice.

Just simply Use this

In your Theme file

First make Style parent as Light

Theme.MaterialComponents.Light.NoActionBar

Then add this Line Under your style

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

So your code is look like this.

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/primary_color_app</item>
        <item name="colorPrimaryVariant">@color/status_bar_color</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>

        <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

        <!-- Customize your theme here. -->
    </style>

Note : If you have android studio updated version make sure you need to make style same for Night Theme. Same Color as Light theme

Upvotes: 9

paul iyinolu
paul iyinolu

Reputation: 75

After adding <item name="android:forceDarkAllowed">false</item> to the styles.xml file, dont forget to change parent theme from

Theme.AppCompat.DayNight.NoActionBar

to

Theme.AppCompat.Light.NoActionBar

uninstall app from the emulator, run cd android && ./gradlew clean and it should be working fine.

Upvotes: 1

user14415508
user14415508

Reputation:

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 steps 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 compileSdkVersion to 29 or higher.

  4. To Change the compileSdkVersion you should edit the the compileSdkVersion 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.

Upvotes: 110

Usman Khan
Usman Khan

Reputation: 100

Change both Light and Dark themes to the same colours. That will not affect output even if the device is in dark mode.

Inside both value/theme.xml and night/theme.xml

Change the following line

<style name="Theme.Labmuffles" parent="Theme.MaterialComponents.DayNight.NoActionBar">

to

<style name="Theme.Labmuffles" parent="Theme.MaterialComponents.Light.NoActionBar">

The app will be in dark mode but even in the dark mode it will display colours of the light mode and it will stay always light.

Upvotes: 3

farhad.kargaran
farhad.kargaran

Reputation: 2373

Never add this code:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

If you put it in your oncreate Method of MainActivity, the onCreate method will called twice!!! It take me an hour to find out that adding this line caused the problem!

just add this line in your style.xml:

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

it looks like this in my code:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Upvotes: 4

anon
anon

Reputation:

Just Modify the few changes below in your project (themes.xml):

If your Android studio has the latest version, then apply this on both "themes.xml" and "themes.xml(night)".

Change:

<style name="Theme.demo" parent="Theme.MaterialComponents.DayNight.NoActionBar">`  

To:

<style name="Theme.demo" parent="Theme.MaterialComponents.Light.NoActionBar">

And then just add the below line:

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

Upvotes: 7

Shubham
Shubham

Reputation: 41

Go and add:

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

in your theme, even if dark mode is enabled in you're device. Light theme is applied in your application.

Upvotes: 4

B. Mohammad
B. Mohammad

Reputation: 2464

for me I had to add <item name="android:forceDarkAllowed">false</item> in the Theme.App.SplashScreen style also, Im using expo bare.

<resources xmlns:tools="http://schemas.android.com/tools">
  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:textColor">#000000</item>
    <item name="android:forceDarkAllowed">false</item>
  </style>
  <style name="Theme.App.SplashScreen" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Below line is handled by '@expo/configure-splash-screen' command and it's discouraged to modify it manually -->
    <item name="android:windowBackground">@drawable/splashscreen</item>
    <item name="android:forceDarkAllowed">false</item>
    <!-- Customize your splash screen theme here -->
  </style>
</resources>

Upvotes: 2

Murilo Dutra
Murilo Dutra

Reputation: 150

This worked for me, I changed from <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar"> to <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">.

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

All code:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:textColor">#000000</item>
</style>

Upvotes: 14

Ganesh MB
Ganesh MB

Reputation: 1179

Just add

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

after

super.onCreate(savedInstanceState);

on every Activity

Next add

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

in your Theme.

Upvotes: 5

Tarique Anowar
Tarique Anowar

Reputation: 130

This line of code works for me.

<!--To disable dark mode-->
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

Upvotes: 3

Ali Rezaiyan
Ali Rezaiyan

Reputation: 3309

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

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

Upvotes: 220

Vikash Sharma
Vikash Sharma

Reputation: 539

I have gone through a lot of solutions but none was robust until I removed the "values-night" folder from the "res" folder.

And change use parent theme: <style name="Theme.CryptoKite" parent="Theme.MaterialComponents.Light.DarkActionBar">

Upvotes: 3

Ilya Andreev
Ilya Andreev

Reputation: 21

In addition to Fauzi Danartha's answer to IMS part. One can extend theme with

<item name="android:forceDarkAllowed" tools:targetApi="q"> false </item>

not from material design's themes but from android:Theme.DeviceDefault.InputMethod (since targetSdkVersion 14).

Also calling getApplication().setTheme() is not necessary, just setTheme() is enough.

Checked on MIUI 12.0.4.

Upvotes: 2

Abubakar
Abubakar

Reputation: 141

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

change both themes DayNight to Light

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

Upvotes: 22

Ehsan Rosdi
Ehsan Rosdi

Reputation: 989

This worked for me (april 2021)

  1. add this line under your theme:

<item name="android:forceDarkAllowed" tools:targetApi="q"> false </item>

  1. delete themes.xml (night) file (under values folder)

Upvotes: 23

Mikepote
Mikepote

Reputation: 6553

Another option for Cordova only developers:

From https://developer.android.com/guide/webapps/dark-theme

"WebView can't differentiate based solely on the presence of the media query, so it needs a color-scheme meta tag with dark value as indication that a web page has its own dark theme. To prevent double-darkening (that is, applying color inversion on top of media query customization) WebView evaluates @media (prefers-color-scheme: dark) to false in this mode."

So I added <meta name="color-scheme" content="dark"> to my html and presto - it now detects my prefers-color-scheme: dark query and doesnt apply auto darkening.

Upvotes: 2

Rishabh Gupta
Rishabh Gupta

Reputation: 119

using the following line can help you to not overload dark mode in your app.

android:forceDarkAllowed="false"

Example of usage,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF"
android:forceDarkAllowed="false"
tools:context=".SplashActivity">

Upvotes: 2

Fauzi Danartha
Fauzi Danartha

Reputation: 1819

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.

Upvotes: 160

MatPag
MatPag

Reputation: 44907

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

Upvotes: 27

Mirza Ali
Mirza Ali

Reputation: 221

Simply Change this

<style name="YourThemeName" parent="Theme.MaterialComponents.DarkActionBar">

To

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

In both theme and theme night

and make sure all colors are same in the theme and theme night

Upvotes: 0

Donovan Phoenix
Donovan Phoenix

Reputation: 1511

In themes.xml change:

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

to

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

Upvotes: 123

Roya Rastgar
Roya Rastgar

Reputation: 153

in layout of your activity that you want to disable force dark, place following attribute in parent layout:

android:forceDarkAllowed="false"

your layout should be like this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:forceDarkAllowed="false">

<!-- ..... -->

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 8

Related Questions