Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a preference from PreferenceActivity?

Tags:

android

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:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="foo">

        <CheckBoxPreference
            android:key="grok" />

            ...

Thanks

like image 994
user291701 Avatar asked Jan 20 '11 22:01

user291701


People also ask

What is preference activity android?

PreferencesActivity is a way to easily create preference screens such as those in Android itself, just look under Settings . These can be used inside applications to easily save preferences to SharedPreferences and then easily access these from within your app. See this page for more information on PreferenceActivity.


1 Answers

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);
like image 64
Attenzione Avatar answered Sep 27 '22 19:09

Attenzione