Search code examples
androidspinner

Android Spinner setOnItemSelectedListener


Is there a way to execute when the same Position is selected?

For example, if 0 is currently selected, selecting 0 again will cause the same event to occur again.


Solution

  • Create a Custom Class that extends Spinner and replace it with your Spinner

    public class NDSpinner extends Spinner {
    
        public NDSpinner(Context context) {
            super(context);
        }
    
        public NDSpinner(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public NDSpinner(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public void
        setSelection(int position, boolean animate) {
            boolean sameSelected = position == getSelectedItemPosition();
            super.setSelection(position, animate);
            if (sameSelected) {
                // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
                getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
            }
        }
    
        @Override
        public void
        setSelection(int position) {
            boolean sameSelected = position == getSelectedItemPosition();
            super.setSelection(position);
            if (sameSelected) {
                // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
                getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
            }
        }
    }