1

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.enter image description here

subair_a
  • 1,028
  • 2
  • 14
  • 19

1 Answers1

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
  • Did you try to actually debug your crash? – schlingel Sep 18 '13 at 12:33
  • 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