How can I detect programmatically if the Android Device is in Dark Mode?

前端 未结 2 740
忘掉有多难
忘掉有多难 2021-02-08 06:21

I\'m trying to support the Android Q Dark theme for my Android app and I can\'t figure out how to import different assets based on the theme I\'m currently in.

Im using

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-08 07:08

    You first need to do this changes in manifest

    
    

    then onConfigurationChanged of activity

    val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
    when (currentNightMode) {
        Configuration.UI_MODE_NIGHT_NO -> {} // Night mode is not active, we're using the light theme
        Configuration.UI_MODE_NIGHT_YES -> {} // Night mode is active, we're using dark theme
    }
    

提交回复
热议问题