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:

  • Android: How to stretch an image to the screen width while maintaining aspect ratio?
  • Crashlytics Android SDK - custom UncaughtExceptionHandler
  • Android VideoView Can't play video error especially .mp4
  • How to use SwipeDismissBehavior.OnDismissListener on RecyclerView
  • How to properly load textures using the libgdx assetmanager
  • Android - Swipe to delete RecyclerView
  • <PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <PreferenceCategory
            android:title="foo">
    
            <CheckBoxPreference
                android:key="grok" />
    
                ...
    

    Thanks

    Related posts:

    How to send an email with a file attachment in Android
    How can I share resources between the different Eclipse projects, so as to avoid duplication?
    What happens if the Parse cloud data service fails?
    Dagger- Should we create each component and module for each Activity/ Fragment
    Null Pointer exception in using support library share action provider
    Android Runtime Exception font asset not found
  • How to use “Share image using” sharing Intent to share images in android?
  • After screen orientation change the dialogFragment appears apparently without any call
  • Maximum size of native heap on Android?
  • Android location manager permissions to be used
  • Show context menu from code behind
  • How to Change the Color Behind Android Dialog
  • 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.