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?

scatmoi
  • 1,958
  • 4
  • 18
  • 32

1 Answers1

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.

bogdan
  • 782
  • 3
  • 7
  • 1
    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](http://stackoverflow.com/q/17181083/1088880) to a ListPreference. Is there a way to do this? Thanks. – scatmoi Jun 19 '13 at 13:50
  • 1
    have you tried to do something like `findViewById(android.R.id.list)` on the preference view? – bogdan Jun 19 '13 at 14:10
  • 1
    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? – scatmoi Jun 19 '13 at 14:31
  • 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. – bogdan Jun 20 '13 at 07:08
  • 1
    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? – scatmoi Jun 21 '13 at 02:36
  • 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)` – bogdan Jun 21 '13 at 08:16
  • 2
    `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 `ListPreference`s... Lastly, if all I really wanted is the PreferenceActivity view, there is already [PreferenceActivity.getListView()](http://developer.android.com/reference/android/app/ListActivity.html#getListView()) – scatmoi Jun 22 '13 at 15:39