Paul Franco
Posted on July 17, 2024
On Android, determining how many users are using dark mode can be achieved through a few methods, but it generally involves collecting analytics data from your app. Here's how you can approach it:
Using Firebase Analytics
Firebase Analytics is a powerful tool for collecting user data and behavior analytics. You can log a custom event to track the theme mode preference of your users.
Add Firebase to your Android project: Follow the Firebase setup instructions to add Firebase to your project.
Log the dark mode preference: Create a function to log an event whenever the user changes the theme or when the app starts. Here's an example of how you can do it:
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase
fun logThemeMode(isDarkMode: Boolean) {
val firebaseAnalytics = Firebase.analytics
val themeMode = if (isDarkMode) "dark" else "light"
val bundle = Bundle().apply {
putString("theme_mode", themeMode)
}
firebaseAnalytics.logEvent("user_theme_mode", bundle)
}
- Detect the current theme mode: You can detect whether the user is in dark mode using the following code:
val isDarkMode = when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_YES -> true
Configuration.UI_MODE_NIGHT_NO -> false
else -> false
}
logThemeMode(isDarkMode)
- View analytics in Firebase: After integrating the above code, you can view the analytics data in the Firebase console under the Events section. Look for the user_theme_mode event to see the theme mode distribution among your users.
Using Custom Analytics
If you are using a custom analytics solution, you can follow a similar approach by sending a custom event or property to your analytics server whenever the theme changes or the app starts.
Detect the current theme mode: Use the same method as above to detect whether the user is using dark mode.
Send the theme mode data: Send this information to your analytics server. The implementation will depend on your custom analytics setup.
Checking System Default
You can also check the default system settings for dark mode, though this will only give you an idea of the preferred mode and not actual usage:
val nightModeFlags = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
val isDarkMode = when (nightModeFlags) {
Configuration.UI_MODE_NIGHT_YES -> true
Configuration.UI_MODE_NIGHT_NO -> false
else -> false
}
Combining these methods with your analytics setup will provide you with insights into how many users are using dark mode on their Android devices.
Posted on July 17, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.