Search code examples
androidandroid-preferencespreferenceactivityonpreferenceclicklistener

onPreferenceClick listener not working / onPreferenceClick not being called


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>

Solution

  • Add android:enabled="true" to Preference:

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