sender object in activity_touch

Spacewalker

Member
Licensed User
Longtime User
Hello,

I'm new to B4A and currently use the trial for 2 weeks (and like it so far.)

Now here my problem :
I have created some lables programmatically and now I try to use the sender object in Activity_touch.
I create the lables like this:
B4X:
For iLine = 0 To 2
      For iColumn = 0 To 2
      
         iCounter = iCounter + 1
         Dim b As Label  
         'b.Initialize("label")    'All buttons share the same event sub
         b.Initialize("Activity")  'Without this the touch_event didnt work at all...
         b.TextSize = 30
         b.Text = iCounter
            
         Activity.AddView(b,offsetX + iColumn * (width + 10dip), offsetY + iLine * (width + 10dip), width, width)
            
      Next
Next
I was able to use sender in the label's click event.
So I know which label has trigegd a click event.

Now I need to determine which label has triggered the touch event.
But I get the following errors :
MyLabel not initialized / ActivityWrapper cannot be cast to android.widget.textview

This is what I've tried:

B4X:
Sub Activity_Touch (Action As Int, x As Float, Y As Float)
Dim myLabel As Label

Select Action

   Case Activity.ACTION_DOWN 
            
      myLabel = Sender         <--"MyLabel not initialized" / "ActivityWrapper cannot be cast to android.widget.textview"
      myLabel.Text = "I've been touched"
      
   end select
   
end sub

Heinz
 

poseidon

Member
Licensed User
Longtime User
re

B4X:
'add label(s) to a container
   Dim lbl As Label
   lbl.Initialize("cell") 'need this to create an event, multiple labels can have the same event name 
   lbl.Text =categoryName
   lbl.Gravity = Gravity.CENTER
   lbl.Color  = Colors.Transparent 'backcolor
   lbl.TextSize = 16
   lbl.TextColor = Colors.Gray
   lbl.Tag = iColumn 
   ???.Addview(..)


' When label clicked, I know the next Question is how I know the event name? - http://www.b4x.com/android/wiki/index.php/Label
Sub Cell_Click
   Dim l As Label
   l = Sender

if l.tag = "3" then msgbox("label 3 clicked!","Test")
'your code here

End Sub

more at http://www.b4x.com/forum/basic4andr...23-scrollview-examples-summary.html#post47137
 
Last edited:
Upvote 0

Spacewalker

Member
Licensed User
Longtime User
thank you for you answer.
Your code is using the sender object in a label's click event - this I was already able to do.
I'd need the sender object in the label's touch event.

Heinz
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Labels don't have a Touch event in Basic4Android and you cannot set Activity as the EventName.
What exactly do you want to do in the Touch event ?
With the Reflection library you could add the Touch event to a Label, but this library is only available for users who bought B4A.

However, I post the code for other users who might be interested in.
B4X:
Sub Globals
    Dim lblTest As Label
    Dim reflect As Reflector
End Sub

Sub Activity_Create(FirstTime As Boolean)
    lblTest.Initialize("lblTest")
    Activity.AddView(lblTest, 10dip, 10dip, 200dip, 100dip)
    lblTest.Color = Colors.Blue
    lblTest.Text = "Test Test"
    
    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
    Select Action
    Case Activity.ACTION_DOWN
        Activity.Title = "DOWN"
    Case Activity.ACTION_MOVE
        Activity.Title = "MOVE"
    Case Activity.ACTION_UP
        Activity.Title = "UP"
    End Select
    Return True
End Sub
Best regards.
 
Upvote 0

Spacewalker

Member
Licensed User
Longtime User
Labels don't have a Touch event in Basic4Android and you cannot set Activity as the EventName.

Well, I have
B4X:
dim b as label 
 b.Initialize("Activity")
in my code and it compiles and runs... but I use the emulator so maybe this makes a difference?
When I click the label in the emulator a touch event is triggered (but not the click_event)...and my problem was that I can not use "sender" in the touch event to find out which of the lables was "touched" (clicked in the emulator)

What I try to achieve is to write a simple game where I have 8 lables in a 3x3 matrix (and 1 empty field) and I want to touch a label and move it to another position in that 3x3 matrix.

Heinz
 
Upvote 0
Top