Android Prevent SwitchPreference Change Value But Allow Click

Apr 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}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.