Android Question Handle all view touch event in scrollview

devmobile

Active Member
Licensed User
I can handle touch event for panel that contain many views(label)
And when i touch on label,the touch event for panel raised successfully
But i add this views in scrollview because i have many views(Views are label that their content are number 1 to number 100)
Is there a way for detect touch event when i use scrollview?
 

Star-Dust

Expert
Licensed User
One mode is this
B4X:
Dim reflect ad Reflector
reflect.Target = Scroll.Panel
reflect.SetOnTouchListener("Pnl_Touch")

'Event
Sub Pnl_Touch(lblTest1 As Object, Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean

Select Action
   Case Activity.ACTION_DOWN
   'do something here..
   Case Activity.ACTION_MOVE
   'do something here..
    Log(x & "-" & y)
   Case Activity.ACTION_UP
   'do something here..
End Select

Return True

End
But can add another panel into Scroll.Panel and intercept event on new Add Panel

B4X:
Panel1.Initialize("Panel1")
Scroll.Panel.AddView(Panel1,0,0,Scroll.Panel.Width,Scroll.Panel.Heigth)
'If you change the size of Scroll.Panel (Width, Height) you also need to change the size of Panel1

sub Panel1_Touch (Action As Int, X As Float, Y As Float)

End SUb
 
Last edited:

devmobile

Active Member
Licensed User
One mode is this
B4X:
Dim reflect ad Reflector
reflect.Target = Scroll.Panel
reflect.SetOnTouchListener("Pnl_Touch")

'Event
Sub Pnl_Touch(lblTest1 As Object, Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean

Select Action
   Case Activity.ACTION_DOWN
   'do something here..
   Case Activity.ACTION_MOVE
   'do something here..
    Log(x & "-" & y)
   Case Activity.ACTION_UP
   'do something here..
End Select

Return True

End
But can add another panel into Scroll.Panel and intercept event on new Add Panel

B4X:
Panel1.Initialize("Panel1")
Scroll.Panel.AddView(Panel1,0,0,100%x,100%y)
Thanks i try it now and aware you
 

devmobile

Active Member
Licensed User
One mode is this
B4X:
Dim reflect ad Reflector
reflect.Target = Scroll.Panel
reflect.SetOnTouchListener("Pnl_Touch")

'Event
Sub Pnl_Touch(lblTest1 As Object, Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean

Select Action
   Case Activity.ACTION_DOWN
   'do something here..
   Case Activity.ACTION_MOVE
   'do something here..
    Log(x & "-" & y)
   Case Activity.ACTION_UP
   'do something here..
End Select

Return True

End
But can add another panel into Scroll.Panel and intercept event on new Add Panel

B4X:
Panel1.Initialize("Panel1")
Scroll.Panel.AddView(Panel1,0,0,Scroll.Panel.Width,Scroll.Panel.Heigth)
'If you change the size of Scroll.Panel (Width, Height) you also need to change the size of Panel1

sub Panel1_Touch (Action As Int, X As Float, Y As Float)

End SUb
Yes it's working
Thanks
And other question is:
When i touch on lables,how do i can detect which label is touching now?
I need to highlight touched label via user
 

devmobile

Active Member
Licensed User

Star-Dust

Expert
Licensed User
I would do so. Also because it is necessary to turn off the highlighted color of the previous label.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Pnl As Panel
End Sub


Sub pnl_Touch(Action As Int,X As Float,Y As Float)
    Dim Pnl As Panel = Sender

    For Each Lab As View In Pnl.GetAllViewsRecursive
        If (X>=Lab.Left)And(X<=Lab.Left+Lab.Width) And(y>=Lab.top)And(y<=Lab.top+Lab.Height) Then
            'HighLight
            Lab.Color=Colors.Red
        Else
            ' De-HighLight
            Lab.Color=Colors.White
        End If
    Next
End Sub
other method
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Pnl As Panel
    Dim LastLabel As Label
End Sub


Sub pnl_Touch(Action As Int,X As Float,Y As Float)
    Dim Pnl As Panel = Sender

    For Each Lab As View In Pnl.GetAllViewsRecursive
        If (X>=Lab.Left)And(X<=Lab.Left+Lab.Width) And(y>=Lab.top)And(y<=Lab.top+Lab.Height) Then
            ' De-HighLight previus is exist
            if LastLabel.IsInitialized then LastLabel.Color=Colors.White
            ' HighLight
            Lab.Color=Colors.Red
            LastLabel=Lab
        End If
    Next
End Sub
 

Star-Dust

Expert
Licensed User
You can, but I do not understand why to do so if you can intercept the labels, even if they are many, with just one event.
To distinguish them just read the TAG.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim LabelLast As Label
    Dim ScrollPanel As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
   
    For i=0 To 10
        Dim Lab As Label
        Lab.Initialize("LabelAll")
        Lab.Tag=i
        Lab.Text="My label : " & I
       
        ScrollPanel.Panel.AddView(Lab,0,i*40dip,100dip,40dip)
    Next
   
    'Can do also with desgin
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub LabelAll_Click
    Dim Lb As Label = Sender
   
    Log(Lb.Tag)
    If LabelLast.IsInitialized Then LabelLast.Color=Colors.White
    Lb.Color=Colors.Red
    LabelLast=Lb
End Sub
 

devmobile

Active Member
Licensed User
If i use click event,i have to click each label separator but in touch event,i touch label and move on other labels
 

Star-Dust

Expert
Licensed User
I undestand, is good idea. you do a Library
 

Star-Dust

Expert
Licensed User
yes, if you want
 

klaus

Expert
Licensed User
Can you explain more in detail what exactly you want to do ?
Do all Labels have the same dimensions ?
How are they organized, in rows and columns ?
Do you also need a click function for each Label ?
Do you need to change their values synamically ?
Depending on the answers to these questions, the solution may be different.
 

Star-Dust

Expert
Licensed User
The solution he seeks for and the one in the post#6 and #7
It would like to drag your finger in the panel and as you place it over a Label that colors are differently and so on but hand as your finger drags into the panel

All this without making any clicks on panel, but keeping your finger lean on the panel and dragging it
 

devmobile

Active Member
Licensed User
Can you explain more in detail what exactly you want to do ?
Do all Labels have the same dimensions ?
How are they organized, in rows and columns ?
Do you also need a click function for each Label ?
Do you need to change their values synamically ?
Depending on the answers to these questions, the solution may be different.
My problem solved Thanks
 
Top