multiple dependency in checkboxpreference android

Question!

i have three checkboxes in my preference screen. i want to make the user select only one checkbox at a time. How do i achieve this?

thank you in advance.



Answers

using radio group maybe? :) http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RadioButtons

or if you really need to use checkbox widget, you could check other checkboxes using isChecked() and untick then via setChecked(false) in your OnCheckedChangeListener class implementation

By : Alex


@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
    //mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    mLocationChangedListener = new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference,
                Object newValue) {
            mChbLocationLastChanged = (CheckBoxPreference) preference;
            return true;
        }

    };
    mLocationClickListener = new Preference.OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            setLocationAccuracy();
            return true;
        }
    };
    mChbLocationHigh = (CheckBoxPreference) findPreference("pref_chb_location_high_accuracy_key");
    mChbLocationHigh.setOnPreferenceChangeListener(mLocationChangedListener);
    mChbLocationHigh.setOnPreferenceClickListener(mLocationClickListener);
    mChbLocationBalanced = (CheckBoxPreference) findPreference("pref_chb_location_balanced_power_key");
    mChbLocationBalanced.setOnPreferenceChangeListener(mLocationChangedListener);
    mChbLocationBalanced
            .setOnPreferenceClickListener(mLocationClickListener);
    mChbLocationLowPower = (CheckBoxPreference) findPreference("pref_chb_location_low_power_key");
    mChbLocationLowPower.setOnPreferenceChangeListener(mLocationChangedListener);
    mChbLocationLowPower
            .setOnPreferenceClickListener(mLocationClickListener);
    mChbLocationNoPower = (CheckBoxPreference) findPreference("pref_chb_location_no_power_key");
    mChbLocationNoPower.setOnPreferenceChangeListener(mLocationChangedListener);
    mChbLocationNoPower
            .setOnPreferenceClickListener(mLocationClickListener);
}

private void setLocationAccuracy() {
    boolean isChecked = mChbLocationLastChanged.isChecked();
    String key = mChbLocationLastChanged.getKey();
    if (key.equalsIgnoreCase("pref_chb_location_high_accuracy_key")) {
        mChbLocationBalanced.setChecked(false);
        mChbLocationLowPower.setChecked(false);
        mChbLocationNoPower.setChecked(false);
    } else if (key.equalsIgnoreCase("pref_chb_location_balanced_power_key")) {
        mChbLocationHigh.setChecked(false);
        mChbLocationLowPower.setChecked(false);
        mChbLocationNoPower.setChecked(false);
    } else if (key.equalsIgnoreCase("pref_chb_location_low_power_key")) {
        mChbLocationHigh.setChecked(false);
        mChbLocationBalanced.setChecked(false);
        mChbLocationNoPower.setChecked(false);
    } else if (key.equalsIgnoreCase("pref_chb_location_no_power_key")) {
        mChbLocationHigh.setChecked(false);
        mChbLocationBalanced.setChecked(false);
        mChbLocationLowPower.setChecked(false);
    }
    if (!mChbLocationLastChanged.isChecked())
        mChbLocationLastChanged.setChecked(true);
}


By : Reno


This video can help you solving your question :)
By: admin