OpenOptionsMenu does not work with ActionBarSherlock Custom SubMenu

In my application, I am using ActionBarSherlock 4.4.

Since ForcedOverflow has been removed from the latest version, I used the following XML to replicate OverflowMenu.

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_overflow" android:icon="@drawable/abs__ic_menu_moreoverflow_holo_dark" android:orderInCategory="100" android:showAsAction="always"> <menu> <item android:id="@+id/action_settings" android:showAsAction="never" android:title="@string/action_settings" /> <item android:id="@+id/recycleBin" android:showAsAction="never" android:title="Recycle Bin" /> </menu> </item> </menu> 

Overflow works great for me now, for both Android 4.3 and 2.3.3.

My problem:

Device 2.3.3 has a hardware menu. I want the menu overflow window to open when a menu key is pressed.

I used the following code in Activity, but it does not work. (I get messages in my LogCat, though)

 @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) { Log.d("HomeActivity", "OpenOptionsMenu"); openOptionsMenu(); return true; } } return super.onKeyUp(keyCode, event); } 

I looked around a lot and tried all the proposed solutions. But nowhere is anyone talking about opening a custom overflow menu with openOptionsMenu() .

Am I missing something?

Is there any way by which I can make it look like I clicked the android:id="@+id/menu_overflow" parent overflow icon element android:id="@+id/menu_overflow" ?

It would be great if someone indicated what I missed here.

+2
android actionbarsherlock android-optionsmenu submenu
Sep 21 '13 at 21:15
source share
2 answers

I had the same problem as you today. I think this is due to the fact that ActionBarSherlock has its own implementation of the options menu, which overrides Android one by disabling the openOptionsMenu() method in this process. Maybe this is a mistake in the library?

In any case, I solved this by overriding onKeyUp in the activity containing the menu as follows:

 @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) { View v = findViewById(R.id.YOUR_MENU_VIEW_HERE); v.performClick(); return true; } return super.onKeyUp(keyCode, event); } 

Hope this helps.

+1
Nov 23 '13 at 19:16
source share

This is how I solved the problem, but Ricardo's answer also works.

 private Menu optionsMenu; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getSupportMenuInflater().inflate(R.menu.display_all, menu); optionsMenu = menu; return true; } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) { //openOptionsMenu(); optionsMenu.performIdentifierAction(R.id.menu_overflow, 0); return true; } } return super.onKeyUp(keyCode, event); } 
0
Nov 24 '13 at 16:53
source share



All Articles