5

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

  • Hi. Have you already checked out this question? This might be a duplicate. https://stackoverflow.com/questions/57175226/how-to-disable-night-mode-in-my-application-even-if-night-mode-is-enable-in-andr/57175501 – Daniel Sep 24 '20 at 20:48
  • 1
    Yeah, I see, our problems are almost same, but I am not using Theme.AppCompat.DayNight as application base theme. And the solution provided there, AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) - doesn't work for me. – Sudipta Debnath Sep 25 '20 at 04:14

3 Answers3

4

You can use this in all application

class AppController: Application() {
   override fun onCreate() {
      super.onCreate()
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
   }
}
Ziad
  • 116
  • 6
  • This worked and is apparently the best way to do this. See https://medium.com/androiddevelopers/appcompat-v23-2-daynight-d10f90c83e94 – Joshua Pinter Jul 25 '23 at 18:13
1

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>
saed
  • 43
  • 1
  • 6
0

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

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
  • Hello and welcome to SO! Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) For example, you can add a working example based on the question details. – Tomer Shetah Dec 21 '20 at 08:28