The question:
What is the way (if there is a way) to customize the menu (the one triggered by the MENU button of the phone). I’m especially interested in two things:
- changing the background color from the standard light gray into a darker gray
- how the menu items are aligned. I have 4 items and they are automatically aligned 2×2, but I would prefer to have them all in one line (1×4)
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
I created my own menu class. It maybe isn’t exactly what you want but it should hopefully get you started. Here is the article I published and a downloadable link to the source code.
http://www.codeproject.com/KB/android/AndroidMenusMyWay.aspx
Method 2
Not with the built-in menu framework.
You are welcome to intercept the MENU button (via onKeyDown()
or something) and render what you want, but bear in mind that users will be expecting it to look like the rest of the menus do on their device.
Method 3
You can also just implement the “onCreateOptionsMenu” method, that is usually used to display the standard menu, and display whatever you want in this case.
In my game, I implemented it to display a “Game Paused” dialog box when the menu button is pressed…
Method 4
Use styles.
This works for me on Android 5.0
<style name="AppTheme" parent="android:Theme.Material.Light">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:actionOverflowMenuStyle">@style/PopupMenu.MyStyle</item>
</style>
<style name="PopupMenu.MyStyle" parent="android:Widget.PopupMenu">
<item name="android:popupBackground">@drawable/actionbar_item_background</item>
</style>
… then the drawable is a regular selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/primary"/>
<item android:drawable="@color/highlighted" android:state_pressed="true"/>
</selector>
Method 5
background menu color in style.xml
in your theme
<item name="android:panelFullBackground">@android:color/darker_gray</item>
Method 6
This answer works but crashed for me when using ActionBarSherlock. Here’s a hacky workaround to make this work nontheless.
// Black Vodoo! Do not try this at home.
final LayoutInflater li = getLayoutInflater();
final Class<LayoutInflater> clazz = LayoutInflater.class;
try {
final Field fieldSet = clazz.getDeclaredField("mFactorySet");
fieldSet.setAccessible(true);
fieldSet.setBoolean(li, false);
li.setFactory(new Factory() {
@Override
public View onCreateView(final String name,
final Context context, final AttributeSet attrs) {
if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try {
final LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
new Handler().post(new Runnable() {
@Override
public void run() {
// Set the text color
((TextView) view).setTextColor(Color.WHITE);
}
});
return view;
} catch (final Exception e) {
}
}
return null;
}
});
} catch (final Exception e) {
e.printStackTrace();
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0