Comments (2)
It does work with PreferenceFragmentCompat(), but I'm doing it slightly differently than you. Try it out.
class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
private lateinit var sharedPref: SharedPreferences
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
sharedPref = requireActivity().getSharedPreferences(MAIN_PREFERENCES, Context.MODE_PRIVATE)
}
override fun onResume() {
super.onResume()
preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}
override fun onPause() {
super.onPause()
preferenceScreen.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
when (key) {
"theme" -> {
.
.
.
}
"language" -> {
val languageCode = sharedPreferences?.getString("language", "nl") ?: "nl"
val editor: SharedPreferences.Editor = sharedPref.edit()
editor.putString(LANGUAGE_KEY, languageCode)
editor.apply()
updateLanguage(languageCode)
updatePreference(key)
}
else -> {
.
.
.
}
}
}
private fun updatePreference(key: String) {
val pref: Preference? = findPreference(key)
if (pref is ListPreference) {
val listPref: ListPreference = pref
pref.value?.let {
pref.setSummary(listPref.entry)
if (key.equals("language", true)) {
.
.
.
}
}
}
}
private fun updateLanguage(languageCode: String) {
(activity as? LocalizationActivity)?.setLanguage(languageCode)
.
.
.
}
}
from localization.
Great, I have implemented it in a similar way now, thanks for the detailed answer!
from localization.
Related Issues (20)
- getDefaultLanguage should have access to a context.
- AppCompat's DayNight theme does not apply when toggle the dark mode in v1.2.10 HOT 1
- App crashes first time using web view after setting a new language in the application. HOT 11
- it is make hyper pay it online payment get way stop ! HOT 2
- LocalizationActivity >= 1.2.6 does not work with ViewPump
- ANR issue when getString() HOT 1
- App crashes when using WebView even after updating to v1.2.10 HOT 4
- In Activity, onConfigurationChanged function always return one orientation information while rotate the device. HOT 1
- Change Language from Fragment HOT 2
- Library doesn't work in Google Play Internal Testing HOT 1
- Version 1.2.10 - ClassCastException in specific devices (Lenovo, Asus) HOT 4
- Version 1.2.9 => Getting ANR issue in LocalizationActivityDelegate.kt file. HOT 5
- Not working in combination with dagger hilt HOT 3
- java.lang.RuntimeException: Using WebView from more than one process at once HOT 3
- Version 1.2.10 => Caused by android.os.BadParcelableException: ClassNotFoundException when unmarshalling: cf HOT 11
- call setLanguage inside Fragment
- Theme change not working HOT 1
- Crash on checkLocaleChange HOT 1
- Crash - Could not find an Application in the given context [ Android Hilt ] HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from localization.