25

I have a menu for my main activity (res/menu/main_menu.xml) and I want only one item on the Actionbar and the rest shall be hidden under three dots menu. Three dots menu

The problem is, I never see the three dots item on my actionbar. Why I can't see three dots item? How to force items to be hidden under it?

Note: I use minSdkVersion="14" and I test in AVD.

Mustafa Chelik
  • 2,113
  • 2
  • 17
  • 29

5 Answers5

44
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/menu_item_share"
        android:title="@string/share"
        android:icon="@android:drawable/ic_menu_share"
        app:showAsAction="ifRoom"
        android:actionProviderClass="android.widget.ShareActionProvider" />

    <item
        android:id="@+id/empty"
        android:title="@string/options"
        android:orderInCategory="101"
        app:showAsAction="always"
        android:icon="@drawable/baseline_more_vert_black">

        <menu>

            <item android:id="@+id/action_settings"
                android:icon="@android:drawable/ic_menu_preferences"
                app:showAsAction="ifRoom"
                android:title="@string/settings" />

            <item android:id="@+id/action_help"
                android:icon="@android:drawable/ic_menu_help"
                app:showAsAction="ifRoom"
                android:title="@string/help" />

        </menu>

    </item>

</menu>

You will need to add the baseline_more_vert_black icons to your res/drawable... folders once you've downloaded them from here.

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
13

try this;

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_new_content_facebook"
        android:orderInCategory="100"
        android:title="@string/str_share_facebook"
        app:showAsAction="never" />

    <item
        android:id="@+id/menu_new_content_twitter"
        android:orderInCategory="200"
        android:title="@string/str_share_twitter"
        app:showAsAction="never" />
</menu>

MainActivity.java

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id == R.id.menu_new_content_twitter){
        // do something
    }
    return super.onOptionsItemSelected(item);
}
ali ozkara
  • 5,425
  • 2
  • 27
  • 24
5

this solution provided by ban-geoengineering is one very intresting... 3 dot icon sometimes doesn't display if you use android:showAsAction namespace android: instead custom namespace like:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/taskNotificationsBtn"
    app:showAsAction="always"
    android:icon="@drawable/tasks"/>

But solution provided by ban-geoengineering help me to solve problem with reversing ActionBar items direction to match this to Arabic layout. I have used ActionBarRTLizer library and standard approach with android os 3dots causes some bug when menu item icons cover each other sometimes (very frustratign situation), but this custom solution

<item
    android:id="@+id/empty"
    android:title="@string/options"
    android:orderInCategory="101"
    android:showAsAction="always"
    android:icon="@drawable/ic_action_overflow">

    <menu>

        <item android:id="@+id/action_settings"
            android:icon="@android:drawable/ic_menu_preferences"
            android:showAsAction="ifRoom"
            android:title="@string/settings" />

        <item android:id="@+id/action_help"
            android:icon="@android:drawable/ic_menu_help"
            android:showAsAction="ifRoom"
            android:title="@string/help" />

    </menu>

</item>

solved the problem and RTL works perfectly well! :)

This may also be helpful if you want to handle hardware menu buttons:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
   if (keyCode == KeyEvent.KEYCODE_MENU) {
     // do stuff
     return true;
   } else {
     return super.onKeyUp(keyCode, event);
   }
}

and add here: mMenuReference.performIdentifierAction(R.id.menu_0, 1);

in order to replace Sub Menu displayed as overflow menu depending for example on current Activity you could use this solution:

 public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate your main_menu into the menu
      getMenuInflater().inflate(R.menu.main_menu, menu);

      // Find the menuItem to add your SubMenu
      MenuItem myMenuItem = menu.findItem(R.id.my_menu_item);

      // Inflating the sub_menu menu this way, will add its menu items 
      // to the empty SubMenu you created in the xml
      getMenuInflater().inflate(R.menu.sub_menu, myMenuItem.getSubMenu());

   }
Shahzain ali
  • 1,679
  • 1
  • 20
  • 33
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
2

Why I can't see three dots item?

Your emulator is emulating a device that has a MENU button. If there is a MENU button on the device, the MENU button brings up the overflow.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Can I remove menu button from my AVD to see how it looks like in a device without menu button? – Mustafa Chelik Dec 23 '14 at 20:46
  • @MustafaChelik: Yes, though it may be awkward. If you are using Eclipse, or can run the **`android avd`** command to bring up the original AVD Manager, in the Device Definitions tab, you would change the "Buttons" drop-down to "Software". In the Android Studio AVD Manager, the equivalent is the "Has Hardware Buttons (Back/Home/Menu)" checkbox in the device definition. Or, in the `config.ini` file for the AVD definition (e.g., `~/.android/avd/.../config.ini`), the `hw.mainKeys` would need to be `no` to emulate a device without a MENU key. – CommonsWare Dec 23 '14 at 20:58
0

The solution can be found here https://stackoverflow.com/a/27502405/1808829, which worked perfect for me. No need to work with xml staff, it is done only programatically.

private void getOverflowMenu() {

         try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            if(menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Community
  • 1
  • 1
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56