Android Prevent SwitchPreference Change Value But Allow Click

April 19, 2018
Cannot use preference.setEnabled(false)

preference.isEnabled = false (setEnabled) cannot be used as it disabled the click event as well.

Instead, listen to the click and switch back to original value everytime the value is changed.

val pref = findPreference("pref_key")
pref.setOnPreferenceClickListener {
    Toast.makeText(activity, "Click Detected", Toast.LENGTH_LONG).show()
    (it as SwitchPreference).isChecked = false
    true
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.