Even if you find a workaround, perhaps this will help someone else. I found an easy way to do this in xml (which logcat answer refers to). The trick I used was to create custom attributes for menu / icon icons. You must have one attribute per menu item icon.
You need to create attrs.xml in your values folder and add your custom attributes. Think of each attribute as a constant that is defined by your themes / styles, and then your styles / views can use these constants to set properties.
<declare-styleable name="customAttrs"> <attr name="customSearchIcon" format="reference" /> </declare-styleable>
In your styles.xml in your values folder, specify your themes / styles that set your custom icon attributes in drawable links.
<style name="AppThemeDark" parent="Theme.Sherlock.ForceOverflow"> <item name="customSearchIcon">@drawable/action_search</item> </style> <style name="AppThemeLight" parent="Theme.Sherlock.Light.ForceOverflow"> <item name="customSearchIcon">@drawable/action_search_light</item> </style>
Finally, in your [menu_name].xml in your menu folder, so that your menu item sets its icon to the corresponding corresponding icon attribute.
<item android:id="@+id/menuitem_search" android:icon="?attr/customSearchIcon"/>
Now, depending on which theme is installed, the icon for the menu item will change. In addition, it allows you to still have specific API versions of your shortcuts (light and dark) with resource identifiers with your drawable folders, so you can have different menu style icons for style icons up to 3.0 and action style icons for 3.0+.
Also remember, when you set a theme at runtime (vs AndroidManifest.xml ), you must set it before calling setContentView() in the Activity . It is recommended to restart your activity after changing the theme of an already created Activity .
source share