I know that this question has been answered multiple times, but I have not found any of them satisfactory and certainly not elegant.
The issue is that OnItemSelected gets fired not only when the user selects an item, but also when the selection is programmatically set.
Some answers propose setting a flag for times when the programmer is setting a value for the spinner. However sometimes other android code will set the value outside of your code.
A common place for android to set the value is upon instantiation of the spinner. Some answers address that particular issue. However, there are numerous places where Android will break down and reinstantiate a spinner. It is not elegant to track down all of them.
So the question is: how does one attach their OnItemSelectedListener code ONLY to selections made by user interaction with the UI?
- Spinner.setOnItemClickListener seems like it would answer this issue perfectly, but Google has it disabled
- AdapterView.setOnClickListener also seems like a natural candidate but it also generates a runtime error
Next place is to extend Spinner and start overriding methods but that of course means messing with the APIs (which I'd rather not do, or I'd at least like to have other users working on it with me)
So I am posting an answer, but any criticisms, improvements, or other more elegant answers are welcome.
The key is overriding onClick to set a flag which ties the onItemSelectedListener to user interaction and fires the onItemClickedListener.
If you don't feel comfortable using the API setOnItemClickedListener (for future compatibility perhaps), you can of course substitute your own method. I just felt like onItemClickedListener should have been implemented to this effect the whole time, so that is my subtle protest.
Also, if anyone can think of a reason that the spinnerTouched flag gets short circuited (stays true for longer than it should), please let us know so that it can be addressed. It seems to work pretty well so far though.