Get touch position on Views that consume touch event

starchartdals

Member
Licensed User
Longtime User
Sorry if this has been covered already, but I've searched the forum for the answer and can't find it:

I need to be able to get a touch position on a screen at all times, even if I am touching a view that consumes the event. For instance, if I have a ListView, I need to know my finger position while I am scrolling the ListView. Is this possible?

As always, thanks for the help!
 

NJDude

Expert
Licensed User
Longtime User
You can place an invisible panel on top of everything and capture the position when the panel is touched, the attached project is a very basic sample of that.
 

Attachments

  • TouchSample.zip
    45.7 KB · Views: 1,246
Last edited:
Upvote 0

starchartdals

Member
Licensed User
Longtime User
Thanks NJDude, but I do not seem to be able to get the event in the underlying view, as in the attached. I added a button, sent it to the back, and put in click event, which never hits. I explicitly set the return from the panel touch event to False, which I would think means 'don't consume the event', but no dice....

Any further suggestions?
 

Attachments

  • Delete.zip
    45.5 KB · Views: 488
Upvote 0

starchartdals

Member
Licensed User
Longtime User
With the panel in the back, I get the button touch, but no position info when touching the button. I need both, if that is possible.

Just to be clear, I need my application and all of its controls to work normally, but I also need finger position everywhere at all times.
 
Last edited:
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You can't get both since only 1 view can have the focus, you will have to add:
B4X:
Panel1.RequestFocus
When you click on the button, in other words, everytime you tap on a view (button, listview, etc) the panel will lose the focus you will have to set the focus back to the panel after that action.
 
Upvote 0

starchartdals

Member
Licensed User
Longtime User
I'm sorry, NJDude, I'm just not getting what you are telling me - how do I use that? I put it in the button1_click event, but I am still not seeing the coordinates update when I touch the button. Sorry if I am being thick headed.

:sign0013:I guess I don't understand what it means for the event to be consumed - doesn't setting the return to false let you act on the event, but then let the event 'pass through' to another 'consumer'?
 
Upvote 0

Jaames

Active Member
Licensed User
Longtime User
I think this is what you need ;)

Try this example :

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
    Dim TransparentPanelOnTop As Panel
   Dim Button1 As Button
   Dim Button2 As Button
   Dim Label1 As Label
   Dim PositionX As Float
   Dim PositionY As Float
End Sub

Sub Activity_Create(FirstTime As Boolean)
Label1.Initialize("Label1")
Button1.Initialize("Button1")
Button2.Initialize("Button2")
TransparentPanelOnTop.Initialize("TransparentPanelOnTop")


Label1.Text = "Waiting dor click..."
Button1.Text = "I am a Button1 CLICK ME"
Button2.Text = "I am a Button2 CLICK ME"

Activity.AddView(Label1,10,10,Activity.Width - 20,60)
Activity.AddView(Button1,10,100,Activity.Width - 20,60)
Activity.AddView(Button2,10,180,Activity.Width - 20,60)
Activity.AddView(TransparentPanelOnTop,0,0,100%x,100%y)
TransparentPanelOnTop.Color = Colors.Transparent
TransparentPanelOnTop.BringToFront


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub TransparentPanelOnTop_Touch (Action As Int, x As Float, Y As Float) As Boolean 'Return True to consume the event
If Action = Activity.ACTION_DOWN Then
   PositionX = x
   PositionY = Y
End If
End Sub
Sub Button1_Click
Label1.Text = "You Clicked Button1 with positions : X = " & PositionX & " Y = " & PositionY
End Sub
Sub Button2_Click
Label1.Text = "You Clicked Button2 with positions : X = " & PositionX & " Y = " & PositionY
End Sub


Best regards
 
Upvote 0

starchartdals

Member
Licensed User
Longtime User
Hmmm, still does not give me any updated position info if I press the button or scroll the listview. Am I seeing different results than you? For instance, do you get position updates while you are scrolling the listview?
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Hmmm, still does not give me any updated position info if I press the button or scroll the listview. Am I seeing different results than you? For instance, do you get position updates while you are scrolling the listview?

That's what I meant when I said LOSING FOCUS, if you tap or scroll the listview the Panel LOSES focus, therefore cannot update the position, only 1 view can have the focus, however, AFTER you click on the button or the listview the focus returns to the panel, or if you tap on the panel itself, I hope that clear things up.
 
