Android preferences onclick event

Badr Hari picture Badr Hari · Mar 16, 2011 · Viewed 59.1k times · Source

In my preferences.xml I have a preference element like this:

<Preference android:title="About" />

I want to assign onClick event, so if user would click on it, I would be able to do open new Intent or browser. I tried to do it like I do with buttons, but this not seem to work.

Answer

Will Tate picture Will Tate · Mar 16, 2011

Badr,

You need to set android:key for the item, Then in your code you can do...

Assuming you use the following in your XML:

<Preference android:title="About" android:key="myKey"></Preference>

Then you can do the following in your code:

Preference myPref = (Preference) findPreference("myKey");
myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
             public boolean onPreferenceClick(Preference preference) {
                 //open browser or intent here
                 return true;
             }
         });