views:

214

answers:

2

Can I change the background color of a Menu item in Android?

Please let me know if anyone have any solution to this. The last option will be obviously to customize it but is there any way for changing the text color without customizing it.

A: 

The short answer is YES. lucky you!
To do so, you need to override some styles of the Android default styles :

First, look at the definition of the themes in Android :

<style name="Theme.IconMenu">
<!-- Menu/item attributes -->
<item name="android:itemTextAppearance">@android:style/TextAppearance.Widget.IconMenu.Item</item>
<item name="android:itemBackground">@android:drawable/menu_selector</item>
<item name="android:itemIconDisabledAlpha">?android:attr/disabledAlpha</item>
<item name="android:horizontalDivider">@android:drawable/divider_horizontal_bright</item>
<item name="android:verticalDivider">@android:drawable/divider_vertical_bright</item>
<item name="android:windowAnimationStyle">@android:style/Animation.OptionsPanel</item>
<item name="android:moreIcon">@android:drawable/ic_menu_more</item>
<item name="android:background">@null</item>
</style>

So, the appearance of the text in the menu is in @android:style/TextAppearance.Widget.IconMenu.Item
Now, in the definition of the styles :

<style name="TextAppearance.Widget.IconMenu.Item" parent="TextAppearance.Small">
<item name="android:textColor">?textColorPrimaryInverse</item>
</style>

So now we have the name of the color in question, if you look in the color folder of the resources of the system :

<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
<item android:state_enabled="false" android:color="@android:color/bright_foreground_light_disabled" /> 
<item android:state_window_focused="false" android:color="@android:color/bright_foreground_light" /> 
<item android:state_pressed="true" android:color="@android:color/bright_foreground_light" /> 
<item android:state_selected="true" android:color="@android:color/bright_foreground_light" /> 
<item android:color="@android:color/bright_foreground_light" /> 
<!--  not selected --> 
</selector>

Finally, here is what you need to do :

Override "TextAppearance.Widget.IconMenu.Item" and create your own style. Then link it to your own selector to make it the way you want. Hope this helps you. Good luck!

Sephy
Thanks for replying. I have done what you suggested but it do not change the color of menu item
sunil
Actually, I just realized by reading more of the menu stuff that it is much more complicated than I thought If you don't want to create your own full custom menu... This will need some more reading for me, I'll keep you posted here if i find more.
Sephy
+1  A: 

The solution above doesn't work. It's possible to override the options menu item text appearance using the method described above, but not the item or menu. To do that there are essentially 3 ways: 1) http://stackoverflow.com/questions/2944244/change-the-background-color-of-the-options-menu 2) Write your own view to display and override onCreateOptionsMenu and onPrepareOptionsMenu to get the results you want. I state this generally because you can generally do whatever you want in these methods, but you probably won't want to call into super(). 3) Copy code from the open-source SDK and customize for your behavior. The default menu implementation used by Activity will no longer apply.

See http://code.google.com/p/android/issues/detail?id=4441 for more clues.

Tenacious
To reiterate...Sephy's solution works for the menu item TextAppearance, though what I've done is override the default through a themes.xml. Also, #2 or #3 above should be aware that calling through to super#onCreateOptionsMenu/super#onPrepareOptionsMenu is designed to place system menu items...as indicated in the javadoc for Activity#onCreateOptionsMenu() . That may/may not matter for your app.
Tenacious
could you give a easy example? thank you
pengwang