30

Hi I am developing an Android application. In my application I am using Sherlock action. I've defined few menu items in action-bar like in following manner

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/card_menu"
        android:actionLayout="@layout/action_button"
        android:showAsAction="always"
        android:title="cards">
        <menu>
            <item
                android:id="@+id/C1"
                android:title="C1"/>
            <item
                android:id="@+id/C2"
                android:title="c2"/>
            <item
                android:id="@+id/C3"
                android:title="C3"/>
        </menu>
    </item>
    <item
        android:id="@+id/notification"
        android:actionLayout="@layout/notification_icon"
        android:icon="@drawable/notification"
        android:showAsAction="always"
        android:title="Notifications"/>

    <item
        android:id="@+id/filter"
        android:icon="@drawable/filter"
        android:showAsAction="always"
        android:title="Filter"/>
</menu>

and My action_button looks like :

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

    <ImageView
        android:id="@+id/menu_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/spinner_ab_focused_maroon"/>
    <TextView
        android:id="@+id/menu_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/imageView0"
        android:text="C1"/>
</RelativeLayout>

Now everything is displayed but my problem is that when I click on card_menu item where I define sub menus and also define action layout. It's not showing those sub menus. My other menu items are working properly. Only when I define action layout for my item which contains sub menus that I am not able to display sub-menu. If I remove action layout it works fine.

I know if we define action layout for item then we have to manually handle click listener. I did that in following manner

final MenuItem item = menu.findItem(R.id.card_menu);
        item.getActionView().setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onOptionsItemSelected(item);
                Toast.makeText(getActivity(), "click on menu", Toast.LENGTH_SHORT).show();
            }
        });

I am able to handle to click event for that item but not able to show drop-down sub menu items.

How do I solve this problem?

Lachlan Goodhew-Cook
  • 1,101
  • 17
  • 31
nilkash
  • 7,408
  • 32
  • 99
  • 176

4 Answers4

58

Try this code in your activity.
Be sure to properly set your

R.menu.menuidentifier

R.id.menuitemidentifier

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.actionbarhelpmenu, menu);
        final Menu m = menu;
        final MenuItem item = menu.findItem(R.id.ActionConnection);
        item.getActionView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {   
                m.performIdentifierAction(item.getItemId(), 0);
            }
        });
        return true;
    }
ericharlow
  • 2,045
  • 3
  • 21
  • 26
  • 1
    Hi Ericharlow thank you for your valuable help.. thank you a lot. your solution works for me. I was searching for this solution since long time.. Now the only problem is that my submenu list comes at right hand top corner. its not coming as regular.. I wanted to display it below my action bar.. How to move it at proper location .. thankx again for help .. – nilkash Mar 02 '13 at 05:49
  • 1
    another problem is that it not working on older version of android devices it execute that click listener but not showing my sub menus. How to solve this as well. Whether Sherlock library supporting this or not. thank you ... – nilkash Mar 02 '13 at 06:04
  • 1
    @nilkash did you succeed using the actionBarSherlock with a custom layout for the action item, even on old android APIs? the weird thing i get from it is that it's not clickable, and when i go to another view, it might handle the click event that occurred before. – android developer May 16 '13 at 11:29
  • 2
    @ericharlow Its not working for me, `menu.performIdentifierAction(menuFil.getItemId(),0)` always return false, and the submenu is not showing, any help – Thamilan S Oct 30 '13 at 02:52
  • Does anyone have any ideas? – nAkhmedov Mar 26 '14 at 05:16
  • @nAkhmedov see my solution in next answer. – agamov Apr 03 '14 at 11:21
  • For API 10: MenuItemCompat.getActionView(menuItem).setOnClickListener(...) – Yar Apr 13 '15 at 10:39
  • I am facing the same issue. this answer it not working. – Nil Jul 30 '19 at 07:13
8

Accepted answer didn't work for me. My submenu behaved different on different devices. On Motorola Moto X it was like this: enter image description here

You can see that sub-menu is in wrong position (I clicked on bubble icon on the right of ActionBar).

So at the end I came up with different solution: use PopupMenu instead. Code looks like this:

@Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_fragment_chat, menu);
    final MenuItem item = menu.findItem(R.id.menu_item_actionbar_avatar);
    MenuItemCompat.getActionView(item).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showProfileMenuPopup(v);
        }
    });
}

public void showProfileMenuPopup(View v) {
    PopupMenu popup = new PopupMenu(getActivity(), v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_avatar_actions, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            return false;
        }
    });
    popup.show();
}

And it worked :)

agamov
  • 4,407
  • 1
  • 27
  • 31
  • 1
    This is a bug in the framework see https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/com/android/internal/view/menu/ActionMenuPresenter.java#L269 You'll notice it tries to find the View for the item by its item data only by using an internal interface. If not found it will default to the Overflow Button which if it doesn't exist popup defaults to 0,0. Add a Overflow text only item and you'll see it work ok. It should default to the MenuItem ActionView for the popup Anchor View if it exists. Ah well. My suggestion, switch to Toolbar. – Simon Mar 03 '15 at 01:49
  • @Simon there was no toolbar in April, 2014 :) – agamov Jun 08 '16 at 10:12
3

Refer to the answer

If there is a button in the action layout, in order to get the callback from menu item, don't forget to set the button

android:clickable="false"
Community
  • 1
  • 1
Willy Chen
  • 241
  • 2
  • 3
2

Full working code

 <item
    android:title="search"
    android:id="@+id/mSearch"
    app:actionLayout="@layout/my_custom_menu_item"
    android:orderInCategory="100"
    app:showAsAction="always"/>

Code:

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.menu,menu); MenuItem item = menu.findItem(R.id.mSearch);
    ImageView iv= (ImageView) item.getActionView().findViewById(R.id.search_1);
    iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "clicked", Toast.LENGTH_SHORT).show();
        }
    });
    return true;
}
Aklesh Singh
  • 917
  • 10
  • 12