I have an PreferenceActivity
where I would like to add Preferences dynamically.
On a long click, these shall do something, however OnPreferenceClickListener
only supports normal clicks, no long clicks.
Is there a way to implement this feature, did I miss something?
Thanks
See if this helps.
In the event that the link dies, here is the main body of the post at that link. Note: I did not author anything below.
The built-in Preference class has a method to receive clicks, onClick, but no method to receive long clicks. In my current project, I actually have a need for this, and found a way to implement it.
PreferenceActivity is actually a ListActivity, with a special adapter behind the scenes. The usual (not long) clicks are processed by using the usual ListView mechanism, setOnItemClickListener. The code to set this up is in PreferenceScreen:
It would be really easy to subclass PreferenceScreen and override bind to add a long-item-click listener to the list view, except this class is final. Because of this, I ended up adding the following code into my PreferenceActivity subclass:
Now I can have a Preference subclass that implements View.OnLongClickListener, which is automatically invoked for long clicks:
Unable to test for you at the moment, but I'm wondering if you could achieve this by using the
getView()
method on aPreference
. Then, once you have thatView
, usesetOnLongClickListener()
.