Android - How to detect if night mode is on when using AppCompatDelegate.MODE_NIGHT_AUTO

前端 未结 5 685
天涯浪人
天涯浪人 2020-12-01 02:53

I\'m using Androids built in day/night mode functionality and I\'d like to add an option to my app for AppCompatDelegate.MODE_NIGHT_AUTO

I\'m having a p

5条回答
  •  醉话见心
    2020-12-01 03:15

    If you are kotlin developer then you can use below code to judge dark mode.

     val mode = context?.resources?.configuration?.uiMode?.and(Configuration.UI_MODE_NIGHT_MASK)
        when (mode) {
            Configuration.UI_MODE_NIGHT_YES -> {}
            Configuration.UI_MODE_NIGHT_NO -> {}
            Configuration.UI_MODE_NIGHT_UNDEFINED -> {}
        }
    

    For more about dark theme mode

    https://github.com/googlesamples/android-DarkTheme/

提交回复
热议问题