0

What I'm trying to accomplish is to use the spinner dropdown's closing action to trigger another event. Since the dropdown closes no matter where you touch on the screen, I guess that there is a listener that waits for a touch/click/etc. at any location, but I can't find that listener.

I can use OnItemSelectedListener to trigger an event when an entry from the dropdown is chosen, easy. I can also listen to the FrameLayout and trigger the event based on that, but it required an extra touch on the screen (maybe the first one gets absorbed by the spinner's listener(?)). There are also other buttons on the screen, so checking all of them as well would be complicated.

Do you know where is the code that actually opens and closes a spinner is?

Here
  • 19
  • 2

1 Answers1

0

Something I tried was to subclass Spinner, and override onWindowFocusChanged. That event fires and hasWindowFocus == true when the spinner closes. That is,

class MySpinner(context: Context, attrSet: AttributeSet): Spinner(context, attrSet) {
    override fun onWindowFocusChanged(hasWindowFocus: Boolean) {
        Toast.makeText(context.applicationContext, "WindowFocusChanged! $hasWindowFocus", Toast.LENGTH_LONG).show()
        super.onWindowFocusChanged(hasWindowFocus)
    }
}

And your XML will need to specify MySpinner.

brismith
  • 696
  • 3
  • 18
  • Thanks, that helps! I'm not doing it exactly like that, just using the onWindowFocusChanged. I didn't know that existed. – Here May 30 '19 at 15:42
  • Although I do still want to find the code that actually closes the spinner. No luck on that so far. – Here May 30 '19 at 18:38
  • Perhaps this might be useful: https://stackoverflow.com/questions/18447063/spinner-get-state-or-get-notified-when-opens – brismith May 30 '19 at 19:01
  • Yeah, I've tried most of that. I guess what I mean is that I don't want to add any new code for detecting if the spinner is open. There must be some obscure line that is inherent in every spinner that tells the spinner when to open or close--that's what I'm looking for. It has to exist somewhere because it's what makes the spinner work. – Here May 30 '19 at 19:35
  • I had poked around here: https://android.googlesource.com/platform/frameworks/base/+/2888524e03896831f487e5dee63f18f1c33c0115/core/java/android/widget/Spinner.java and wondered whether one could get a handle to the DropDownPopup (line 161) and watch for its events, or hook its show method perhaps. – brismith May 30 '19 at 20:28
  • Thanks, I think that will work. I'll give an update with details for anyone who cares to know. – Here Jun 03 '19 at 19:11