Android Question using variable from designer confusion.


Licensed User
I have read the beginners guide to using the designer, but I don't understand how to use the variables in my code.
For example, in the designer I make a label called Lab2.
Then in my code

Sub Activity_Create(FirstTime As Boolean)


and I have a sub routine

Sub usocket_PacketArrived (uu As UDPPacket)
Dim msg As String
msg = BytesToString(uu.Data, uu.Offset, uu.Length, "UTF8")
Lab2.Text = " " & msg.Replace("Temp=", "Temperature =") & Chr(176) & "C"
' Msgbox("Message received: " & msg, "") ' works ok
End Sub

But I get an error that Lab2 is not declared. If I don't refer to the variable Lab2 then the label appears as it should.
Can someone point me to the documentation which explains what I should do please? (As far as I understand it the introduction doesn't explain this.)

This is my first attempt at B4A so don't be too hard on me yet!


Licensed User
In the designer Tools you have "Generte member". You should mark the label so that it is declared in the globals,
or you can declare it yourself there "Dim lab2 as label".
After that you can refer to the label in the code.


Licensed User
Thanks guys. And Manfred, that was a lot of effort to help me which made it very simple for me. Thanks.

But although the label now gets updated with the correct text it only appears where it was first designed in the designer. I have moved it in the designer and set its color but nothing I do in the designer has any effect so far. If I connect the designer to my device then the label shows correctly. I make sure I save the layout before I compile. I've tried closing the designer and opening again and the design is as I made it but the compiled app doesn't change. If I set the position and size in my code that works but that's not the idea of a graphical designer.

EDIT- Solved?
In the Designer-
If you use File|Save then the design reverts to the first layout variant. So maybe since I didn't notice that and I was changing the design for a variant which was not matched to the device I am testing on.
Last edited: