Skip to content

Instantly share code, notes, and snippets.

@shubham08gupta
Created April 8, 2020 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shubham08gupta/1b48e7e821554aba0ba14be38ac0ac90 to your computer and use it in GitHub Desktop.
Save shubham08gupta/1b48e7e821554aba0ba14be38ac0ac90 to your computer and use it in GitHub Desktop.
A sample to give the option to switch the theme b/w the light and dark mode
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="theme_options_key">
<item>Light</item>
<item>Dark</item>
<item>Set by battery saver</item>
</string-array>
<string-array name="theme_options_values">
<item>light</item>
<item>dark</item>
<item>default</item>
</string-array>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="theme_options_key">
<item>Light</item>
<item>Dark</item>
<item>System default</item>
</string-array>
</resources>
class MyApplication : Application(){
override fun onCreate() {
super.onCreate()
applyTheme()
}
private fun applyTheme() {
PreferenceManager.getDefaultSharedPreferences(this)
.getString(getString(R.string.key_app_theme), ThemeHelper.DEFAULT_MODE)?.apply {
// we need to re-apply the theme since the system does not saves the
// information about our theme
ThemeHelper.applyTheme(this)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2" tools:ignore="MissingTranslation">
<string name="key_app_theme"><xliff:g id="preference_key">selected_app_theme</xliff:g></string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<ListPreference
app:defaultValue="default"
app:entries="@array/theme_options_key"
app:entryValues="@array/theme_options_values"
app:iconSpaceReserved="false"
app:key="@string/key_app_theme"
app:title="@string/title_choose_theme"
app:useSimpleSummaryProvider="true" />
</PreferenceScreen>
import android.os.Bundle
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.unifa_e.bus.driver.R
import com.unifa_e.bus.driver.common.ThemeHelper
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
listenAppThemeChanges()
}
/***
* applies the new theme whenever there is a change
*/
private fun listenAppThemeChanges() {
findPreference<ListPreference>(getString(R.string.key_app_theme))?.apply {
onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
ThemeHelper.applyTheme(newValue as String)
true
}
}
}
}
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
object ThemeHelper {
//the values must be same as defined in arrays.xml
private const val LIGHT_MODE = "light"
private const val DARK_MODE = "dark"
const val DEFAULT_MODE = "default" // follow system settings
fun applyTheme(theme: String) {
val mode = when (theme) {
LIGHT_MODE -> AppCompatDelegate.MODE_NIGHT_NO
DARK_MODE -> AppCompatDelegate.MODE_NIGHT_YES
else -> {
// Android 10 and above supports Dark theme by default
// For others, Battery saver mode switches the system to Dark theme
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
} else {
AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
}
}
}
AppCompatDelegate.setDefaultNightMode(mode)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment