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

Discussion in 'Code Snippets' started by Erel, Jan 31, 2019.

  1. Erel

    Erel Administrator 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)
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice