How to disable automatic change of day night theme based on android system settings?

4.2k Views Asked by At

So far I have learnt from google, you have to use Theme.Appcompat.Daynight or Material Daynight theme to make your app support dark mode. And you need to use different styles.xml in values and values-night directories. I am not willing to make my app support dark theme. But when I change android system theme (from notification panel) to dark, my app becomes dark. I am using Theme.Appcompat.Light as base theme, did nothing for my app to support dark mode, still my app becomes dark. I am using cardview for the very first time, don't know if it may be the cause since I am very new in android programming. Your little help will be much welcome. Please, check screenshots here:

System dark mode off

System dark mode on

3

There are 3 best solutions below

0
On

in android 10 ,android forse dark to app to pervent that , add below to your defult theme in style :

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

You can use this in all application

class AppController: Application() {
   override fun onCreate() {
      super.onCreate()
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
   }
}
1
On

Add this line inside onCreate method of the MainActivity before calling setContentView:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)