onPreferenceClick listener not working / onPreferenceClick not being called

3.9k views Asked by At

I am trying to open a website when one of my preferences is clicked (not when the preference is actually changed because there isn't one).

The problem is that the onPreferenceClick() is never called.

This is my PreferenceActivity:

public class About extends PreferenceActivity implements
        OnPreferenceClickListener {

TextView tv_developer;
TextView tv_version;
String versionName;

int counter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.about);        

        Preference p_developer = (Preference) findPreference("p_developer");
        p_developer.setOnPreferenceClickListener(this);

        Preference p_licences = (Preference) findPreference("p_licences");
        p_licences.setOnPreferenceClickListener(this);



    }

    @Override
    public boolean onPreferenceClick(Preference pref) {
        // TODO Auto-generated method stub

        Log.i("Anything pressed", "YES");
        if (pref.getKey().equals("p_developer")) {
            Log.i("p_developer", "YES");


        } else {
            Log.i("p_developer", "NO");
        }
        return true;
    }   
}

This is the xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="about"
    android:title="About" >


    <Preference
        style="?android:preferenceInformationStyle"
        android:key="p_licences"
        android:title="p_licences" />

    <PreferenceCategory android:title="Category" >

        <Preference
            style="?android:preferenceInformationStyle"
            android:key="p_developer"
            android:title="p_developer" />

    </PreferenceCategory>

</PreferenceScreen>

I have also found that this does not do anything either:

<Preference android:title="@string/prefs_web_page" >
    <intent android:action="android.intent.action.VIEW"
            android:data="http://www.example.com" />
</Preference>
4

There are 4 answers

0
srain On BEST ANSWER

Add android:enabled="true" to Preference:

<Preference
    style="?android:preferenceInformationStyle"
    android:key="p2"
    android:enabled="true"
    android:title="p2" />
5
Philipp Jahoda On

You are using:

 PreferenceActivity.addPreferencesFromResource(R.layout.about);

Shouldn't it be more like:

 PreferenceActivity.addPreferencesFromResource(R.xml.yourxmlfilename);

Since thae preferences you defined are not a layout. You need to load the name of the .xml file, not the key of the preference in .xml.

So, put your preferences .xml file in the "xml" folder, and give it a name like "mypreferences.xml". Then, load it like that:

PreferenceActivity.addPreferencesFromResource(R.xml.mypreferences);

In my project, the file looks like this: (name "settings.xml")

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="Your Settings" >
            <Preference
            android:key="delete"
            android:title="Delete User"
            android:summary="Deletes the user." />

      <!-- and so on -->

I add it to the PreferenceActivity by calling:

PreferenceActivity.addPreferencesFromResource(R.xml.settings);

And set the onClickListener:

Preference deletePref = (Preference) findPreference("delete");
        deletePref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                // do stuff
                return true;
            }
        });

And it works.

Furthermore, you could try using a different name for title and key.

0
CodeToLife On

Fresh answer here.

You need to declare listener first. And then attach that listener for each(!) of end Preference objects. Like these:

    final static int CAMERA_SLEEP_TIME=0,
                CAMERA_TIME=1,
                RESET=2;

private Preference.OnPreferenceClickListener listener =new Preference.OnPreferenceClickListener(){
    @Override
    public boolean onPreferenceClick(Preference preference) {
        String keyLcl = preference.getKey();
       if(keyLcl.equals(cameraSleepTime))
           triggerPrefsDialog(CAMERA_SLEEP_TIME);
        else
       if (keyLcl.equals(cameraTime))
           triggerPrefsDialog(CAMERA_TIME);
        else
       if (keyLcl.equals(reset))
           triggerPrefsDialog(RESET);
        else
       return false;
        return true;

    }


};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    Resources resLcl = getResources();
    cameraSleepTime=resLcl.getString(R.string.camera_sleep_time);
    cameraTime=resLcl.getString(R.string.camera_time);
    reset =resLcl.getString(R.string.reset);
    PreferenceScreen sceenLcl = getPreferenceScreen();
    PreferenceCategory pcLcl=(PreferenceCategory)sceenLcl.getPreference(1);
    pcLcl.getPreference(0).setOnPreferenceClickListener(listener);
    pcLcl.getPreference(1).setOnPreferenceClickListener(listener);
    pcLcl=(PreferenceCategory)sceenLcl.getPreference(2);
    pcLcl.getPreference(0).setOnPreferenceClickListener(listener);
    l.a(pcLcl.getTitle());
}

And related preferences.xml:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceCategory

    android:title="@string/switch_mode">

    <CheckBoxPreference
        android:key="@string/enable_switch"
        android:defaultValue="true"
        android:gravity="center"
        android:title="@string/is_switched"

        />
</PreferenceCategory>
<PreferenceCategory

    android:title="@string/seconds_category">
    <Preference
        android:key="@string/camera_time"

        android:defaultValue="600000"
        android:title="@string/camera_time"
        />
    <Preference
        android:key="@string/camera_sleep_time"
        android:defaultValue="600000"
        android:title="@string/camera_sleep_time"
        />
</PreferenceCategory>
<PreferenceCategory
    android:title="@string/System">
    <Preference
        android:key="@string/reset"
        android:defaultValue="false"
        android:title="@string/reset"
        />
</PreferenceCategory>
<PreferenceCategory
    android:title="@string/to_be_added">
</PreferenceCategory>

0
Markus Ressel On

For me the solution was to extend the existing PreferenceCategory class (in Support Library) and override isEnabled(). This method always returned false so I created a class called ClickablePreferenceCategory and changed it to true.

In addition to that I had to add android:selectable="true" to my custom Preference xml node. The result looks like this:

<PreferenceCategory
    android:enabled="true"
    android:key="@string/key_category_problem_solving"
    android:selectable="true"
    android:title="@string/problem_solving">

</PreferenceCategory>

And the ClickablePreferenceCategory:

public class ClickablePreferenceCategory extends PreferenceCategory {

    public ClickablePreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public ClickablePreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ClickablePreferenceCategory(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ClickablePreferenceCategory(Context context) {
        super(context);
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}