Skip to content

Instantly share code, notes, and snippets.

@JakeSteam
Last active March 27, 2019 14:12
Show Gist options
  • Save JakeSteam/6937ef3b6c217a330ec4822dd8c5d1bf to your computer and use it in GitHub Desktop.
Save JakeSteam/6937ef3b6c217a330ec4822dd8c5d1bf to your computer and use it in GitHub Desktop.
Using PreferenceFragment to effortlessly handle user settings https://blog.jakelee.co.uk/using-preferencefragment-to-effortlessly-handle-user-settings/
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
this.supportFragmentManager
.beginTransaction()
.replace(R.id.fragment_frame, PrefsFragment())
.commit()
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Booleans -->
<string name="pref_boolean1">boolean1</string>
<bool name="pref_boolean1_default">false</bool>
<string name="boolean1_title">First boolean</string>
<string name="boolean1_desc">This toggles the first boolean</string>
<string name="pref_boolean2">boolean2</string>
<bool name="pref_boolean2_default">false</bool>
<string name="boolean2_title">Second boolean</string>
<string name="boolean2_desc">This toggles the second boolean</string>
<!-- Strings -->
<string name="pref_string1">string1</string>
<string name="pref_string1_default">FirstDefault</string>
<string name="string1_title">String 1</string>
<string name="string1_desc">Set the first string</string>
<string name="pref_string2">string2</string>
<string name="pref_string2_default">SecondDefault</string>
<string name="string2_title">String 2</string>
<string name="string2_desc">Set the second string</string>
<!-- Int -->
<string name="pref_int1">int1</string>
<integer name="pref_int1_step">1</integer>
<integer name="pref_int1_min">0</integer>
<integer name="pref_int1_max">10</integer>
<integer name="pref_int1_default">10</integer>
<string name="int1_title">Int 1</string>
<string name="int1_desc">The first integer</string>
<string name="pref_int2">int2</string>
<integer name="pref_int2_step">1</integer>
<integer name="pref_int2_min">0</integer>
<integer name="pref_int2_max">10</integer>
<integer name="pref_int2_default">2</integer>
<string name="int2_title">Int 2</string>
<string name="int2_desc">The second integer</string>
</resources>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="@string/pref_show_values"
android:title="@string/show_values_title"
android:icon="@drawable/ic_show_values"
android:summary="@string/show_values_desc"/>
<PreferenceScreen
android:title="@string/open_repo_title"
android:icon="@drawable/ic_repo"
android:summary="@string/open_repo_desc">
<intent android:action="android.intent.action.VIEW"
android:data="@string/repo_url"/>
</PreferenceScreen>
<PreferenceCategory android:title="Booleans">
<SwitchPreference
android:key="@string/pref_boolean1"
android:defaultValue="@bool/pref_boolean1_default"
android:title="@string/boolean1_title"
android:icon="@drawable/ic_one"
android:summary="@string/boolean1_desc"/>
<SwitchPreference
android:key="@string/pref_boolean2"
android:defaultValue="@bool/pref_boolean2_default"
android:title="@string/boolean2_title"
android:icon="@drawable/ic_two"
android:dependency="@string/pref_boolean1"
android:summary="@string/boolean2_desc"/>
</PreferenceCategory>
<PreferenceCategory android:title="Strings">
<EditTextPreference
android:key="@string/pref_string1"
android:defaultValue="@string/pref_string1_default"
android:title="@string/string1_title"
android:icon="@drawable/ic_one"
android:summary="@string/string1_desc"/>
<EditTextPreference
android:key="@string/pref_string2"
android:defaultValue="@string/pref_string2_default"
android:title="@string/string2_title"
android:icon="@drawable/ic_two"
android:summary="@string/string2_desc"/>
</PreferenceCategory>
<PreferenceCategory android:title="Integers">
<SeekBarPreference
android:key="@string/pref_int1"
android:defaultValue="@integer/pref_int1_default"
android:title="@string/int1_title"
android:icon="@drawable/ic_one"
android:summary="@string/int1_desc"/>
<SeekBarPreference
android:key="@string/pref_int2"
android:defaultValue="@integer/pref_int2_default"
android:title="@string/int2_title"
android:icon="@drawable/ic_two"
android:summary="@string/int2_desc"/>
</PreferenceCategory>
</PreferenceScreen>
class PrefsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.preferences_ui)
findPreference(getString(R.string.pref_show_values)).onPreferenceClickListener = showValuesListener
setupSeekbars()
}
private fun setupSeekbars() {
(findPreference(getString(R.string.pref_int1)) as SeekBarPreference).apply {
seekBarIncrement = resources.getInteger(R.integer.pref_int1_step)
min = resources.getInteger(R.integer.pref_int1_min)
max = resources.getInteger(R.integer.pref_int1_max)
}
(findPreference(getString(R.string.pref_int2)) as SeekBarPreference).apply {
seekBarIncrement = resources.getInteger(R.integer.pref_int2_step)
min = resources.getInteger(R.integer.pref_int2_min)
max = resources.getInteger(R.integer.pref_int2_max)
}
}
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) {
getString(R.string.pref_string1) -> {
val pref = findPreference(key) as EditTextPreference
Toast.makeText(activity!!, "String changed to ${pref.text}", Toast.LENGTH_SHORT).show()
}
}
}
private val showValuesListener = Preference.OnPreferenceClickListener { _ ->
val prefHelper = PreferenceHelper(activity!!)
AlertDialog.Builder(activity!!)
.setTitle(R.string.values_title)
.setMessage(String.format(getString(R.string.values_text),
prefHelper.getBooleanPref(PreferenceHelper.BooleanPref.setting1),
prefHelper.getBooleanPref(PreferenceHelper.BooleanPref.setting2),
prefHelper.getStringPref(PreferenceHelper.StringPref.setting1),
prefHelper.getStringPref(PreferenceHelper.StringPref.setting2),
prefHelper.getIntPref(PreferenceHelper.IntPref.setting1),
prefHelper.getIntPref(PreferenceHelper.IntPref.setting2)
))
.setPositiveButton(R.string.values_close) { _, _ -> }
.show()
true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment