Closing dropdown in Spinner in Android
Asked Answered
B

4

3

I need to animate an icon of an arrow when opening and closing a spinner in Android. I can rotate the arrow when opening the spinner: I just put a setOnTouchListener on the Spinner.

The problem comes, when the dropdown is closed, or hidden, because I don't know how to set a listener or something like that on that action.

Anybody has an idea about how to do this, if possible?

Thanks a lot in advance.

Bootie answered 9/9, 2013 at 9:17 Comment(0)
L
3

I do not know why Google can not do it for so long, but you can solve the problem this way:

You must override the protected method "onDetachedFromWindow" for Spinner, make it as public method, and calling forth it by clicking on the item in your CustomSpinnerAdapter.

For example:

    public class CustomSpinner extends Spinner
    {
        Context context = null;

        public CustomSpinner(Context context)
        {
            super(context);
        }

        public CustomSpinner(Context context, int mode)
        {
            super(context, mode);
        }

        public CustomSpinner(Context context, AttributeSet attrs)
        {
            super(context, attrs);
        }

        public CustomSpinner(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
        }

        public CustomSpinner(Context context, AttributeSet attrs, int defStyle, int mode)
        {
            super(context, attrs, defStyle, mode);
        }

        @Override public void onDetachedFromWindow()
        {
            super.onDetachedFromWindow();
        }
    }

I hope you know how to create SpinnerCustomAdapter and insert this CustomSpinner in xml.

Lumisterol answered 14/1, 2014 at 20:1 Comment(0)
A
0

You can do something like this,

 boolean bflag=true;//declare it as public

     spinner.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                // TODO Auto-generated method stub

                 if(bflag==true)
                {
                    //first animation code goes here
                    Toast.makeText(getActivity(), "on", Toast.LENGTH_SHORT).show();
                    bflag=false;
                }

                else
                {
                    //second animation code goes here
                    Toast.makeText(getActivity(), "off", Toast.LENGTH_SHORT).show();
                    bflag=true;
                }


                return false;
            }

        });
Authenticity answered 9/9, 2013 at 9:26 Comment(2)
it still doesn't work. When clicking in any part of the screen -and thus Android closing the spinner- that isn't triggered.Bootie
awesome approach to achieve down down open / close of spinnerMassorete
P
0

You need to use Reflection and get access to the private field 'mPopup' and then set the method setOnDismissListener(), which is triggered when the pop-up is closed no matter it the user clicks on the empty area or selects new item. You can learn more about how it works here: https://mcmap.net/q/331491/-catching-an-event-when-spinner-drop-down-is-dismissed

Here is the full source code for the custom Spinner

open class CustomSpinner: androidx.appcompat.widget.AppCompatSpinner {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    lateinit var listPopupWindow: ListPopupWindow
    lateinit var onPopUpClosedListener: (dropDownMenu: DropDownMenu) -> Unit
    lateinit var onPopUpOpenedListener: (dropDownMenu: DropDownMenu) -> Unit

    init {

        try {

            // get the listPopupWindow
            val listPopupWindowField = androidx.appcompat.widget.AppCompatSpinner::class.java.getDeclaredField("mPopup")
            listPopupWindowField.isAccessible = true
            listPopupWindow = listPopupWindowField.get(this) as ListPopupWindow
            listPopupWindow.isModal = false

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun performClick(): Boolean {
        val returnValue = super.performClick()

        // indicate that the pop-up was opened
        if (::onPopUpOpenedListener.isInitialized) {
            onPopUpOpenedListener.invoke(this)
        }

        try {

            // get the popupWindow
            val popupWindowField = ListPopupWindow::class.java.getDeclaredField("mPopup")
            popupWindowField.isAccessible = true
            val popupWindow = popupWindowField.get(listPopupWindow) as PopupWindow

            // get the original onDismissListener
            val onDismissListenerField = PopupWindow::class.java.getDeclaredField("mOnDismissListener")
            onDismissListenerField.isAccessible = true
            val onDismissListener = onDismissListenerField.get(popupWindow) as PopupWindow.OnDismissListener

            // now override the original OnDismissListener
            listPopupWindow.setOnDismissListener {

                // indicate that the pop-up was closed
                if (::onPopUpClosedListener.isInitialized) {
                    onPopUpClosedListener.invoke(this)
                }

                // now we need to call the original listener that will remove the global OnLayoutListener
                onDismissListener.onDismiss()
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }

        return returnValue
    }
}

And then simply attach listeners to your custom spinner

val customSpinner = findViewById<CustomSpinner>(R.id.mySpinner)
customSpinner.onPopUpClosedListener = {
    // called when the pop-up is closed
}

customSpinner.onPopUpOpenedListener = { 
    // called when the pop-up is opened      
}
Paramaribo answered 13/9, 2021 at 3:50 Comment(0)
C
-1

try this way

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // // called when spiner will closed

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // called when spiner will closed

            }
        });
Corum answered 9/9, 2013 at 9:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.