Android Question click event on an array of labels

Mark.S

Member
Licensed User
Only been using 4BA since Christmas (had to do something to avoid clearing up!). I've used VB6 on and off for years, but finding the syntax for 4BA frustrating.
Simple problem, I need to know which label in an array was clicked.

Sub Globals
Dim lbllabel(6) As Label
Dim X As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main")
For X = 1 To 5
lbllabel(X).Initialize("")
lbllabel(X).TextSize=40
lbllabel(X).Text = X
lbllabel(X).TextColor =Colors.Black
lbllabel(X).Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
lbllabel(X).Color = Colors.Cyan
Activity.AddView(lbllabel(X), 300*X+0dip, 20dip, 80dip, 80dip)
Next
End Sub

Sub lblClick_Click
Dim lbl As Label
lbl = Sender
Select lbl
Case 1
Case 2
Case 3
etc

End Select
End Sub


I realize this is a noob question, in VB i knew the answer 25 years ago
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Use the Tag property to determine which was clicked...
B4X:
lbllabel(X).Tag= X
....

Sub lblClick_Click
Dim lbl As Label
lbl = Sender
Select lbl.Tag
Case 1
Case 2
Case 3
etc

Also please place code inside code tags to make easier to read, it's the icon to the left of the save icon.
Welcome to B4X!
:)
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Hi, welcome to B4A!!!

For starters, the name of the language is B4A, maybe your code doesn't work because you are using 4BA :D.

Another thing, when posting code, please use the [code]...[/code] tags, that makes it look better.

Looking at your code, what do you have in the layout named "Main"? if you have the labels in question then that's the problem, views created with the designer DO NOT need to be initialized.

I understand you've just got B4A, so, I would recommend you read the manuals first, all those "noob" questions will be explained there, you can find the links to the documentation in my signature below.
 
Upvote 0

XbNnX_507

Active Member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
  Activity.LoadLayout("Main")
For X = 1 To 5
lbllabel(X).Initialize("EVENTNAME") ' <- event name 
lbllabel(X).TextSize=40
lbllabel(X).Text = X
lbllabel(X).TextColor =Colors.Black
lbllabel(X).tag = X '<- TAG UNIQUE
lbllabel(X).Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
lbllabel(X).Color = Colors.Cyan
Activity.AddView(lbllabel(X), 300*X+0dip, 20dip, 80dip, 80dip)
Next
End Sub

Sub EVENTNAME_Click ' <- event name same one you used in label.initialize("EVENTNAME")

Dim lbl As Label
lbl = Sender
Select lbl

case lbl.tag  ' <- tag property 
Case 1
Case 2
Case 3
etc
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Sub Globals
Please use [CODE]code here...[/CODE] tags when posting code.

codetag001.png

codetag002.png

codetag003.png
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Three comments on your code:
The Array idexes begin with 0.
So, you could use:
B4X:
Sub Globals
    Dim lbllabel(5) As Label
    Dim X As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    For X = 0 To 4
       lbllabel(X).Initialize("lbllabel") ' <- event name
       lbllabel(X).TextSize=40
       lbllabel(X).Text = X + 1
       lbllabel(X).TextColor =Colors.Black
       lbllabel(X).Tag = X + 1 '<- TAG UNIQUE
       lbllabel(X).Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
       lbllabel(X).Color = Colors.Cyan
       Activity.AddView(lbllabel(X), 90dip*X+10dip, 20dip, 80dip, 80dip)
    Next
End Sub

Sub lbllabel_Click ' <- event name same one you used in label.initialize("EVENTNAME")
    Dim lbl As Label
    lbl = Sender
    Select lbl.Tag
    Case 1
    Case 2
    Case 3
    End Select
End Sub
In your case you have lbllabel(0) for nothing.

And this line
Activity.AddView(lbllabel(X), 300*X+0dip, 20dip, 80dip, 80dip)
should be:
Activity.AddView(lbllabel(X), 300dip*X+0dip, 20dip, 80dip, 80dip)
You must use dip values (densitiy independant pixel) for view positions and dimensions.

In Activity.AddView(lbllabel(X), 300*X+0dip, 20dip, 80dip, 80dip)
the biggest value for the Left property will be 1500 pixels, and I am afraid that on some devices the last Label will be out of the screen.
So the line should become:
Activity.AddView(lbllabel(X), 90dip*X+10dip, 20dip, 80dip, 80dip)
10dip is the Left property of the first Label and 90dip gives a space of 10dip between the Labels, you can change this value according to your needs.
 
Upvote 0

Mark.S

Member
Licensed User
Klaus, thank you for your reply.
the target device is a 8" pad, and the project will be a bespoke spreadsheet.
As vbscript does not work on android 'excel' spreadsheet apps, B4A is my only solution.
(and it's about time I got back into coding :) )
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Hi @Mark.S if you take time to skim through the beginners guide I guarantee you will reap the reward. Having already got a VB background I doubt it will take you long and it will inform you of the differences that you have to contend with, the main one being the Activity life cycle on Android.
Good luck with your coding endeavours.

Kind regards,
RandomCoder.
 
Upvote 0
Top