Android Question How do you know where in a button/panel you have clicked?

vecino

Well-Known Member
Licensed User
Longtime User
Hi, I have a button (can be a panel or an image), and clicking on the left part diagonally does one thing.
If the right part is pressed diagonally it does something else.
How can I control it?
Thank you very much.
ventas-compras.png
 

Alexander Stolte

Expert
Licensed User
Longtime User
B4X:
Private Sub MyNicePanel_Touch (Action As Int, X As Float, Y As Float)
    
    If Action = MyNicePanel.TOUCH_ACTION_UP Then
        
        If x < MyNicePanel.Width/2 Then
            Log("Clicked on the left side")
        Else
            Log("Clicked on the right side")
        End If
        
    End If
    
End Sub
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hi, thanks for your help, although I think it is no good, because if you click from the middle of the button downwards, on the right side, then it will think that you have clicked on the left side.
Notice that it is diagonal, as if it were two triangles.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Notice that it is diagonal, as if it were two triangles.
I know, i gave you a very simple example. Now you know how you can do this. In your case you need to check the x and y value, to find out which diagonal page was clicked on
 
Upvote 0

angel_

Well-Known Member
Licensed User
Longtime User
With some corrections (orientation, left position, etc.) it can be a simple condition (assumption square button)

B4X:
Private Sub MyNicePanel_Touch (Action As Int, X As Float, Y As Float)
   
    If Action = MyNicePanel.TOUCH_ACTION_UP Then
       
        If y > x Then
            Log("Clicked on the left side")
        Else
            Log("Clicked on the right side")
        End If
       
    End If
   
End Sub
 
Last edited:
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Thank you very much, it works perfectly.
And also in a very simple way, I was trying to make calculations by measuring the hypotenuse and... I was breaking my head.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
This should work if the panel is not square . . .

B4X:
        If ((MyNicePanel.Width - X) / MyNicePanel.Width) > (Y / MyNicePanel.Height) Then
            Log("Clicked on the left side")
        Else
            Log("Clicked on the right side")
        End If

Edit : OOPS! Might have put the inequality the wrong way!
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
This should work if the panel is not square . . .

B4X:
        If ((MyNicePanel.Width - X) / MyNicePanel.Width) > (Y / MyNicePanel.Height) Then
            Log("Clicked on the left side")
        Else
            Log("Clicked on the right side")
        End If

Edit : OOPS! Might have put the inequality the wrong way!

Now it is working properly.
Thank you very much, friends.

:)
 
Upvote 0
Top