Created
April 8, 2020 09:56
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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