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

前端 未结 2 742
忘掉有多难
忘掉有多难 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:14

    Okay finally found the solution I was looking for. As @deepak-s-gavkar points out the parameter that gives us that information is on the Configuration. So, after a small search I found this article that gives this example method that has worked perfectly for what I wanted:

    fun isDarkTheme(activity: Activity): Boolean {
            return activity.resources.configuration.uiMode and
                    Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
        }
    

提交回复
热议问题