OpenOptionsMenu function not working in ICS?

Im using the action bar compatibility library. I try to open the options menu using a button with the openOptionsMenu () function, but it does nothing.

The menu is displayed as usual when you press the menu key on my phone. What is wrong here?

public class ReadActivity extends ActionBarActivity { ... @Override public boolean onCreateOptionsMenu(Menu menu) { boolean value; MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.read, menu); value = super.onCreateOptionsMenu(menu); if (Helper.SupportsNewApi()) { getActionBar().hide(); } else { ((View) ((LinearLayout) findViewById(R.id.actionbar_compat)) .getParent()).setVisibility(View.GONE); } return value; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); break; case R.id.menu_search: // Toast.makeText(this, "Tapped search", Toast.LENGTH_SHORT).show(); break; case R.id.menu_bookmark: // selectText(); // setFullScreen(false); break; case R.id.menu_day_night_mode: break; case R.id.menu_settings: break; case R.id.menu_zoom_in: showOverlay(false); break; case R.id.menu_zoom_out: showOverlay(false); break; case R.id.menu_table_of_contents: Intent tocIntent = new Intent(this, TocActivity.class); int GET_SECTION_REFERENCE = 1; startActivityForResult(tocIntent, GET_SECTION_REFERENCE); break; case R.id.menu_overflow: Toast.makeText(this, "Tapped overflow", Toast.LENGTH_SHORT).show(); //closeOptionsMenu(); openOptionsMenu(); //tried the below aswell, no results //getWindow().openPanel(Window.FEATURE_OPTIONS_PANEL, null); break; } return super.onOptionsItemSelected(item); } @Override //disable volume buttons public boolean onKeyDown(int keyCode, KeyEvent event) { if (!menuShown && (keyCode == 25 || keyCode == 24)) { return true; } return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { Log.d(tag, "Keycode is = "+keyCode); if (keyCode == 82) { if (!menuShown) { //openOptionsMenu(); showOverlay(true); } else { showOverlay(false); } //don't want it to open when pressing menu return true; } else if (keyCode == 4 && menuShown) { showOverlay(false); return true; } else if (keyCode == 25 && !menuShown) { prevPage(); return true; } else if (keyCode == 24 && !menuShown) { nextPage(); return true; } return super.onKeyUp(keyCode, event); } } 
+15
android android-actionbar android-menu
Apr 03 2018-12-12T00:
source share
8 answers

I'm not sure how appropriate this is, but this forum I found seems to be the answer to the same question that you have.

I hope this post is relevant enough to solve your problem.

Good luck

+4
Apr 19 2018-12-12T00:
source share

I had the same problem, trying to get around this openOptionsMenu thing in the application that I do this, it should work on Android 1.6 and higher. Following Werner Van Belle's answer, I came to the conclusion that we could solve a workaround to solve the problem. Therefore, I came up with the following code: it is always beautiful when people do not mark the method as final, so we can always redefine it. This is ideal if you do not want to refuse to target your application to the latest api (android: targetSdkVersion = "17"). Hope you enjoy it. :)

 @Override public void openOptionsMenu() { Configuration config = getResources().getConfiguration(); if((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) > Configuration.SCREENLAYOUT_SIZE_LARGE) { int originalScreenLayout = config.screenLayout; config.screenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE; super.openOptionsMenu(); config.screenLayout = originalScreenLayout; } else { super.openOptionsMenu(); } } 
+31
Jul 27 '13 at 22:35
source share

To shed light on this sad development of Google. Obviously, Google wants everyone to adopt the new ActionBar. They could achieve this by making the ActionBar easier to use than the old menu system. However, this is not how they planned the transition. No, they thought it would be wise for harras programmers to make it impossible to use the old menu, but without proper backward compatibility.

Below is the code taken from com.android.internal.policy.impl, which should create the optionsMenu panel. As you can see, the code simply refuses to create a parameter bar. Although, obviously, there is. So, to answer your question: forget about it, Google does not want you to no longer use this option Panel.

  // Don't open an options panel for honeycomb apps on xlarge devices. // (The app should be using an action bar for menu items.) if (st.featureId == FEATURE_OPTIONS_PANEL) { Context context = getContext(); Configuration config = context.getResources().getConfiguration(); boolean isXLarge = (config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE; boolean isHoneycombApp = context.getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.HONEYCOMB; if (isXLarge && isHoneycombApp) { return; } } 
+9
Jun 06 '13 at 15:33
source share

I also do not understand how to use the force lock menu button. However, the following trick helped me show the menu on "limited" types of devices.

First of all, we need to determine if we need the next hack or not.

 boolean requireDirtyMenuButtonHack = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && (activity.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_XLARGE) > 0; 

Then:

 protected final OnClickListener mMenuButtonClickListener = new OnClickListener() { @Override public void onClick(View v) { if (requireDirtyMenuButtonHack) { Configuration config = getContext().getResources().getConfiguration(); config.screenLayout &= ~Configuration.SCREENLAYOUT_SIZE_XLARGE; config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_LARGE; } getActivity().openOptionsMenu(); } }; 

Do not forget to clean! (I donโ€™t know if itโ€™s necessary, but itโ€™s better to cast)

 public void onPrepareOptionsMenu(Menu menu) { if (requireDirtyMenuButtonHack) { Configuration config = getContext().getResources().getConfiguration(); config.screenLayout &= ~Configuration.SCREENLAYOUT_SIZE_LARGE; config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_XLARGE; } //do the preparing... } 
+4
Sep 05 '13 at 10:16
source share

android: targetSdkVersion = "10" in the manifest helped me. openOptionsMenu () works as expected now on ICS +. In addition, an overflow button appears on the bottom of the screen (on the deviceโ€™s button bar).

ps: I use the NoTitleBar theme (NoActionBar for sdk 11 and above) + ViewPagerIndicator by Jake Wharton.

+2
Sep 24 '12 at 21:52
source share

This worked for me, my code is very similar to yours, and what I want to do is from the button on the action bar, open the overflow menu :

 public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_home_about: dialog = new Dialog(this); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.customdialog); dialog.show(); break; default: case R.id.menu_home_refresh: updateLists(true); break; case R.id.menu_home_menu: new Handler().postDelayed(new Runnable() { public void run() { openOptionsMenu(); } }, 0); return true; } return false; } 
+2
Aug 30 '13 at 16:43
source share

Do you want you to show the button on the right side of the action bar?

Here is how I did it:

Res / menu / main.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_share" android:title="Logout" android:icon="@android:drawable/ic_lock_power_off" android:orderInCategory="1" android:showAsAction="always" /> </menu> 


activity 1) take note of ActionBarActivity; 2) MenuInflater in onCreateOptionsMenu 3) onOptionsItemsSelected (I think you need to return super.onOptionsItemSelected (item))

 public class BaseActivity extends ActionBarActivity { .... @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return true; } .... @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_share: //Do something break; } return super.onOptionsItemSelected(item); } 
0
Apr 03 '12 at 15:30
source share

If you are using your custom toolbar, you can try:

 toolBar.showOverflowMenu(); 
0
Jun 20 '16 at 8:32
source share



All Articles