Android: Why does long click also trigger a normal click?

AndroidEventsListviewOnlongclicklistener

Android Problem Overview


I have a ListView with listeners for a long click and a regular click.

Why, when I long press a list item, the regular click event gets called too?

I need to have two separate functions for the different clicks.

Android Solutions


Solution 1 - Android

From Event Listeners: > onLongClick() - This returns a boolean to indicate whether you have consumed the event and it should not be carried further. That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-click listeners.

Are you returning true from your onLongClick() and still getting the normal click event?

Edited to add: For a ListView, you may be using OnItemLongClickListener. The onItemLongClick() there uses a similar boolean return value to indicate whether it consumed the event.

Solution 2 - Android

Restating the answer in simpler terms:

Given:

@Override
public boolean onLongClick(View view) {
    
    return true; // or false
}
  • return true means that the event is consumed. It is handled. No other click events will be notified.
  • return false means the event is not consumed. Any other click events will continue to receive notifications.

So if you don't want onClick to also be triggered after an onLongClick, then you should return true from the onLongClick event.

Solution 3 - Android

Make sure you are overriding OnClickListener for your onClick method. Also make sure you are overriding OnLongClickListener for your onLongClick method. And make sure that your onLongClick method returns true, as this will consume the onClick.

Solution 4 - Android

You can implement setOnLongClickListener creating an instance of new View.OnClickListener() or new View.OnLongClickListener(), if you create a Long click and implements a normal OnclickListener you will get this errors of single click activating the method. You should use new View.OnLongClickListener() for catching only long clicks

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmellowgView Question on Stackoverflow
Solution 1 - AndroiderichamionView Answer on Stackoverflow
Solution 2 - AndroidSuragchView Answer on Stackoverflow
Solution 3 - AndroidAlex LockwoodView Answer on Stackoverflow
Solution 4 - AndroidMatheus PadovaniView Answer on Stackoverflow