1

I am trying to set the background drawable/color of the overflow menu dynamically in code and have found answers here that DON'T WORK for Android 4.x. Does anyone have a solution or ideas on how to do this?

Community
  • 1
  • 1
droideckar
  • 1,077
  • 10
  • 20

1 Answers1

1

If I remember correctly, I found many solutions before but none of them were working for 4.4. I had to change define a custom theme and use the following in a style as mentioned on this answer on How to change the background color of Action Bar's Option Menu in Android 4.2? question. My sniplet below is also from there. Thanks to @Jonik. :

<style name="MyAppActionBarTheme" parent="android:Theme.Holo.Light">
    <item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>
    <item name="android:actionBarStyle">@style/MyApp.ActionBar</item>
</style>

<!-- The beef: background color for Action Bar overflow menu -->
<style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
    <item name="android:popupBackground">@drawable/menu_dropdown_panel</item>
</style>

Also I remember that this somehow didn't work if instead of parent="android:Theme.Holo.Light", I used parent= "android:Theme.Holo.Light.DarkActionBar". So make sure you are using the appropriate parent theme.

Let me know if it helped.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • thanks for your answer but i am trying to set the background color dynamically in code (not via custom style). – droideckar Nov 14 '14 at 21:10
  • Seeing the answers on the link that you gave, they are trying comparing the name of the view to an "internal" class `com.android.internal.view.menu.IconMenuItemView`. I'm sure that it was not intended to be used directly like this. In any release the name or path 'might' change, which will probably break your app. If you come across any way to do it programatically in a clean way, please do post your answer here. Thanks. It would be good to know. – Shobhit Puri Nov 14 '14 at 21:14
  • yes, this is definitely the issue, com.android.internal.view.menu.IconMenuItemView, is not found on my test devices. i will try finding the source xml for the overflow menu and access the menu using the getResouces().getIdentifier() approach to set the view manually in code. – droideckar Nov 14 '14 at 22:42
  • If you insist on changing the color on runtime, why not make two different `themes` in `style.xml` and programmatically switch themes. That seems lesser work than what you mentioned. And you can use xml for defining them – Shobhit Puri Nov 14 '14 at 22:44
  • good question. the reason is because the theme is based on input from the user during runtime (they can pick any color to define their style/theme). ultimately, this would be the case, but currently, i don't have a solution. is there a way to set the style value at runtime? – droideckar Nov 16 '14 at 00:23
  • Not sure about that, but if you've few predefined number of colors that you are want the user to choose from, you can do something like http://stackoverflow.com/a/6390025/1306419 – Shobhit Puri Nov 16 '14 at 00:49
  • ok, not sure i am describing my requirement. i want to be able to do this in the java code of my activity: getActionBar().getOverflowMenu().setBackgroundDrawable(new ColorDrawable(userDefinedColor); and since the method getOverflowMenu() does not exist, is there another way? – droideckar Nov 17 '14 at 02:44