Paruyr
Paruyr

Reputation: 47

How use Spinner as ACTION not as SELECTION?

Is there possibility to use spinner as action function not selection? When user clicks on spinner view there is pop up with some items, currently spinner allows us to select item it means when user clicks on first item then the onItemSelected callback will be initiated and next time when the same item will be clicked nothing will happen () even OnNothingSelected is not called. how modify it to make it as action (for example print hello world) even the user click the same item several times in a row. there is another issue when initiating onItemSelected is called without user interaction. I have tried to add some bools to control if user clicked or it was called within initiation, not working because item is not selected when choosing the same item. In another word I want add onItemClick listetner which crash when I am adding. maybe I should use something else?

Upvotes: 1

Views: 177

Answers (1)

Shay Kin
Shay Kin

Reputation: 2657

As said in official documentation :

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

But I found this solution which allow to know if a Spinner is open or closed in the link below. and I modified it to obtain the ItemSelected when the Spinner closes https://stackoverflow.com/a/18636385/7085389

You need to create a custom Spinner like the code below

import android.content.Context;
import android.util.AttributeSet;

    public class CustomSpinner extends androidx.appcompat.widget.AppCompatSpinner {
        // implement the Spinner constructors that you need
    
        public CustomSpinner(Context context) {
            super(context);
        }
    
    
        public CustomSpinner(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
    
        private OnSpinnerItemSelected mListener;
        private boolean mOpenInitiated = false;
    
    
        @Override
        public boolean performClick() {
            // register that the Spinner was opened so we have a status
            // indicator for when the container holding this Spinner may lose focus
            mOpenInitiated = true;
            return super.performClick();
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            if (hasBeenOpened() && hasFocus) {
                performClosedEvent();
            }
        }
    
        /**
         * Register the listener which will listen for events.
         */
        public void setOnSpinnerItemSelected(
                OnSpinnerItemSelected onSpinnerItemSelected) {
            mListener = onSpinnerItemSelected;
        }
    
        /**
         * Propagate the closed Spinner event to the listener from outside if needed.
         */
        public void performClosedEvent() {
            mOpenInitiated = false;
            if (mListener != null) {
                mListener.onSelect(this.getSelectedItem());
            }
        }
    
        /**
         * A boolean flag indicating that the Spinner triggered an open event.
         *
         * @return true for opened Spinner
         */
        public boolean hasBeenOpened() {
            return mOpenInitiated;
        }
    
        /**
         * An interface which a client of this Spinner could use to receive
         * open/closed events for this Spinner.
         */
        public interface OnSpinnerItemSelected {
            /**
             * Callback triggered when the spinner was closed.
             */
            void onSelect(Object item);
        }
    }

and you can use it like the code below:

 spinner.setOnSpinnerItemSelected(new CustomSpinner.OnSpinnerItemSelected() {
            @Override
            public void onSelect(Object item) {
                Log.d(TAG, "onSelect: "+item);
            }
        });

Upvotes: 1

Related Questions