Skip to content

Instantly share code, notes, and snippets.

@HugoMatilla
Last active July 8, 2021 05:08
Show Gist options
  • Save HugoMatilla/e2f591f56f95fb8d9f53e6ca9b5a3c47 to your computer and use it in GitHub Desktop.
Save HugoMatilla/e2f591f56f95fb8d9f53e6ca9b5a3c47 to your computer and use it in GitHub Desktop.
Add a DarkMode Button to your activity
fun addDarkModeButton() {
val darkButton = Button(this).apply {
text = if (getDefaultNightMode() != MODE_NIGHT_YES) "Go Dark " else "Go Light"
layoutParams = LayoutParams(MATCH_PARENT, WRAP_CONTENT)
}
(window.decorView as ViewGroup).addView(darkButton) // or `yourContainer.addView(darkButton)`
darkButton.setOnClickListener {
AppCompatDelegate.setDefaultNightMode(
if (getDefaultNightMode() != MODE_NIGHT_YES) MODE_NIGHT_YES else MODE_NIGHT_NO
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment