Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove Android preferences from the screen

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);
    }
}
....
}
like image 728
honeal Avatar asked Nov 02 '10 19:11

honeal


People also ask

How do I turn off preferences on Android?

findPreference("yourpref"). setEnabled(false);

What is preference screen?

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.


2 Answers

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.

like image 170
honeal Avatar answered Nov 11 '22 07:11

honeal


//remove all preferences
    mPollPref = getPreferenceScreen();
    mPollPref.removeAll();

//Change language
    setApplicationLanguage();

//and rebuild
    addPreferencesFromResource(R.xml.preferences);
like image 39
Openssl3343 Avatar answered Nov 11 '22 09:11

Openssl3343