3

If I disable using setEnabled(false) the switch is (1) set to off and (2) I don't get a response from the OnClickListener to launch a dialog.

Edit: In this case, I don't want the switch to automatically switch from on to off.

Has anyone dealt with this problem?

user2892437
  • 1,111
  • 1
  • 14
  • 22

2 Answers2

6

The solution was to use setChecked(true) inside the OnPreferenceClickListener callback then launch the dialog.

Note: this did not work inside of the OnPreferenceChangeListener callback.

frogatto
  • 28,539
  • 11
  • 83
  • 129
user2892437
  • 1,111
  • 1
  • 14
  • 22
2

For those who want the code :

if (!BuildConfig.FULL_VERSION) {
            SwitchPreferenceCompat preference = (SwitchPreferenceCompat) findPreference(getString(R.string.pref_notifications_service_key));
            preference.setDefaultValue(false);
            preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    ((SwitchPreferenceCompat) preference).setChecked(false);
                    new ProFeatureDialogFragment().show(getFragmentManager(), "PRO_FEATURE_TAG");
                    return false;
                }
            });
        }
J-Jamet
  • 847
  • 8
  • 17