Android Question [SOLVED] Call button_click using x and y coordinates

scsjc

Well-Known Member
Licensed User
Longtime User
I would like to be able to press a button on the screen only using X and Y coordinates.

Does anyone have any code made that by entering X and Y, it goes through all the buttons on the screen and returns the button that is to be able to call the sub button_click?

Thank you :)
 

Star-Dust

Expert
Licensed User
Longtime User
With Accessibility Service you can click on a precise coordinate.
 
Upvote 0

scsjc

Well-Known Member
Licensed User
Longtime User
With Accessibility Service you can click on a precise coordinate.
thanks for your quick response :) It sure is a good option.

But I wanted to do something very simple, simply read the buttons on the screen (in a loop) and knowing their coordinates return which button is pressed.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
At first glance this may seem simple, but the left, top positions of views are relative to their parents.
If all buttons have activity as parent then identifying the button is easy. If the layout has buttons inside panels, it is more difficult.
The code below will identify a button from its properties, if all buttons have activity as parent.

Once the button is identified, you have another challenge. How to know what event-handler to call?
You can put the event name in the tag property in designer.
When button is identified, you can retrieve that string and then use CallSub(Me, evName & "_Click)

The code below is for B4XPages, where Root is like Activity in standard templates.

B4X:
Private Sub PressButton(X As Float, Y As Float)
    For i = 0 To Root.NumberOfViews - 1
        Dim thisView As B4XView = Root.GetView(i)
        If thisView Is Button Then
            If thisView.Left <= X And (thisView.Left + thisView.Width) >= X Then
                If thisView.Top <= Y And (thisView.Top + thisView.Height) >= Y Then
                    CallSub(Me, thisView.Tag.As(String) & "_Click")
                End If
            End If
        End If
    Next
End Sub
 
Upvote 0

scsjc

Well-Known Member
Licensed User
Longtime User
At first glance this may seem simple, but the left, top positions of views are relative to their parents.
If all buttons have activity as parent then identifying the button is easy. If the layout has buttons inside panels, it is more difficult.
The code below will identify a button from its properties, if all buttons have activity as parent.

Once the button is identified, you have another challenge. How to know what event-handler to call?
You can put the event name in the tag property in designer.
When button is identified, you can retrieve that string and then use CallSub(Me, evName & "_Click)

The code below is for B4XPages, where Root is like Activity in standard templates.

B4X:
Private Sub PressButton(X As Float, Y As Float)
    For i = 0 To Root.NumberOfViews - 1
        Dim thisView As B4XView = Root.GetView(i)
        If thisView Is Button Then
            If thisView.Left <= X And (thisView.Left + thisView.Width) >= X Then
                If thisView.Top <= Y And (thisView.Top + thisView.Height) >= Y Then
                    CallSub(Me, thisView.Tag.As(String) & "_Click")
                End If
            End If
        End If
    Next
End Sub
thanks a lot
I test the code
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
thanks for your quick response :) It sure is a good option.

But I wanted to do something very simple, simply read the buttons on the screen (in a loop) and knowing their coordinates return which button is pressed.
I thought you were referring to buttons from another application.
If you have to click buttons of your own App obviously @William Lancee's Solution is the perfect one,
 
Upvote 0

scsjc

Well-Known Member
Licensed User
Longtime User
At first glance this may seem simple, but the left, top positions of views are relative to their parents.
If all buttons have activity as parent then identifying the button is easy. If the layout has buttons inside panels, it is more difficult.
The code below will identify a button from its properties, if all buttons have activity as parent.

Once the button is identified, you have another challenge. How to know what event-handler to call?
You can put the event name in the tag property in designer.
When button is identified, you can retrieve that string and then use CallSub(Me, evName & "_Click)

The code below is for B4XPages, where Root is like Activity in standard templates.

B4X:
Private Sub PressButton(X As Float, Y As Float)
    For i = 0 To Root.NumberOfViews - 1
        Dim thisView As B4XView = Root.GetView(i)
        If thisView Is Button Then
            If thisView.Left <= X And (thisView.Left + thisView.Width) >= X Then
                If thisView.Top <= Y And (thisView.Top + thisView.Height) >= Y Then
                    CallSub(Me, thisView.Tag.As(String) & "_Click")
                End If
            End If
        End If
    Next
End Sub

great solution, fast and easy
i update the code to focus panel position buttons, and work perfectly. THANKS :)

B4X:
Private Sub PressButton(paneltouch As Panel, X As Float, Y As Float) As Boolean
    x = x - paneltouch.Left
    y = y - paneltouch.top
    For i = 0 To paneltouch.NumberOfViews - 1
        Dim thisView As B4XView = paneltouch.GetView(i)
        If thisView Is Button Then
            If thisView.Left <= X And (thisView.Left + thisView.Width) >= X Then
                If thisView.Top <= Y And (thisView.Top + thisView.Height) >= Y Then
                    CallSub(Me, thisView.Tag.As(String) & "_Click")
                    Return True
                End If
            End If
        End If
    Next
    Return False
End Sub
 
Upvote 0
Top