4

I have a plain action bar menu like this:

This is what it looks like in Java:

getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        //when an item is selected (i.e local/My Places/etc)
        return false;
    }
};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, new String[] { "Local", "My Places", "Checkins", "Latitude" });

getActionBar().setListNavigationCallbacks(adapter, navigationListener);

The callback for when an item is selected works fine, but I want a callback for when the dropdown is opened/closed.

I've looked at ActionBar.OnMenuVisibilityListener but the following prints nothing in my console.

ActionBar.OnMenuVisibilityListener listener = new ActionBar.OnMenuVisibilityListener() {
    @Override
    public void onMenuVisibilityChanged(boolean isVisible) {
        System.out.println("hello world!");
    };
};

getActionBar().addOnMenuVisibilityListener(listener);

What can I try next?

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
  • Its a boolean, have you tried `if(isvisible){ system.out.println("hello world"); }` – WonderWorld Apr 15 '15 at 20:45
  • Yep I have, the issue is that there is **no output**. If the if statement was what I was missing, I should have still got an output on expand and compress. – Dave Chen Apr 15 '15 at 20:46
  • I would rather use a toolbar from the support library and adding a Spinner within that toolbar – Rod_Algonquin Apr 15 '15 at 20:46
  • The only difference i see between the 2 is that you use `new OnNavigationListener() ` and for the other `new ActionBar.OnMenuVisibilityListener()` , maybe thats the problem. Just a guess tho, maybe that don't matter. – WonderWorld Apr 15 '15 at 20:55
  • No difference, I imported ActionBar, so I can leave it in or take it out. – Dave Chen Apr 15 '15 at 20:58
  • OnMenuVisibilityListener is called when you will click ActionBar's Options Item Icon (3 dots, in which you see Settings.). – Zealous System Apr 16 '15 at 11:31
  • That's what I thought too. I'm thinking there's on onclick event for the actionbar itself, or at least a way to get the spinner element from the actionbar and then attach the event onto it. – Dave Chen Apr 16 '15 at 16:56

1 Answers1

0

Recently I face the same task. And I use the same method to show Spinner.

But to solve this I have to replace this Spinner with custom one. I remove this code and put own Spinner through xml layout of my activity.

<com.company.yourapp.View.MenuSpinner
        android:id="@+id/catalogue_menu_spinner"
        android:layout_width="wrap_content"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/catalogue_status_bar"
        android:background="@null"
        android:layout_marginLeft="60dp" />

Class that notify me when menu is showed (MenuSpinner in code above) I get from this question.

I hope my answer will help someone who like me stuck on this.

Community
  • 1
  • 1
rkyr
  • 3,131
  • 2
  • 23
  • 38