7

so I am facing this problem for long time. I've got Nexus 4 and Nexus 7 both running Android 4.3, and i've got application with targetSdkVersion="11"("I use 11 because any target sdk below 11 doesn't support multitouch for me). And the problem is that 3-dot menu shows on Nexus 4 but doesnt show on Nexus 7. 3 dot menu button on nexus 7 works only if I put targetSdkVersion="8" but then multitouch doesnt work

Nexus 4: enter image description here

Nexus 7 : enter image description here

code :

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="11" />

screenshots : nexus 7enter image description here

nexus 4: enter image description here

user1798049
  • 265
  • 1
  • 3
  • 15
  • 2
    Are you referring to the ... button in the action bar? Posting screenshots would help. If you have insufficient reputation to do that directly, upload the images elsewhere and reference them in your question. – CommonsWare Nov 02 '13 at 20:22
  • i added the screensohts, i don't want to use action bar, i want to have 3-dot menu in navigation bar – user1798049 Nov 02 '13 at 20:44

5 Answers5

12

In case you are specifically wondering why the button is not being shown the following rules apply when Android determines if a legacy menu button is needed:

  • If target API version is less than 11 it is shown on all devices
  • If target version is 11, 12, or 13 (i.e. tablet-only Honeycomb) Android assumes that your app has been designed for tablets and won't show a legacy button on tablets, but will on phones
  • If target is 14 or above (ICS and above), Android assumes your app is designed for tablets and phones and so the legacy button isn't shown.

But like the other answers say, you shouldn't be using this menu button. If you don't want an entire ActionBar, another option would to have a three-dot button in your activity which shows a menu using PopupMenu.

Alex Curran
  • 8,818
  • 6
  • 49
  • 55
  • An article that sums this up rather nicely: http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html – RonR Sep 03 '14 at 21:01
9

You should not be using that menu anymore. From the Menus documentation:

On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with Android 3.0, the Menu button is deprecated (some devices don't have one), so you should migrate toward using the action bar to provide access to actions and other options.

Use an ActionBar.

Community
  • 1
  • 1
Catherine
  • 13,588
  • 9
  • 39
  • 60
2

The correct solution is to use an ActionBar but there may be some hacks to get the overflow menu to appear.

Specifically, there's a hidden window flag FLAG_NEEDS_MENU_KEY you can access via reflection. Here's a code snippet (from this blog):

public static void addLegacyOverflowButton(Window window) {
  if (window.peekDecorView() == null) {
    throw new RuntimeException("Must call addLegacyOverflowButton() after setContentView()");
  }

  try {
    window.addFlags(WindowManager.LayoutParams.class.getField("FLAG_NEEDS_MENU_KEY").getInt(null));
  }
  catch (NoSuchFieldException e) {
    // Ignore since this field won't exist in most versions of Android
  }
  catch (IllegalAccessException e) {
    Log.w(TAG, "Could not access FLAG_NEEDS_MENU_KEY in addLegacyOverflowButton()", e);
  }
}

I tested this on a couple of Nexus devices and it works. Comments on the blog state that there are devices where it doesn't work. Use with caution. And use an ActionBar, really.

laalto
  • 150,114
  • 66
  • 286
  • 303
1

There's a simple way to force a menu option to stay in the menu overflow. If you're creating a menu with XML, you can force this using the "showAsAction" attribute.

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

    <item
        android:id="@+id/menu_option"
        android:showAsAction="never"
        android:title="@string/option_name" /> 

</menu>

If you set "showAsAction" to "never", it will be forced to don't show on the ActionBar, so the option will remain on the menu overflow.

There's more info (like how to vinculate the XML menu file to an Activity) on the official Android documentation webpage: http://developer.android.com/guide/topics/resources/menu-resource.html

I wish this can be helpful!

devrique
  • 526
  • 4
  • 10
0

I wouldnt always recommend using this, since its a hack which breaks the consistency of the phone, but if you want the "3 dots" menu, which is called the overflow menu you need to add this method

 //Hack to display overflowMenu on devices with a menu button
 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();
    }
}

And in your onCreate() call this method.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
setContentView(R.layout.Activity); 

getOverflowMenu();
}
Akshat Agarwal
  • 2,837
  • 5
  • 29
  • 49