Android: Spinner element on click does not work if it is already selected

I have a Spinner with onItemSelected interation that works, but as the Api spec says:

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

I need to remove this restriction, and I want the callback to be called also if the user selects the same item. How to do it?
Did someone do the same?

Any idea of ​​this will be noticeable.

+7
source share
3 answers

i want that the callback is invoked also if the user select the same element. How to do that?

Setting OnItemClickListener for Spinner will throw an exception and with the help of ItemSelectedListener you will not be notified if the user clicks on the selected / same item.

I believe the only way to overcome this limitation is to use a CustomAdapter for Spinner elements and implement setOnClickListener for each view in the adapter.

+1
source

I had the same problem and looked around a bit. There may be several ways to make this functionality work, but the spinner extension worked for me. You can do something similar to what I found here .

So instead of using the default Android spinner, expand it and add some code to it that will call your callback method.

I would like to add that using setOnItemClickListener on Spinner will throw an exception as stated in the documentation:

 A spinner does not support item click events. Calling this method will raise an exception. 
+1
source

In this case, you need to create your own counter: try

 public class MySpinner extends Spinner{ OnItemSelectedListener listener; public MySpinner(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void setSelection(int position) { super.setSelection(position); if (position == getSelectedItemPosition()) { listener.onItemSelected(null, null, position, 0); } } public void setOnItemSelectedListener(OnItemSelectedListener listener) { this.listener = listener; } } 
0
source

All Articles