Android Question [RESOLVED] Add DoubleClick listener to label

jimmyF

Active Member
Licensed User
Longtime User
Hello,

I would like to know if and how to set a DoubleClick listener to a label.
I don't see it in the Reflector library.

Thanks
J
 

Semen Matusovskiy

Well-Known Member
Licensed User
As I know, View supports OnClickListener, OnLongClickListener, but does not support "OnDoubleClickListener".
The reason is simple - instead of double tap, Android expects long touch.

Of course if to have a great wish, it' s possible to analyze a time between two clicks. If less than (for example), 200 ms, means double-click.
But again - Android is not a Windows with double-click by a mouse.
 
Upvote 0

jimmyF

Active Member
Licensed User
Longtime User
That makes sense Semen.

I can add it to an individual EditText using Gesture Detector and it works just fine.
When I add it to my label(s) which are part of the Layout in my UltimateListView so I don't actually know how to get the Sender from the label. At least, I can't seem to get the correct Sender value from the Label double_clicked or double Tapped in the Layout.

Thank you for your insight
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I made following experiment
I declared Dim WaitClick As Byte in Process_Globals and added following sub

B4X:
Sub Label1_Click

    If WaitClick = 1 Then
        WaitClick = 2
        Return
    End If

    If WaitClick <> 0 Then Return
 
    WaitClick = 1
    Sleep (200)
     
    If WaitClick = 1 Then
        Log ("Single")
    Else
        Log ("Double")
    End If
    WaitClick = 0
         
End Sub

In slowly emulator better to use Sleep (500).
 
Upvote 0

jimmyF

Active Member
Licensed User
Longtime User
Seriously Semen! Couldn't you have put in a bunch more code just to make it LOOK more complicated? :rolleyes::D;)

I have spent a few hours searching the Internet for an answer.

Still learning......:confused:

Much appreciated, by the way.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I made following experiment
I declared Dim WaitClick As Byte in Process_Globals and added following sub

B4X:
Sub Label1_Click

    If WaitClick = 1 Then
        WaitClick = 2
        Return
    End If

    If WaitClick <> 0 Then Return
 
    WaitClick = 1
    Sleep (200)
  
    If WaitClick = 1 Then
        Log ("Single")
    Else
        Log ("Double")
    End If
    WaitClick = 0
      
End Sub
In this way the double click log would appear twice, because twice it enters Label1_Click

I would do this:
B4X:
LastClick as long ' in Globals
Sub Label1_Click
    If DateTime.Now-LastClick<300 Then
        Log("Double Click")
    Else
        Log("Single Click")
    End If
    LastClick=DateTime.Now
End Sub
In this way the log would first give the single click and after then double click.

Or if you want to make only log double click appear. I would do this:
B4X:
Sub Label1_Click
    If DateTime.Now-LastClik<300 Then
        Log("Double Click")
        LastClik=-1
    Else
        LastClik=DateTime.Now
        Sleep(300)
        If LastClik<>-1 Then
            Log("Single Click")
        End If
    End If
End Sub
 
Upvote 1

Semen Matusovskiy

Well-Known Member
Licensed User
@Star-Dust

In this way the double click log would appear twice, because twice it enters Label1_Click

It's not so and you obviously did not try my code. A logic maybe a little strange. But I will try to explain.

When user clicks first time (WaitClick = 0), the program sets WaitClick = 1 and falls asleep per 200 ms.
What happens further ...

a) If user does not click during sleep period, the subroutine wakes up, logs "Single" (because WaitClick = 1) and returns initial state (WaitClick = 0)

b) user clicks one time during sleep period.

In this case the program sets WaitClick = 2 and exits.
B4X:
 If WaitClick = 1 Then
        WaitClick = 2
        Return
    End If
Note, that similar actions do not have relation to first instance, where we set Sleep (200).

First instance of Sub wakes up, sees WaitClick = 2, reports "Double" and returns initial state (WaitClick = 0)

c) user clicked more than one time during sleep period.

In this case rhe subroutine simply ignores click (when WaitClick = 2, a fragment If WaitClick = 1 Then does not work, but works If WaitClick <> 0 Then Return)
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
@Star-Dust



It's not so and you obviously did not try my code. A logic maybe a little strange. But I will try to explain.

When user clicks first time (WaitClick = 0), the program sets WaitClick = 1 and falls asleep per 200 ms.
What happens further ...

a) If user does not click during sleep period, the subroutine wakes up, logs "Single" (because WaitClick = 1) and returns initial state (WaitClick = 0)

b) user clicks one time during sleep period.

In this case the program sets WaitClick = 2 and exits.
B4X:
 If WaitClick = 1 Then
        WaitClick = 2
        Return
    End If
Note, that similar actions do not have relation to first instance, where we set Sleep (200).

First instance of Sub wakes up, sees WaitClick = 2, reports "Double" and returns initial state (WaitClick = 0)

c) user clicked more than one time during sleep period.

In this case rhe subroutine simply ignores click (when WaitClick = 2, a fragment If WaitClick = 1 Then does not work, but works If WaitClick <> 0 Then Return)
You're right, I did not notice the return.
But I prefer my solution, a matter of taste :p
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I am sorry, but

B4X:
Sub Label1_Click
    If DateTime.Now-LastClick<300 Then
        Log("Double Click")
    Else
        Log("Single Click")
    End If
    LastClick=DateTime.Now
End Sub

is not a solution.

Imagine that last click was at 12:00:00.000
Then user clicks twice at 12:00:01.000 and 12:00.01.150.
What will report your subroutine ? "Single Click", "Double Click". Meanwhile this is simply "Double Click" (150 ms between clicks)
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I am sorry, but

B4X:
Sub Label1_Click
    If DateTime.Now-LastClick<300 Then
        Log("Double Click")
    Else
        Log("Single Click")
    End If
    LastClick=DateTime.Now
End Sub

is not a solution.

Imagine that last click was at 12:00:00.000
Then user clicks twice at 12:00:01.000 and 12:00.01.150.
What will report your subroutine ? "Single Click", "Double Click". Meanwhile this is simply "Double Click" (150 ms between clicks)
I believe you have not tried my code. ;)

As soon as you make the first click, LstClick takes the value 12: 00: 01.000
On the second click he will calculate that they are less than 300ms.

However I do not want to argue, it is not a race and above all it is not my way to approach the forum. I just try to offer solutions to other developer friends.

Regard
 
Upvote 0

jimmyF

Active Member
Licensed User
Longtime User
Thanks for all the feedback and responses.
Semen's code works for me and does the job.
The only change I made was to increase the Sleep value to 250 which seems to make a difference and is more consistent with the double tap/click speed of other views that I am using.

Thank you all!
 
Upvote 0
Top