Android - Confirm Dialog when changing the preference

2.2k views Asked by At

can anyone suggest me a way to perform AlertDialog to confirm the changes when change the CheckboxPreference or SwitchPrerence, ex. if i want to Uncheck the Preference the AlertDialog should warn them like "Are you sure want to uncheck?" plus the dialog should be customizable because i am gonna use own Layout. i don't want the Default Bluestrip Header with the Dialog.

SwitchPreference pref = (SwitchPreference) findPreference("prefkey_use_passcode");
    pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference arg0, Object arg1) {
            DialogPreference dialog = new DialogPreference(getParent());

            return false;
        }
    });
2

There are 2 answers

4
Peri Hartman On BEST ANSWER

Something like this should work:

      AlertDialog.Builder builder = new AlertDialog.Builder (this);
      builder.setMessage (getString (R.string.are_you_sure));
      builder.setNegativeButton (getString(android.R.string.cancel),
                                 new DialogInterface.OnClickListener()
                                 {
                                   @Override
                                   public void onClick (DialogInterface dialog, int which)
                                   {
                                     finish();
                                   }
                                 });
      builder.setPositiveButton (getString(android.R.string.ok),
                                 new DialogInterface.OnClickListener()
                                 {
                                   @Override
                                   public void onClick (DialogInterface dialog, int which)
                                   {
                                     --- do the work to update the preference ---
                                     finish();
                                   }
                                 });
      builder.create().show();
      return;
0
user1026605 On

I managed to do it like that:

        final CheckBoxPreference myPref = (CheckBoxPreference) findPreference("myPref");
        if (myPref != null) {
            myPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if (newValue instanceof Boolean && ((Boolean) newValue) != PreferencesHelper.getMyPref()) {
                        final boolean isEnabled = (Boolean) newValue;
                        if (!isEnabled) {

                            new AlertDialog.Builder(PreferencesActivity.this)
                            .setTitle("Confirm")
                            .setIcon(R.drawable.alert)
                            .setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    // do nothing
                                }
                            })
                            .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    // OK has been pressed => force the new value and update the checkbox display
                                    PreferencesHelper.setMyPref(false);
                                    myPref.setChecked(false);
                                }
                            }).create().show();

                            // by default ignore the pref change, which can only be validated when OK is pressed
                            return false;

                        }
                    } 
                    return true;
                }
            });
        }

=> PreferencesHelper code

public static boolean getMyPref() {
    return getSharedPreferences().getBoolean("myPref", true);
}

public static void setMyPref(boolean newValue) {
    Editor edit = getEditor();
    edit.putBoolean("myPref", newValue);
    edit.apply();   
}