How to style text in action overflow menu on device with Android>4.0 and hardware button?

14,699

Solution 1

I've looked into the problem. And there seems to be no way in changing textColor for the options menu for Android >= 4.0 devices with HW menu key. Not even changing primary, secondary or tertiary colors affected the text color.
The only "solution" that comes to my mind now is some pretty nasty use of java reflection API.

However, you can set the background of the bottom options menu of Android >= 4.0 HW key devices separately from the background of the non-HW key dropdown menu.

You already has the styling for non-HW key dropdown menu. The non-HW key dropdown menu is defined by android:popupMenuStyle (and popupMenuStyle):

<style name="Theme.settleup" parent="@style/Theme.Sherlock.Light.DarkActionBar">
    <item name="android:popupMenuStyle">@style/settleup_PopupMenu</item> 
</style>

But the background of the Android >= 4.0 HW key bottom options menu is defined with android:panelBackground item in the theme:

<style name="Theme.settleup" parent="@style/Theme.Sherlock.Light.DarkActionBar">
    <item name="android:panelBackground">@drawable/mybackground</item>
</style>

Since it seems to be a problem to change the text color for bottom options menu on Android >= 4.0 HW key devices, I would suggest to leave this part to its default styling.

Solution 2

Been digging in the AppCompat theming but whatever I tried the textColor remains white. What I do now is just popup the Toolbar menu manually like this:

@Override
public boolean onKeyUp(int keycode, KeyEvent e) {
   switch (keycode) {
       case KeyEvent.KEYCODE_MENU:
           if ( getSupportActionBar() != null ) {
               getSupportActionBar().openOptionsMenu();

               return true;
           }
   }

   return super.onKeyUp(keycode, e);
}

Pressing the menu button while the menu is open magically closes it.

Who needs the ugly black bottom aligned menu anyway?

Solution 3

You can apply styles and Themes in Overflow MenuItem as per below. OverFlow Menu is ListView so, we can apply theme as per listview.

Apply below code in styles.xml

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:dropDownListViewStyle">@style/PopupMenuListView</item>
            <item name="android:actionBarWidgetTheme">@style/PopupMenuTextView</item>
            <item name="android:popupMenuStyle">@style/PopupMenu</item>
            <item name="android:listPreferredItemHeightSmall">40dp</item>
    </style>

    <!-- Change Overflow Menu ListView Divider Property -->
        <style name="PopupMenuListView" parent="@android:style/Widget.Holo.ListView.DropDown">
            <item name="android:divider">@color/app_navigation_divider</item>
            <item name="android:dividerHeight">1sp</item>
            <item name="android:listSelector">@drawable/list_selector</item>
        </style>

        <!-- Change Overflow Menu ListView Text Size and Text Size -->
        <style name="PopupMenuTextView" parent="@android:style/Widget.Holo.Light.TextView">
            <item name="android:textColor">@color/app_white</item>
            <item name="android:textStyle">normal</item>
            <item name="android:textSize">18sp</item>
            <item name="android:drawablePadding">25dp</item>
            <item name="android:drawableRight">@drawable/navigation_arrow_selector</item>
        </style>

        <!-- Change Overflow Menu Background -->
        <style name="PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
            <item name="android:popupBackground">@drawable/menu_overflow_bg</item>
        </style>

Solution 4

For AppCompat theme:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="actionBarPopupTheme">@style/HardwareOptionMenu</item>
</style>

<style name="HardwareOptionMenu" parent="ThemeOverlay.AppCompat.Dark">
    <item name="android:textColorSecondary">@color/white</item>
    <item name="android:colorBackground">@color/black</item>
</style>

Solution 5

try also adding the items without android prefix

<style name="Theme.settleup" parent="@style/Theme.Sherlock.Light.DarkActionBar">
    <item name="android:popupMenuStyle">@style/settleup_PopupMenu</item>
    <item name="popupMenuStyle">@style/settleup_PopupMenu</item> 
    <item name="android:actionMenuTextColor">@android:color/white</item>
    <item name="actionMenuTextColor">@android:color/white</item>
</style>
<style name="settleup_PopupMenu" parent="@style/Widget.Sherlock.ListPopupWindow">
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_settleup</item>
    <item name="popupBackground">@drawable/menu_dropdown_panel_settleup</item>
</style>
Share:
14,699
David Vávra
Author by

David Vávra

I'm Google Developer Expert for Android.

Updated on June 17, 2022

Comments

  • David Vávra
    David Vávra almost 2 years

    I would like custom background & text color for my overflow menu. It works fine with devices without hardware menu button, but I'm not able to style devices with hardware menu button. See the screenshots:

    Screnshot from Galaxy Nexus Screenshot from Galaxy S3

    I'm using these styles:

    <style name="Theme.settleup" parent="@style/Theme.Sherlock.Light.DarkActionBar">
        <item name="android:popupMenuStyle">@style/settleup_PopupMenu</item> 
        <item name="android:actionMenuTextColor">@android:color/white</item>
    </style>
    <style name="settleup_PopupMenu" parent="@style/Widget.Sherlock.ListPopupWindow">
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_settleup</item>
    </style>