Java – How do I know when the action bar menu expands?

How do I know when the action bar menu expands?… here is a solution to the problem.

How do I know when the action bar menu expands?

I have a normal 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

when selecting an item works fine, but I want the callback when opening / closing the drop-down menu.

I looked at ActionBar.OnMenuVisibilityListener, but the following command doesn’t print anything 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?

Solution

Recently I was faced with the same task. I use the same method to display Spinner.

But to fix this, I have to replace this Spinner with a custom one. I removed this code and placed my own Spinner through the 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" />

Notify my class (MenuSpinner in the code above) when the menu is displayed from me this gets the problem.

I hope my answer helps people like me who insist on this.

Related Problems and Solutions