Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 the official DayNight theme for making the dark/light versions and for drawables is very easy to just point to the XML and it will choose the correct value either from values or values-night depending on what is enabled.

I wanted to do something similar where depending on the theme it would load either the asset "priceTag_light.png" or "priceTag_dark.png".

val inputStream = if(darkIsEnabled) { 
                    assets.open("priceTag_dark.png")
                  } else {
                    assets.open("priceTag_light.png")
                  }

Is there a way I get that flag?

like image 228
Izadi Egizabal Avatar asked Aug 28 '19 04:08

Izadi Egizabal


People also ask

How do I see apps in dark mode?

On your phone, open the Settings app. Tap Display. Turn Dark theme on or off.

How many Android users are using dark mode?

According to a survey carried out by Android Authority with 2,500 Android users, 81.9% use Dark Mode on their phones, in apps and anywhere else available. 9,9% said they switch between Dark and Light Mode. Thus, a total of 91.8% of respondents use some form of Dark Mode on their devices.


1 Answers

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
    }
like image 77
Izadi Egizabal Avatar answered Sep 24 '22 00:09

Izadi Egizabal