How to setOnTouchListener() on a ListPreference?
Asked Answered
P

1

6

Handling onTouchEvent on a view is straightforward as every view has the setOnTouchListener() method to do just that.

Alas, although ListPreference is a view, it isn't defined in a layout XML, and thus can't be accessed via findViewById(). So... I don't seem to figure out how to setOnTouchListener() for it.

My idea was to get a reference to it in PreferenceActivity's onCreate(), then getDialog().getCurrentFocus() but at that particular moment in time, it has no focus and not even a dialog (getDialog() returns null, confirmed).

Any idea how to work around this?

Petronel answered 19/6, 2013 at 13:9 Comment(0)
R
0

Use registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) for the shared preferences, if you are interested in all the preferences, or .setOnPreferenceChangeListener(listener) if you only want to know the changes on one item. You will be notified when the preference is changed.

Roguery answered 19/6, 2013 at 13:45 Comment(7)
I am afraid that wouldn't cut it for me. I am not really interested in only knowing when a preference has changed (I am already implementing it BTW). What I really want is to attach a GestureDetector to a ListPreference. Is there a way to do this? Thanks.Petronel
have you tried to do something like findViewById(android.R.id.list) on the preference view?Roguery
Unfortunately I can't do that because a ListPreference is not a view. It is defined in an XML, but not a layout XML. Am I out of luck?Petronel
you define it inside an xml but you use it from a fragment or from an activity. The listPreference is used to create a view after that so in the end it will be a view.Roguery
Yes, but what id to use for findViewById()? AFAIK, a ListPreference has no id, because it's not defined in a layout XML. Can you actually point to code that gets the id of a ListPreference?Petronel
if you are using the listPreference inside a PreferenceActivity use findViewById(android.R.id.list), if you are inside a fragment, inside the method onViewCreated(), you can do view.findViewById(android.R.id.list)Roguery
android.R.id.list is the ID for the entire PreferenceActivity view, not the ListPreference. Also note that a PreferenceActivity could have more than one ListPreferences... Lastly, if all I really wanted is the PreferenceActivity view, there is already PreferenceActivity.getListView()Petronel

© 2022 - 2024 — McMap. All rights reserved.