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.
You would need to create an onpreferenceChangelistener:
prefChangeListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference == pref1 && newValue.toString().equals("true")){
pref2.setChecked(false);
pref3.setChecked(false);
}
if (preference == pref2 && newValue.toString().equals("true")){
pref1.setChecked(false);
pref3.setChecked(false);
}
if (preference == pref3 && newValue.toString().equals("true")){
pref1.setChecked(false);
pref2.setChecked(false);
}
return true;
}
};
Then to use it,
pref1.setOnPreferenceChangeListener(prefChangeListener);
pref2.setOnPreferenceChangeListener(prefChangeListener);
pref3.setOnPreferenceChangeListener(prefChangeListener);
Or, you can use a ListPreference which will pop up a radio group and you can just put the three option in there. That's what I did in my App, but If the checkboxpreferences are needed, then that of course will work.
Here's my listpreference (for choosing how long it will ring for):
<ListPreference android:persistent="true"
android:title="Ring Time"
android:summary="Choose how long the phone will ring when activated"
android:key="ringTime"
android:defaultValue="ringNot"
android:entries="@array/ringTime"
android:entryValues="@array/ringSetting" />
And here's my XML for the @array/ringTime and @array/ringSetting
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ringTime">
<item>Don\'t Ring</item>
<item>Ring 30 Seconds</item>
<item>Ring 2 Minutes</item>
<item>Ring 5 Minutes</item>
</string-array>
<string-array name="ringSetting">
<item>ringNot</item>
<item>ring30Sec</item>
<item>ring2Min</item>
<item>ring5Min</item>
</string-array>
</resources>
I know this is a 5-month old question as of today, but I thought this might help someone else. :)
And then for implementing the listpreference inside my code:
ringTime = pref.getString("ringTime", "ringNot");//ListPreferences are stored as strings
if (ringTime.equals(ring30Sec))
//ring for 30 seconds
else if (ringTime.equals(ring2Min))
//ring for 2 minutes
else if (ringTime.equals(ring5Min))
//ring for 5 minutes
else Toast.makeText(getBaseContext(), "Not Ringing", Toast.LENGTH_LONG).show();