48

What is the difference between these two listeners,documentation says :

OnItemSelectedListener - Interface definition for a callback to be invoked when an item in this view has been selected.

OnItemClickListener - Interface definition for a callback to be invoked when an item in this AdapterView has been clicked.

Selection,Click aren't these equal on touch screen?

Akram
  • 7,548
  • 8
  • 45
  • 72
Suresh
  • 9,495
  • 14
  • 49
  • 63
  • 1
    I think selected is clicks + focus gains, e.g. you can trigger it with the trackball too – apps Nov 14 '10 at 13:05

3 Answers3

58

OnItemSelectedListener is used for Spinners, and OnItemClickListener is used for ListViews.

Bryan Denny
  • 27,363
  • 32
  • 109
  • 125
  • 2
    Nice. that's simple, but rarely source say about this. It really makes me confusing – hqt Jul 13 '12 at 16:06
  • 2
    I think you mean, 'It makes me confused' :) However, I agree - Java interfaces often suffer from this strange problem of declaring methods that are never intended to be called. – Alex Jun 27 '13 at 01:49
  • 14
    This is incorrect, AdapterViews can use either / both types of listeners. user1611552 has correctly identified the difference and that should be the correct answer – d370urn3ur Apr 07 '14 at 10:37
38

AdapterView.OnItemSelectedListener is invoked only when the newly selected position is different from the previously selected position or if there was no selected item.

However AdapterView.OnClickListener is invoked even you click the same item everytime.

http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html

user1611552
  • 621
  • 5
  • 8
  • 6
    Even though the docs say so but onitemselectedlistener does not work for listview and similarly onitemclicklistener does not work for spinner view as it is a general specification and does not include/exclude any particular exception. – user2779311 Aug 03 '14 at 12:53
1

Android makes a distinction between selection events and click events. Widgets based off of the “spinner” paradigm — including Spinner and Gallery — treat everything as selection events. Other widgets — like ListView and GridView — treat selection events and click events differently. For these widgets, selection events are driven by the pointing device, such as using arrow keys to move a highlight bar up and down a list. Click events are when the user either “clicks” the pointing device (e.g., presses the center D-pad button) or taps on something in the widget using the touchscreen.

(source: Excerpt from "The Busy Coder's Guide to Android Development" Version 3.8)