I need to change the background of menu items only. When I tried with actionBarItemBackground, it changes the background of application logo [As u can see in the attachment] also. Any help or link will be appreciated.
Asked
Active
Viewed 2,333 times
1

subair_a
- 1,028
- 2
- 14
- 19
-
You want to change the brackground of your icons or of the whole actionbar? – schlingel Sep 18 '13 at 11:35
-
Only the background of menu item. Please see the image attached. – subair_a Sep 18 '13 at 11:38
-
Check this answer, http://stackoverflow.com/questions/11397407/styling-action-item-background-actionbarsherlock – Basim Sherif Sep 18 '13 at 11:58
-
@BasimSherif That answer also saying to use actionBarItemBackground. I have tried this but that didnt work for me. – subair_a Sep 18 '13 at 12:06
1 Answers
2
You can use a custom layout for your actionbar item like so:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@id/item_text"
android:showAsAction="always"
android:actionLayout="@layout/action_text"/>
</menu>
This is styleable as you wish. Another possibility would be adding only the ID (item_text in the example) and set the background like so:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.item_text);
item.getActionView().setBackgroundDrawable(new ColorDrawable(/* your color */));
return super.onPrepareOptionsMenu(menu);
}

schlingel
- 8,560
- 7
- 34
- 62
-
I am getting a crash when I tried onPrepareOptionsMenu() java.lang.NullPointerException at com.demo.customtabs.TabActivity.onCreateOptionsMenu(RDCTabActivity.java:71) at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:44) at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:560) at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:466) at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:265) at – subair_a Sep 18 '13 at 12:32
-
-
Do your usual view styling. It should work that way. Maybe it's easier to just use a custom layout. – schlingel Sep 18 '13 at 12:43
-
I couldn't get this to work on Android 4.0+. Here's what worked for me: http://stackoverflow.com/a/20077381/56285 – Jonik Nov 19 '13 at 18:04