Android Question (Solved). Changing label text within loop

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

Probably a simple thing but it is screwing with my head.

At times in a project I have a number of labels [lblF0 -- lblF15] that need their text colour changed. The text on the label is assigned by the user so this cannot be used to identify the label.

I can do this by changing each label individually I am trying to do it within a loop but trying to identify the label by name triggers errors. I've tried several variations and usually get a Mixed Type error.

This works but is a bit like a sledge hammer.

B4X:
'        lblF0.TextColor = Colors.Gray
'        lblF1.TextColor = Colors.Gray
'        lblF2.TextColor = Colors.Gray
'        lblF3.TextColor = Colors.Gray   
'        lblF4.TextColor = Colors.Gray   
'        lblF5.TextColor = Colors.Gray
'        lblF6.TextColor = Colors.Gray
'        lblF7.TextColor = Colors.Gray
'        lblF8.TextColor = Colors.Gray
'        lblF9.TextColor = Colors.Gray
'        lblF10.TextColor = Colors.Gray
'        lblF11.TextColor = Colors.Gray
'        lblF12.TextColor = Colors.Gray
'        lblF13.TextColor = Colors.Gray
'        lblF14.TextColor = Colors.Gray
'        lblF15.TextColor = Colors.Gray



This more like I am trying to do.

B4X:
For Each v As View In Activity.GetAllViewsRecursive
            If v Is Label Then
                Private LblFF As Label = v
                For W = 0 To 15
                    If v = "lblF"&W  Then                             'It is here trying to Name the Label from the loop is where I fail.
                        LblFF.TextColor = Colors.Gray             'If I can get the principle correct the colour can be varied.
                    End If
                Next
            End If
        Next

Thanks in advance for the simple answer that will be obvious once you tell me.

Regards Roger
 

JohnC

Expert
Licensed User
Longtime User
You can use the labels' .TAG string property to assign a unique ID for each label.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Or you could store the target labels in an array an access them directly:

B4X:
Dim TargetLabels() As Label = Array As Label(Label1,Label2,Label3,Label4,Label5,Label6,Label7,Label8,Label9,Label10,Label11,Label12,Label13,Label14,Label15)

Then:
B4X:
Sub Button1_Click
    For i = 0 To TargetLabels.Length - 1
        TargetLabels(i).TextColor = Colors.Red
    Next
End Sub

Sub Button2_Click
    For i = 0 To TargetLabels.Length - 1
        TargetLabels(i).TextColor = Colors.Gray
    Next
End Sub


Example attached
 

Attachments

  • LblId.zip
    9.7 KB · Views: 161
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
You can use the labels' .TAG string property to assign a unique ID for each label.
Thanks JohnC

The Tags were already in place [forgotten], with your reminder the penny dropped.

Regards Roger


Many thanks stevel05.
Another option.

Regards Roger
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…