How to remove a preference from PreferenceActivity?

I’m using PreferenceActivity. How do I remove a preference? I cannot seem to get this to work:

Preference p = findPreference("grok");
boolean worked = getPreferenceScreen().removePreference(p);
// worked == false.

So the preference is found, but the removePreference() call fails. What’s the right way to do this? I’m using a preference.xml file for the keys like so:

  • Using drawable resources
  • Is using Scala on Android worth it? Is there a lot of overhead? Problems?
  • DDMS Not Showing Threads From Device
  • Activity OnDestroy never called?
  • Android: Goto HTTP Url on Button Click
  • What is the use of BaseColumns in Android
  • <PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <PreferenceCategory
            android:title="foo">
    
            <CheckBoxPreference
                android:key="grok" />
    
                ...
    

    Thanks

  • View SQLite database on device in Android Studio
  • Missing or Incorrect images and backgrounds randomly throughout app lifecycle
  • Android — Add new tab when button click like Google Chrome new button?
  • Marquee Set Speed
  • Android app for phone and tablet: 1 or 2 apps?
  • GCMRegistrar.getRegistrationId(this) always returns empty string, even after successful call to register
  • 2 Solutions collect form web for “How to remove a preference from PreferenceActivity?”

    you can remove only exact child in PreferenceGroup. So in your case, you should add some key to PreferenceCategory (with title=”foo”), then findPreference with this key & then remove it child

    XML:

    <PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <PreferenceCategory
        android:key="category_foo"
        android:title="foo">
    
        <CheckBoxPreference
            android:key="grok" />
    
            ...
    

    Code:

    Preference p = findPreference("grok");
    // removing Preference
    ((PreferenceGroup) findPreference("category_foo")).removePreference(p);
    

    Instead of setting multiple ids, you can get the entire tree of preferences and find the parent of any preference, and then remove any of its children preferences:

    public static Map<Preference,PreferenceGroup> buildPreferenceParentTree(final PreferenceActivity activity)
        {
        final Map<Preference,PreferenceGroup> result=new HashMap<Preference,PreferenceGroup>();
        final Stack<PreferenceGroup> curParents=new Stack<PreferenceGroup>();
        curParents.add(activity.getPreferenceScreen());
        while(!curParents.isEmpty())
          {
          final PreferenceGroup parent=curParents.pop();
          final int childCount=parent.getPreferenceCount();
          for(int i=0;i<childCount;++i)
            {
            final Preference child=parent.getPreference(i);
            result.put(child,parent);
            if(child instanceof PreferenceGroup)
              curParents.push((PreferenceGroup)child);
            }
          }
        return result;
        }
    

    example:

      final Map<Preference,PreferenceGroup> preferenceParentTree=buildPreferenceParentTree(SettingsActivity.this);
      final PreferenceGroup preferenceParent=preferenceParentTree.get(preferenceToRemove);
      preferenceGroup.removePreference(preferenceToRemove);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.