Android: Spinner item on click does't work if it is already selected
Asked Answered
D

3

7

I've a Spinner with onItemSelected interation that works, but how the Api specification 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 limitation and i want that the callback is invoked also if the user select the same element. How to do that?
Anyone did the same thing?

Any idea about this would be appreciable..

Debatable answered 25/6, 2012 at 4:34 Comment(3)
try with setOnItemClickListenerPicador
I am also looking for the same....Jugglery
Possible duplicate of How can I get an event in Android Spinner when the current selected item is selected again?Keenankeene
K
1

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

Setting the OnItemClickListener for a Spinner will throw an exception and using ItemSelectedListener you will not be notified if the user click on the selected/same element.

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

Kant answered 25/6, 2012 at 4:56 Comment(0)
O
1

I had this same problem and looked around for a bit. There might be multiple ways of getting this functionality to work but extending the spinner worked for me. You could do something similar to what I found here.

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

I would like to add that using the setOnItemClickListener on a 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.
Osterman answered 26/3, 2013 at 14:19 Comment(0)
K
0

In this case you have to make a custom spinner: Try this

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;
}
}
Kentigera answered 2/12, 2017 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.