Check that MenuItem is in ActionBar Overflow

Question: Is there a way to check the code if MenuItem (or which MenuItems) are in the ActionBar overflow menu? I am using ActionBarSherlock

I need this because I have a bunch of icons that will display in the ActionBar if there is space. I have a hungry dark theme, so the icons are made in this way.

My problem arises when menu items are placed in the overflow menu. On Pre-Honeycomb devices, this means that they will be displayed when the user presses the menu button. This menu is the exact opposite background like my ActionBar, and I want it to have a different set of icons.

+4
source share
3 answers

Perhaps I found a solution to this problem: in the design guide ( here ) there is a table that shows how many action lines the elements are displayed depending on the width of the dip.

Based on this table, I wrote the following code:

@Override public boolean onPrepareOptionsMenu(Menu menu) { MenuItem search = menu.findItem(R.id.menu_search); // Get width in dp DisplayMetrics metrics = new DisplayMetrics(); Display display = getWindowManager().getDefaultDisplay(); display.getMetrics(metrics); float logicalDensity = metrics.density; int dp = (int) (display.getWidth() / logicalDensity + 0.5); if (dp < 360) { // only two icons search.setIcon(R.drawable.ic_menu_search); // Show menu icon for pre-3.0 menu } else { search.setIcon(R.drawable.ic_action_search); // Show action bar icon for action bar } return true; } 
+3
source

I sent an answer to a similar question that will help solve your problem:

fooobar.com/questions/407283 / ...

Basically you can use onPrepareOptionsMenu to remove item icons without action.

0
source

You can use reflection. Put the following code in the class, and then call Foo.isInOverflow(yourMenuItem);

 protected static final String SUPPORTCLASS = "android.support.v7.internal.view.menu.MenuItemImpl"; protected static final String NATIVECLASS = "com.android.internal.view.menu.MenuItemImpl"; protected static Method sSupportIsActionButton; protected static Method sNativeIsActionButton; static { try { Class<?> MenuItemImpl = Class.forName(NATIVECLASS); sNativeIsActionButton = MenuItemImpl.getDeclaredMethod("isActionButton"); sNativeIsActionButton.setAccessible(true); } catch (Exception ignored) { } try { Class<?> MenuItemImpl = Class.forName(SUPPORTCLASS); sSupportIsActionButton = MenuItemImpl.getDeclaredMethod("isActionButton"); sSupportIsActionButton.setAccessible(true); } catch (Exception ignored) { } } // -------------------------------------------------------------------------------------------- /** * Check if an item is showing (not in the overflow menu). * * @param item * the MenuItem. * @return {@code true} if the MenuItem is visible on the ActionBar. */ public static boolean isActionButton(MenuItem item) { switch (item.getClass().getName()) { case SUPPORTCLASS: try { return (boolean) sSupportIsActionButton.invoke(item, (Object[]) null); } catch (Exception e) { // fall through } case NATIVECLASS: try { return (boolean) sNativeIsActionButton.invoke(item, (Object[]) null); } catch (Exception e) { // fall through } default: return true; } } /** * Check if an item is in the overflow menu. * * @param item * the MenuItem * @return {@code true} if the MenuItem is in the overflow menu. * @see #isActionButton(MenuItem) */ public static boolean isInOverflow(MenuItem item) { return !isActionButton(item); } 

Note: you need to add the following line to your proguard configuration file for reflection to work in assemblies:

 -keep public class android.support.v7.internal.view.menu.** { *; } 
0
source

All Articles