2

I have an ActionBar and some menu items that are set to show on the ActionBar like this:

<item
        android:id="@+id/menu_play1"
        android:orderInCategory="100"
        android:showAsAction="ifRoom|withText" 
        android:title="Play1x"/>

onOptionsItemSelected(MenuItem item) works fine at catching normal clicks but I'd like to catch a long press so I can can do something different to the default behavior for normal press.

Is that possible? And how? thanks.

John Källén
  • 7,551
  • 31
  • 64
steveh
  • 1,352
  • 2
  • 27
  • 41
  • only possible with custom action view, see http://stackoverflow.com/a/9261497/1018177 – Matthias Robbers Jan 13 '13 at 15:16
  • that post is a bit light on details but this http://developer.android.com/guide/topics/ui/actionbar.html#ActionView looks interesting. if i figure it out, i'll post some code – steveh Jan 14 '13 at 02:49

1 Answers1

0

It is not good approach to allow long presses on MenuItems.

Using Java Reflection have tried to make it possible:

 private interface OnMenuItemLongClickListener{
        boolean onMenuItemLongClik(MenuItem m);
    }
    private void getMenuItemsView(Activity a, final Menu m, final OnMenuItemLongClickListener listener) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
        View homeButton = a.findViewById(android.R.id.home);
        ViewParent parentOfHome = homeButton.getParent().getParent(); //ActionBarView is parent of home ImageView, see layout file in sources

        if (!parentOfHome.getClass().getName().contains("ActionBarView")) {
            parentOfHome = parentOfHome.getParent(); 
            Class absAbv = parentOfHome.getClass().getSuperclass(); //ActionBarView -> AbsActionBarView class
            Field actionMenuPresenterField = absAbv.getDeclaredField("mActionMenuPresenter");
            actionMenuPresenterField.setAccessible(true);
            Object actionMenuPresenter = actionMenuPresenterField.get(parentOfHome);
            Field actionMenuViewField = actionMenuPresenter.getClass().getSuperclass().getDeclaredField("mMenuView");
            actionMenuViewField.setAccessible(true);
            Object actionMenuView = actionMenuViewField.get(actionMenuPresenter);
            Field childrenField= actionMenuView.getClass().getSuperclass().getSuperclass().getDeclaredField("mChildren");
            childrenField.setAccessible(true);
            Field menuField =actionMenuPresenter.getClass().getSuperclass().getDeclaredField("mMenu");
            menuField.setAccessible(true);
            Object menu = menuField.get(actionMenuPresenter);
            Object[] menuItemsAsViews = (Object[])childrenField.get(actionMenuView);
            View.OnLongClickListener longListener = new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {

                    return listener.onMenuItemLongClik(m.findItem(v.getId()));
                }
            };
            for(Object menuView:menuItemsAsViews ){
                View v = (View)menuView;
                v.setOnLongClickListener(longListener);
            }


    }
 }

You can get this at this gist: https://gist.github.com/NikolaDespotoski/6978883

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148