max power
max power

Reputation: 23

Android: Long click on Action Button

I want to fire two different functions on an action bar button: one on "click" and another on "long click". To make an example, it should work like a car-radio. where you can store a frequency of a radio channel on a long press and call it on a short press.

The problem is, that you don't really have a custom listener on the action buttons. Google gives:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_search:
            openSearch();
            return true;
        case R.id.action_compose:
            composeMessage();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Is there a possibility to call a listener on long click?

Upvotes: 1

Views: 2735

Answers (2)

Naresh
Naresh

Reputation: 3179

You can create a custom View for action bar and implement the onLongClick on the custom View.

Upvotes: 0

Alxe
Alxe

Reputation: 391

What you are looking for is OnLongClickListener. Be ware that it must return a boolean meaning that it executed a LongClick or not.

You'll need to add two Listeners on the same Button object:

button.setOnClickListener(new OnClickListener() { ... });
button.setOnLongClickListener(new OnLongClickListener() { ... });

Upvotes: 1

Related Questions