Search code examples
androidactionbarsherlockandroid-actionbarandroid-menu

Set MenuItem onPressed actions Android actionbar? Preferably programmatic


I have a menu created without XML in my class files which are just icons. I'd like to remove the blue square/rectangle highlight shown when the MenuItem is pressed, and instead just change the color of the item (by just showing the same drawable with a new color, from the res).

My menu is as follows, is what I'm trying to achieve possible? I know it probably is via XML but that's not what I want.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuItem item = menu.add(Menu.NONE, 0, Menu.NONE, "BACK").setIcon(android.R.drawable.back);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    return true;
}

I'm using ActionBarSherlock in case that changes anything.

Thanks!

EDIT: If replacing the image isn't possible, I'll settle for just stopping the highlight from appearing. But bonus marks for the image swap :p


Solution

  • I'd like to remove the blue square/rectangle highlight shown when the MenuItem is pressed

    You can use Android Action Bar Style Generator in order to identify and replace/modify the assets you need (try modifying the value of Accent color).

    and instead just change the color of the item (by just showing the same drawable with a new color, from the res).

    You should simply use the two colored version (default and pressed) of your image within a specific XML selector to be used as resource for your icon.