Search code examples
androidmenumenuitemoverflow-menu

Overflow menu doesn't show - android


I have an action bar in my activity with 3 items - Add item, Options item and Overflow item. Add item is set to always be visible in the action bar, so is the overflow item. My problem is with the Options item - I want it to be shown at the overflow menu, but whenever I click at the overflow item(the icon with the 3 dots), it simply does nothin. However, if I click built-in menu button on the device, it shows me the overflow menu with my Options item.

So my question is - why when I click the overflow icon, nothing happens but when I click the built-in menu button on the device, it does show me the overflow menu?

Here is my XML menu code:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/action_add"
        android:icon="@drawable/ic_action_new"
        android:showAsAction="ifRoom"
        android:title="@string/action_add_ringtone"/>

    <item
        android:id="@+id/action_set_options"
        android:showAsAction="never"
        android:title="Options" />

    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/ic_action_overflow"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="@string/action_settings"/>

</menu>

Here is my implementation for onOptionsItemSelected :

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
        case R.id.action_settings:
            return super.onOptionsItemSelected(item);
        case R.id.action_add:
            pickRingtone(1000);
            return true;
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

Solution

  • You will need to override the onCreateOptionsMenu method (Ctrl+O in Android Studio).

    @Override
    public void onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.your_menu_name, menu);
    }
    

    For a fragment the MenuInflater will already be a parameter, and you will need to call setHasOptionsMenu(true); somewhere such as at the end of onCreate as well.