Menu button does not appear on Nexus 7

therefore, I have encountered this problem for a long time. I have Nexus 4 and Nexus 7 running Android 4.3, and I have an application with targetSdkVersion = "11" ("I use 11 because any target SDK below 11 does not support multi-touch for me). That is 3-point the menu is displayed on Nexus 4 but not displayed on Nexus 7. The 3-button menu button on nexus 7 only works if I put targetSdkVersion = "8", but then the multitouch does not work

Nexus 4: enter image description here

Nexus 7: enter image description here

code:

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

screenshots: nexus 7 enter image description here

nexus 4: enter image description here

+6
source share
5 answers

If you are wondering why the button does not appear, the following rules apply, if Android determines whether an obsolete menu button is needed:

  • If the target version of the API is less than 11, it appears on all devices
  • If the target version is 11, 12 or 13 (i.e. only for Honeycomb tablet), Android assumes that your application is designed for tablets and will not show the outdated button on tablets, but will be on phones
  • If the target is 14 or higher (ICS and higher), Android assumes that your application is designed for tablets and phones, so the outdated button does not appear.

But, like the other answers, you should not use this menu button. If you don't need the entire ActionBar, another option would be to have a three-point button in your activity that displays the menu using PopupMenu .

+12
source

You should no longer use this menu. From the menu documentation:

In Android 3.0 and above, items from the options menu are represented by the action bar as a combination of on-screen actions and overflow options. Starting with Android 3.0, the menu button is outdated (on some devices not), so you need to go to the action bar to provide access to actions and other parameters.

Use ActionBar.

+9
source

The correct solution is to use an ActionBar , but some hacks may appear to display the overflow menu.

In particular, there is a hidden window flag FLAG_NEEDS_MENU_KEY , which you can get through reflection. Here's a snippet of code (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 several Nexus devices and it works. Comments on the blog say that there are devices in which it does not work. Use with caution. And use the ActionBar , really.

+2
source

There is an easy way to make a menu option stay in a menu overflow. If you create a menu with XML, you can force this to use 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 not to show on the ActionBar, so the option will remain in the menu overflow.

There is more information (for example, how to expand the XML menu file in Activity) on the official Android documentation web page: http://developer.android.com/guide/topics/resources/menu-resource.html p>

I want this to be useful!

+1
source

I would not always recommend using this, since it is a hack that violates the consistency of the phone, but if you want a 3-point menu 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(); } 
0
source

Source: https://habr.com/ru/post/957259/


All Articles