Android Code Snippet [B4X] Set CSBuilder or Text to a Label


Staff member
Licensed User
This sub accepts a string or CSBuilder and sets it as the Label's text. It is useful for cross platform solutions.
Note that the Text parameter type is Object.
Public Sub SetTextOrCSBuilderToLabel(xlbl As B4XView, Text As Object)
   #if B4A or B4J
   xlbl.Text = Text
   #else if B4i
   If Text Is CSBuilder And xlbl Is Label Then
       Dim lbl As Label = xlbl
       lbl.AttributedText = Text
       If GetType(Text) = "__NSCFNumber" Then Text = "" & Chr(Text)
       xlbl.Text = Text
   End If
   #end if
End Sub
In order to support FontAwesome or Material Icons characters (in B4i), the code needs to check whether the value is a number and if so it treats it as a unicode point value.

If you want to pass a number then convert it to a string first:
SetTextOrCSBuilderToLabel(lbl, "" & 123)