Android Question How Grab Click from non declared Label?

dws

Member
Licensed User
Longtime User
Hello to everyone,

I've read a layout on a panel.
The layout includes some labels that I have not stated in the activity.

Sample Code like this

Sub Activity_Create(FirstTime As Boolean)

' Load the main layout for activity
Activity.LoadLayout("1")

' Load the Layout ( 1 Panel and inside 4 labels in horizontal alignment )
' Each Label has same TAG name

Dim xui as XUI
Dim p as B4XView = xui.CreatePanel("MyEventName")
p.SetLayoutAnimated(0, 0, 0, 100%x, 100%y)
p.LoadLayout("layout_with_4Labels")

' This panel host the view of layout

Dim ViewPanel as panel
ViewPanel.Initiliaze("ViewPanel")
ViewPanel.AddView(p, 0, 0, 100%x, 100%y)

End Sub


Now : How can I control the click event without stating all the Labels?
Like this

Sub Label_Click
log(Label.Text & " - " & Label.Tag)
End Sub

Can anyone help me ....

Thanks
 

JohnC

Expert
Licensed User
Longtime User
This tutorial demonstrates how to create "view arrays", which you can do with the labels:

 
  • Like
Reactions: dws
Upvote 0

dws

Member
Licensed User
Longtime User
I have another sub where i read all views from panel to load from layout.

Like this
in Global
DIm Labels() As Label

Sub Read_LoadedLayout(p as panel)

' Count the labels view from panel was load from Layout
Dim iViewCount as Int = 0

' Label View Counter
For Each v as View In p.GetAllViewsRecursive
iViewCount = iViewCount + 1
End Sub

' Redim for match labels
Dim Labels(iViewCount) As Label

' Repeat for assign labels
Dim iThisView As Int = 0
For Each v as View In p.GetAllViewsRecursive

' Count
iThisView = iThisView + 1

' Check if is Label
if v Is Label then

' Get Label
Dim L as Label = v

' Add Label to Array
Labels(iThisView) = L

log(L.tag)

End if

next

End Sub


This work fine ... Now how to handle Click Event for this labels ... ???
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
OK, then the only other way I know of is to set the "Event Name" for all the labels to the same event name like "Label_Click" in the designer/layout, so no matter which label is clicked on, it will call the same Label_Click event.

Since it seems you are already assigning a unique .Tag value to each of the labels, then you can determine which label was clicked on by getting the Sender.Tag value:

B4X:
Sub Label_Click
    Dim L As Label
    L = Sender
    If L.Tag = "5" then
       ...
    end if
End Sub
 
Last edited:
Upvote 0

dws

Member
Licensed User
Longtime User
I have attach a small example. What is wrong there ... any help ...

Instractions
1. Press Load Layout Button
2. Press ReadLaout
3. Press the labels
4. See Log

Thanks.
 

Attachments

  • TestLayout.zip
    10.9 KB · Views: 112
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Firstly .. To make your posts easier to read , please use Code Tags ... [code] Paste Code Here ... [/code]

I think your problem would have been solved if you had added the Sender Object to code in Post #1 AND changed all the label Event Names in the Designer to "Label"
B4X:
Sub Label_Click
  Dim L as Label = Sender
  log(L.Text & " - " & L.Tag)
End Sub

Capture.JPG



Looking at your sample , I think you are over complicating things to try and solve your question ... but I might be wrong.

I have made a few changes / comments , including introducing you to Smart String Literals. https://www.b4x.com/android/forum/threads/b4x-smart-string-literal.50135/

Hope this is of some help.
 

Attachments

  • TestLayout2.zip
    11 KB · Views: 109
Upvote 0
Top