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

6

19

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 2x2, but I would prefer to have them all in one line (1x4)
Africanist answered 20/10, 2009 at 9:45 Comment(1)
Now, this question is old, but for those looking to customise Action Bar overflow menu background colour (say, on Android 4.0+), see this solution: https://mcmap.net/q/131076/-how-to-change-the-background-color-of-action-bar-39-s-option-menu-in-android-4-2Pudendas
M
8

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.

Morphophonemics answered 20/10, 2009 at 18:26 Comment(2)
Thanks. Then I will skip on this for now and maybe implement it at a later point when there's more time.Africanist
i have seen custom arrangements of menus (as seen here androidandme.com/wp-content/uploads/2009/12/… ) so there must be a way to do it.Wellfounded
G
13

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

Goraud answered 29/3, 2011 at 16:22 Comment(0)
M
8

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.

Morphophonemics answered 20/10, 2009 at 18:26 Comment(2)
Thanks. Then I will skip on this for now and maybe implement it at a later point when there's more time.Africanist
i have seen custom arrangements of menus (as seen here androidandme.com/wp-content/uploads/2009/12/… ) so there must be a way to do it.Wellfounded
S
4

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

Saloma answered 20/10, 2009 at 23:59 Comment(0)
C
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>
Counterman answered 9/12, 2014 at 2:5 Comment(0)
A
2

background menu color in style.xml in your theme

<item name="android:panelFullBackground">@android:color/darker_gray</item>
Abirritant answered 7/4, 2013 at 20:23 Comment(0)
C
0

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();
    }
Coronach answered 30/8, 2013 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.