7

I want to implement android 10 dark theme in my app , I have these following cases:

SYSTEM_DEFAULT, NIGHT_MODE, LIGHT_MODE

The problem is when I change theme from night or light to system_default from inside the app and it can not understand whether the system is in light mode or dark mode. so the theme won't be updated.

I've tried the dark theme by google https://developer.android.com/guide/topics/ui/look-and-feel/darktheme

and implementing the configuration still won't be good for me because I don't want to recreate my activity if user changes day to system default when system default is day.

Is there anyway I could handle this?

when(id) {
  NIGHT - > theme = Theme.NIGHT_MODE
  DAY - > theme = Theme.LIGHT_MODE
  SYSTEM_DEFAULT - > theme = Theme.SYSTEM_DEFAULT
}

context ? .clearCachedDrawables()
activity ? .recreate()
}

EDIT:

when (themeStatus) {
            Theme.LIGHT_MODE ->
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            Theme.NIGHT_MODE ->
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
            Theme.SYSTEM_DEFAULT ->
               AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
        }
Maryam Mirzaie
  • 536
  • 6
  • 24

1 Answers1

7

You don't need to set the theme in your activity and recreate it. It's done automatically if you've setup your app theme right.

To use the Dark in your app, you should extend the DayNight theme as your app theme.

<style name="AppTheme" parent="Theme.AppCompat.DayNight"> 

or

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">

If you want for example a different color during dark mode, you should create a 'Android resource directory' called values-night with a 'resource file called colors.xml

In the colors.xml you can set a different color hex for one of your existing colors.

For example:

values/colors.xml contains

<color name="myColor">#000000</color>

values-night/colors.xml contains

<color name="myColor">#FFFFFF</color>

EDIT

You can switch between Dark/light mode in app by calling

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)

or

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

or

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
Luciano
  • 2,691
  • 4
  • 38
  • 64
  • 2
    I have already done this , the problem is when i change my theme from light to system default and system is nigh mode , the app won't be updated to nigh mode although it recreates the activity – Maryam Mirzaie Sep 17 '19 at 07:41
  • @MaryamMirzayee so your goal is to change between light and dark mode in you app instead of normal way by changing this in android settings? Otherwise why are you changing the theme from light to system default. – Luciano Sep 17 '19 at 08:26
  • 2
    yes , my goal is to give user the options i mentioned above(light,dark,system_default) , I want the user to choose system default and my app should apply the state which system default is at right at that time – Maryam Mirzaie Sep 17 '19 at 08:33
  • @MaryamMirzayee sorry i misunderstood your question, so i've updated my answer with a solution how to change the dark/light mode in app. – Luciano Sep 17 '19 at 08:50
  • 3
    I already did that too , as I said `AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)` this code doesn't work. The app won't be notifed when the theme changes from `MODE_NIGHT_YES` or `MODE_NIGHT_NO` to `MODE_NIGHT_FOLLOW_SYSTEM` – Maryam Mirzaie Sep 17 '19 at 10:51
  • @Luciano Can you tell me my -night color is not capturing always its showing black color in night mode. – Mohit Suthar Feb 07 '20 at 12:34
  • 3
    I have the same problem even with stable AppCompat 1.1.0 release – Andre Thiele May 25 '20 at 11:56