I'm trying to remove a preference from the screen so it isn't visible if the user is using an SDK greater than 2.2. I've found several answers saying that getPreferenceScreen().removePreference(thePreference) will work, but I'm getting FALSE returned every time I try it. Am I using it in the wrong place? Any clue from my code?
public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
private static final String POLLING_PREFERENCE = "update_frequency_list";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
// Get a reference to the preferences
mPollPref = getPreferenceScreen().findPreference(POLLING_PREFERENCE);
//If the SDK is 2.2 or greater do not display polling preferences (using C2DM instead)
if(Build.VERSION.SDK_INT > 7) {
getPreferenceScreen().removePreference(mPollPref);
}
}
....
}
findPreference("yourpref"). setEnabled(false);
This settings screen is used to manage the preferences of the users. For creating this settings screen android provides a feature to make a settings preferences screen.
Okay the issue in my case was that my preferences were separated into categories and when the preferences are categorized you can't simply ".removePreference" like we were trying to do. I first had to reference the category containing the preference I wanted to remove and then remove that preference from that category like so..
public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
private static final String POLLING_PREFERENCE = "update_frequency_list";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
// Get a reference to the preferences
mPollPref = getPreferenceScreen().findPreference(POLLING_PREFERENCE);
//If the SDK is 2.2 or greater do not display polling preferences (using C2DM instead)
if(Build.VERSION.SDK_INT > 7) {
PreferenceCategory notificationsCategory = (PreferenceCategory) findPreference("notifications_category");
notificationsCategory.removePreference(mPollPref);
}
}
....
}
I assume this has something to do with the ability to have multiple preferences with the same key (in order to remove the correct one). Anyway thank you Vladimir for sticking with me. Definitely wouldn't have found the answer without you.
//remove all preferences
mPollPref = getPreferenceScreen();
mPollPref.removeAll();
//Change language
setApplicationLanguage();
//and rebuild
addPreferencesFromResource(R.xml.preferences);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With