Detecting Click vs Hold

Nathan Sokalski 4,116 Reputation points
2022-10-24T00:30:52.587+00:00

I have several View(s) in which I want the user to be able to click and/or hold. The Click event is obviously used for clicking, and the Touch event for holding. However, I only want ONE of the events to occur (I know they will both always happen, but hopefully you know what I mean by clicking vs holding) based on whether the user keeps their finger pressed or releases it immediately. The best way I could come up with to solve this scenario is to use a timer to determine how long the View has been pressed, although this seems very inefficient. Is there any good or standard way to detect whether the user is clicking or holding?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,205 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 24,356 Reputation points Microsoft Vendor
    2022-10-24T02:32:18.85+00:00

    Hello,

    There is no API for long press in touch events, so you could only use a timer to distinguish click from long press.

    In addition, it is more recommended to use LongClick event, if you only want to distinguish click from long press.

    If you want to try this way, please refer to the following code:

       public class LongClickLis : Java.Lang.Object, IOnLongClickListener   
       {  
         
           public bool OnLongClick(View v)  
           {  
               // handle long press event  
               return true; // add it to to distinguish `click` from `long press`.  
           }  
       }  
       // in the activities:  
       textView.Click += ShortOnClick;  
       textView.SetOnLongClickListener(longClickLis);  
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful