Joe Rakhimov
Joe Rakhimov

Reputation: 5083

Menu button long press action

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?

Upvotes: 0

Views: 4142

Answers (1)

Paul Burke
Paul Burke

Reputation: 25584

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.

Upvotes: 1

Related Questions