0

In my settings Fragment I have a password protected SwitchPreference. I use it to unlock an "Edit mode" in the app. Not everyone should be allowed into this mode so I've put a password on it. However, this causes a problem.

Initially clicking the SwitchPreference will switch it to the "ON" position, which will trigger my onSharedPreferenceChanged method. However, if the person gets the password wrong, it will then switch it to the "OFF" position, which will then trigger my onSharedPreferenceChanged method again!

So the problem is that I'm listening for when the SwitchPreference gets changed, but I should be listening for when it gets clicked. A quick search on StackOverflow or Google will tell you that you shouldn't be trying to listen for a click, and should listen for the onSharedPreferenceChanged. However, as I said, this doesn't suit my needs.

Does anyone know another method I can override here that will let me listen for when the SwitchPreference is clicked?

My goal is to listen for a click, if its switch is "ON", then just turn if "OFF". But if it's "OFF", then popup a password dialog. If the password is correct, set the switch to "ON", otherwise leave it "OFF".

Thanks

eoinzy
  • 2,152
  • 4
  • 36
  • 67
  • You could use (https://developer.android.com/reference/android/preference/TwoStatePreference.html#onClick%28%29)[this] maybe. But **beware** if you use click for `Switches` in Android, because you can switch these by dragging, and this won't trigger click events. – Gent Ahmeti Aug 12 '16 at 11:30
  • You could disable your preference per default and enable it via setEnabled(true) if the state is ON or the correct password is entered – Patrick Piatkowski Aug 12 '16 at 12:08
  • That might be an option actually. Just use `setEnabled(false)` all the time and just listen for the `onClick` if possible. Although visually it might not be perfect. – eoinzy Aug 15 '16 at 16:08

0 Answers0