How to check if drop down menu of ActionBar is active

206 views Asked by At

I'm using the ActionBar extending from the AppCompatActivity. How can I check, if the dropdown menu of the ActionBar is opened at the moment.

I've tried it in this method. But it does not fire if I open the drop down menu:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent = new Intent();

    Log.i("ActionBar", "ActionBar dropdown is open at this moment");

    switch (item.getItemId()) {
        case R.id.preferences:
            intent.setClass(StartupActivity.this, PreferencesActivity.class);
            startActivityForResult(intent, 0);
            return true;
        case R.id.info:
            intent.setClass(StartupActivity.this, InformationActivity.class);
            startActivityForResult(intent, 0);
            return true;
        case R.id.contact:
            intent.setClass(StartupActivity.this, ContactActivity.class);
            startActivityForResult(intent, 0);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

It only fires, if I click on an item of the drop down menu. But I want to check, if the user clicks on the three-dot menu.

enter image description here

2

There are 2 answers

0
Stampy On

I've tried this method, it works:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // menu open
    return super.onPrepareOptionsMenu(menu);
}

Even when initializing the menu, and not only when you click, but better than nothing.

1
Joseph Roque On

Try the following method:

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
    // menu is open
    return super.onMenuOpened(featureId, menu);
}