Question: Is there a way to check in code if a MenuItem (or which MenuItems) are in the overflow menu of the ActionBar? I'm using ActionBarSherlock
The reason I need this is because I have a bunch of icons that will show up in the ActionBar if there is room. I have a holo dark theme so the icons are made to fit that.
My problem comes when the menu items are put into the overflow menu. On Pre-Honeycomb devices this means they will show when the user presses the menu button. This menu is the exact opposite background as my ActionBar and I want to have a different set of icons to fit that.
I might have found a solution to this problem: In the Design Guide (here), there is a table that shows how many action bar items are shown depending on the width in dip.
Based on that table I have written 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;
}