touch event with multiple lables (Reflection)

Spacewalker

Member
Licensed User
Longtime User
Hello,

I use the reflection library (V2.2)
I need to add a touch event to some lables.
Actually I have 9 labels - for the testcode here I just use 2 labels

My problem is that the touch event is triggered only for one of the 2 lables (for the second one)
But I need the touch event to be triggered for all labels.

And my next question is :
If I manage that all lables trigger the touch event - how can I find out which label it was?
Would "sender" work...?


Here is my code:
B4X:
Sub Globals

Dim lblTest As Label
Dim reflect As Reflector

End Sub

Sub Activity_Create(FirstTime As Boolean)

'**create first label
lblTest.Initialize("lblTest")
lblTest.Color = Colors.Blue
lblTest.Text = "Test1"
lblTest.Tag  = "1"
Activity.AddView(lblTest, 10dip, 10dip, 200dip, 100dip)

'**create second label (only this lable triggers the touch event)
lblTest.Initialize("lblTest")  
lblTest.Color = Colors.red
lblTest.Text = "Test2"
lblTest.Tag  = "2"
Activity.AddView(lblTest, 10dip, 150dip, 200dip, 100dip)

'**create more labeles (in for/next loop)
'....

reflect.Target = lblTest
reflect.SetOnTouchListener("lblTest_Touch")

End Sub


Sub lblTest_Touch(lblTest1 As Object, Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
    

'*try to find out which label has triggered this event : Result: only the second label triggers this event
Log("lblTest1 Object= "& lblTest1)

Dim myLabel As Label
myLabel = Sender
Log("sender.tag= "& myLabel.tag)
   
'*************************************
Select Action
Case Activity.ACTION_DOWN
   'do something here..
 ase Activity.ACTION_MOVE
   'do something here..
Case Activity.ACTION_UP
   'do something here..
      
Return True

End Sub

thx,
Heinz
 

agraham

Expert
Licensed User
Longtime User
Each of your labels is a separate instance. You need to add the listener to each instance.
B4X:
lblTest.Initialize("lblTest")
   lblTest.Color = Colors.Blue
   lblTest.Text = "Test1"
   lblTest.Tag = "1"
   Activity.AddView(lblTest, 10dip, 10dip, 200dip, 100dip)
   reflect.Target = lblTest
   reflect.SetOnTouchListener("lblTest_Touch")

   '**create second label (only this lable triggers the touch event)
   lblTest.Initialize("lblTest") 
   lblTest.Color = Colors.red
   lblTest.Text = "Test2"
   lblTest.Tag = "2"
   Activity.AddView(lblTest, 10dip, 150dip, 200dip, 100dip)
   reflect.Target = lblTest
   reflect.SetOnTouchListener("lblTest_Touch")
 
Upvote 0
Top