Question

I've created a layout that contains a spinner and injected into the custom view of my action bar.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Spinner
        android:id="@+id/navigationSpinner"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:background="@drawable/actionbar_spinner_background" />

</RelativeLayout>

I want the actionbar_spinner_background to be a selector with two states - effectively when the spinner is open and when its closed - but I can't find any way of detecting this. I've been through each of the states (state_focused etc) and found only the following seems to work (because the drop down menu is a popup window), but the problem is that the spinner will change state when anything overlays my app, for example when the status bar is pulled down.

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_window_focused="false"
        android:drawable="@drawable/actionbar_spinner_background_up" />

    <item android:drawable="@drawable/actionbar_spinner_background_down" />

</selector>

I've also seen similar stackoverflow questions here and here, but they both refer to "android:state_dropdown_showing" which doesn't appear to have been available for a long time (as an aside, my min api level is 14), and also here, which might be a slightly different use case, but has no answer anyway.

The best plan I seem to be able to come up with at the moment is to override my activity's onWindowFocusChanged() event, check to see if the drop down menu window/layout is present, and then call setActivated() (or something similar to set a state) on the spinner view.

Was it helpful?

Solution

You should go either with the solution you described or create a customs spinner adapter in which you can catch the opening event or a completely custom spinner and have everything work they way you want it....i never found an xml/selector/layout solution to this problem. I have seen solutions with onTouch listeners but they are quite a bit of a workaround since you have to catch all sort of events into consideration..better create a custom spinner once and use it forever if you plan on doing android for a while.

A spinner by it self is nothing special you can mimic it by opening a dialog on a button for ex...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top