2

I want to set the background color of the Spinner's main TextView to blue whenever the user is about to make a selection from the dropdown. So how do I know when the dropdown appears and disappears?

I think we can use an onTouch listener on the Spinner for when the dropdown appears, but I am not sure what we would do for when it disappears.

BeLambda
  • 887
  • 1
  • 10
  • 19
  • need clarity on one thing, if item is selected you want it blue or before selecting anything.? – Atif AbbAsi Nov 25 '19 at 07:00
  • By default, the background could be something like white, but while the dropdown is appearing, the background should be blue. – BeLambda Nov 25 '19 at 07:31
  • ok you can create a custom spinner and you can set background color for that list and onItem selected get that View and change its background . – Atif AbbAsi Nov 26 '19 at 06:06

1 Answers1

2

There is a method that tells us about the state of the spinner. Which is following

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Log.d("","selected")

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            Log.d("nothing" , "unselected");  
        }

    });

But it won't tell you the state when you touch outside of the spinner. so to resolve this issue you will have to implement your own logic for that. Fortunately, luksporg has shared a solution to that which can help you out.

public class CustomSpinner extends Spinner {

   /**
    * An interface which a client of this Spinner could use to receive
    * open/closed events for this Spinner. 
    */
    public interface OnSpinnerEventsListener {

        /**
         * Callback triggered when the spinner was opened.
         */
         void onSpinnerOpened(Spinner spinner);

        /**
         * Callback triggered when the spinner was closed.
         */
         void onSpinnerClosed(Spinner spinner);

    }

    private OnSpinnerEventsListener mListener;
    private boolean mOpenInitiated = false;

    // implement the Spinner constructors that you need

    @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;
        if (mListener != null) {
            mListener.onSpinnerOpened(this);
        }
        return super.performClick();
    }

    @Override
    public void onWindowFocusChanged (boolean hasFocus) {
        if (hasBeenOpened() && hasFocus) {
            performClosedEvent();
        }
    }

    /**
    * Register the listener which will listen for events.
    */
    public void setSpinnerEventsListener(
            OnSpinnerEventsListener onSpinnerEventsListener) {
        mListener = onSpinnerEventsListener;
    }

    /**
     * Propagate the closed Spinner event to the listener from outside if needed.
     */
    public void performClosedEvent() {
        mOpenInitiated = false;
        if (mListener != null) {
            mListener.onSpinnerClosed(this);
        }
    }

    /**
     * A boolean flag indicating that the Spinner triggered an open event.
     * 
     * @return true for opened Spinner 
     */
    public boolean hasBeenOpened() {
        return mOpenInitiated;
    }

}
Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74