0

When I press menu button long, phone is vibrating. How to connect long press of menu key to some action. For example, to changeMyLang() method. Is it possible to do like that?

Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109
  • 1
    See here: http://stackoverflow.com/questions/10530517/override-menu-key-long-press-and-launch-an-application-on-long-press – Chiral Code Aug 28 '13 at 07:47

1 Answers1

1

By default, ActionBar items show the title attribute in a tooltip. You cannot override this functionality. You can, however, create a custom ActionProvider, where you can set a View.OnLongClickListener on it.

Check out the "ApiDemo" ActionBarSettingsActionProviderActivity. (link) Using that as a starting point, the onCreateActionView of your ActionProvider would look something like this:

    @Override
    public View onCreateActionView() {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.action_bar_custom_action_provider, null);

        ImageButton button = (ImageButton) view.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Respond to normal click
            }
        });
        button.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                // Respond to long-click
                return true;
            }
        }
        return view;
    }

EDIT:

If you're talking about overriding the hardware menu button, you can do that, but only while in your app. See here for snippets on how to hook into the key event.

Community
  • 1
  • 1
Paul Burke
  • 25,496
  • 9
  • 66
  • 62
  • Just reread your question and realized you might be talking about the system menu button ("hardware" button). If so, this answer has nothing to do with that. I thought you meant `Menu` button. – Paul Burke Aug 28 '13 at 07:41