Upvote 0

starchartdals

Member
Licensed User
Longtime User
Jaames,

Thank you, that does work.

Thanks to both of you for your help!!!
Guys like you make this forum great!

Kindest Regards
 
Upvote 0

jabc

Member
Licensed User
Longtime User
I think this is what you need ;)

Try this example :

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
    Dim TransparentPanelOnTop As Panel
   Dim Button1 As Button
   Dim Button2 As Button
   Dim Label1 As Label
   Dim PositionX As Float
   Dim PositionY As Float
End Sub

Sub Activity_Create(FirstTime As Boolean)
Label1.Initialize("Label1")
Button1.Initialize("Button1")
Button2.Initialize("Button2")
TransparentPanelOnTop.Initialize("TransparentPanelOnTop")


Label1.Text = "Waiting dor click..."
Button1.Text = "I am a Button1 CLICK ME"
Button2.Text = "I am a Button2 CLICK ME"

Activity.AddView(Label1,10,10,Activity.Width - 20,60)
Activity.AddView(Button1,10,100,Activity.Width - 20,60)
Activity.AddView(Button2,10,180,Activity.Width - 20,60)
Activity.AddView(TransparentPanelOnTop,0,0,100%x,100%y)
TransparentPanelOnTop.Color = Colors.Transparent
TransparentPanelOnTop.BringToFront


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub TransparentPanelOnTop_Touch (Action As Int, x As Float, Y As Float) As Boolean 'Return True to consume the event
If Action = Activity.ACTION_DOWN Then
   PositionX = x
   PositionY = Y
End If
End Sub
Sub Button1_Click
Label1.Text = "You Clicked Button1 with positions : X = " & PositionX & " Y = " & PositionY
End Sub
Sub Button2_Click
Label1.Text = "You Clicked Button2 with positions : X = " & PositionX & " Y = " & PositionY
End Sub


Best regards

Hello,

I have tried your example on my test tablet, Nexus 7 first model, Android 4.4.2
Clicking on the button does not call the Button1_Click sub (no text change). I also tried finishing the sub by either return true and return false, no effect.
I have the impression the touch event is always consumed by the transparent panel, and not transmitted below.
Could the recent Android updates have broken this feature ?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Could the recent Android updates have broken this feature ?
Yes.
You can add the Touch event with the Reflection library which adds the Return value.

B4X:
Sub Globals
    Dim TransparentPanelOnTop As Panel
    Dim Button1 As Button
    Dim Button2 As Button
    Dim Label1, Label2 As Label
    Dim PositionX As Float
    Dim PositionY As Float
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Label1.Initialize("Label1")
    Button1.Initialize("Button1")
    Button2.Initialize("Button2")
    TransparentPanelOnTop.Initialize("TransparentPanelOnTop")

    Label1.Text = "Waiting dor click..."
    Button1.Text = "I am a Button1 CLICK ME"
    Button2.Text = "I am a Button2 CLICK ME"

    Activity.AddView(Label1, 10dip, 10dip, Activity.Width - 20dip, 60dip)
    Activity.AddView(Button1, 10dip, 100dip, Activity.Width - 20dip, 60dip)
    Activity.AddView(Button2, 10dip, 180dip, Activity.Width - 20dip, 60dip)
    Activity.AddView(TransparentPanelOnTop, 0, 0, 100%x, 100%y)
    TransparentPanelOnTop.Color = Colors.Transparent
    TransparentPanelOnTop.BringToFront

    Dim rf As Reflector
    rf.Target = TransparentPanelOnTop
    rf.SetOnTouchListener("TransparentPanelOnTop_Touch")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub TransparentPanelOnTop_Touch (viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean 'Return True to consume the event
    If action = Activity.ACTION_DOWN Then
        PositionX = X
        PositionY = Y
        Label1.Text = "You touched at X = " & X & " Y = " & Y
    End If
    Return False
End Sub

Sub Button1_Click
    Label1.Text = "You Clicked Button1 with positions : X = " & PositionX & " Y = " & PositionY
End Sub

Sub Button2_Click
    Label1.Text = "You Clicked Button2 with positions : X = " & PositionX & " Y = " & PositionY
End Sub
 
Upvote 0
Top