znq
znq

Reputation: 44963

Android: customize application's menu (e.g background color)

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:

Upvotes: 19

Views: 56532

Answers (6)

Marco Bonechi
Marco Bonechi

Reputation: 241

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>

Upvotes: 4

Kirill Rakhman
Kirill Rakhman

Reputation: 43791

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();
    }

Upvotes: 0

wdog
wdog

Reputation: 621

background menu color in style.xml in your theme

<item name="android:panelFullBackground">@android:color/darker_gray</item>

Upvotes: 2

authorwjf
authorwjf

Reputation: 141

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

Upvotes: 13

alocaly
alocaly

Reputation: 717

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...

Upvotes: 4

CommonsWare
CommonsWare

Reputation: 1006539

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.

Upvotes: 8

Related Questions