4

I'm using ActionBarSherlock. I have a MenuItem, and I want to use a custom selector with only that MenuItem, not the others in the ActionBar. This is the menu code:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_include_location"
        android:icon="@drawable/icon_place_selector"
        android:showAsAction="always"
        android:title="@string/menu_include_location"/>

    <item
        android:id="@+id/menu_send"
        android:icon="@drawable/ic_send"
        android:showAsAction="ifRoom|withText"
        android:title="@string/menu_send"/>

</menu>

Here is the icon_place_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/location_place" android:state_pressed="false"/>
    <item android:drawable="@drawable/location_no_gradient" android:state_pressed="true"/>

</selector>

The issue is that in the MenuItem, the icon is only a small part of it. Here's a screenshot of what shows up. The entire background should change, and not just the icon. How do I do that?

enter image description here

gsgx
  • 12,020
  • 25
  • 98
  • 149

3 Answers3

4

You can change the selector of a particular action bar item by setting a custom ActionView in the code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.activity_main, menu);
    MenuItem menuItem = menu.findItem(R.id.menu_include_location);
    ImageView image = new ImageView(this);
    image.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));
    image.setPadding(16, 0, 16, 0);
    image.setImageResource(R.drawable.ic_launcher);
    image.setBackgroundResource(R.drawable.icon_place_selector);
    image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // ...
        }
    });
    menuItem.setActionView(image);
    return true;
}

Apply these styles if you want to change the selector for all action bar items:

<style name="AppTheme" parent="@style/Theme.Sherlock">
    <item name="android:selectableItemBackground">@drawable/icon_place_selector</item>
    <item name="android:actionBarItemBackground">@drawable/icon_place_selector</item>
</style>
Matthias Robbers
  • 15,689
  • 6
  • 63
  • 73
  • Thanks, but I want to apply this specific selector to only one MenuItem, not all of them. – gsgx Nov 30 '12 at 21:06
  • Thanks, I ended up doing a similar thing through an XML layout, but this was helpful. – gsgx Dec 01 '12 at 02:09
1

Use Actionbar style generator, to style your style and download zip file

In downloaded zip file open selectable_background_xxxx.xml (xxxx -> name given to your own style) file in "drawables" folder and change the drawable to nine-patch img or color code you want when pressed state is true.

<item android:state_pressed="true" android:drawable="@color/your_color" /> 
Sruthi Mamidala
  • 361
  • 1
  • 3
  • 10
1

first you shall know, if you set android:selectableItemBackground in your holo style, the whole widget, i.e. a button, will have that background selector.

if you just need to change your actionbar menuitem' s icon selector background, Here is a simple answer!

just set android:actionBarItemBackground in your holo style!

<style name="AppTheme.Dark" parent="android:Theme.Holo">
    <item name="android:actionBarItemBackground">@drawable/item_background_holo_light</item>
</style>

hope it helpful:-)

longkai
  • 3,598
  • 3
  • 22
  • 24