Android Question Doubt on CharSequence / CSBuilder usage

FERNANDO SILVEIRA

Active Member
Licensed User
Hello guys,

I read this topic and was trying to implement some text enhancements to my app. It happens that the CS elements seems to work directly on the view and, in my case, I work literals and fields in string literals and, at the end, combine these string literals on the final label.text view object. Then I noticed that this approach (use of intermediate literals) produce no effect on the result, resulting no formatting at all at the end, as can be seen in the attached image,

It worked, however, when I prepare the CS string and send it right to the label.text view.

Is there other way to apply formatting to my resulting text? HTML tags <B> </B> <I> </I> would be enough.

Regards,
Fernando

B4X:
        Select wCodEvento
            Case     1    '  *** USING CS ON THE 1ST CASE JUST FOR TEST ***
               
                cs.Initialize.Append(IIF(wRegistro(7) = Null, "Nascid" & wSufixo, "Nascid" & wSufixo & " em " & wRegistro(7)) & " no dia ")
                cs.Bold.Color(Colors.Green).Append(wDataEvento).Pop.Pop 'two pops: the first removes the green color and the second removes the bold style
                cs.Append(". ").PopAll
                wTxtNascimento = cs
'                lblDetalhePessoa.text = cs
'                Return

            Case     2    ' 
                wTxtCasamento = wTxtCasamento & "Casad" & wSufixo & " no dia " & wDataEvento & " com " & wRegistro(8) & IIF(wRegistro(7) = Null, "", ", em " & wRegistro(7)) & ". "
            Case     3    ' 
                wTxtBatizado = "Batizad" & wSufixo & " no dia " & wDataEvento & IIF(wRegistro(7) = Null, "", ", em " & wRegistro(7)) & ". "
        End Select
    Next
 


' *** CS text (wTxtNascimento) prepared above does nothing when combined as below
    lblDetalhePessoa.text = wTxtApelido & wTxtIdade & wTxtNascimento & wTxtPaiMae & wTxtCasamento & wTxtFalecimento & wTxtCrismado & wTxt1Comunhao & wTxtFormado & wTxtDivorcio & wTxtEmancipado

upload_2018-6-5_13-47-3.png
 

Mahares

Expert
Licensed User
Longtime User
Is there other way to apply formatting to my resulting text
Fernando: I think you have to do something like this for it to work. This will work:
B4X:
Dim cs As CSBuilder
    Dim strvar1 As String ="BRAZIL IS IN WORLD CUP 2018."
    Dim strvar2 As String =" FERNANDO IS LOOKING FORWARD"
    Dim strvar3 As String =" TO A WIN."  
    cs.initialize.Color(Colors.Red).Append(strvar1).Color(Colors.Blue).Append(strvar2) _
    .Color(Colors.Green).Append(strvar3).PopAll
    lbl.Text=cs
 
Upvote 0

FERNANDO SILVEIRA

Active Member
Licensed User
Fernando: I think you have to do something like this for it to work. This will work:
B4X:
Dim cs As CSBuilder
    Dim strvar1 As String ="BRAZIL IS IN WORLD CUP 2018."
    Dim strvar2 As String =" FERNANDO IS LOOKING FORWARD"
    Dim strvar3 As String =" TO A WIN." 
    cs.initialize.Color(Colors.Red).Append(strvar1).Color(Colors.Blue).Append(strvar2) _
    .Color(Colors.Green).Append(strvar3).PopAll
    lbl.Text=cs

Hello, Mahares
You right, it works doin this way when the output is a label.text. However I have to prepare my text string in advance and only when everything is ready concatenate these strings to label.text.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
When everything is ready and all variables are defined, that is when you create your csbuilder and stick it in the label text. That is what my example does, unless I am not understanding fully the issue
 
Upvote 0

FERNANDO SILVEIRA

Active Member
Licensed User
When everything is ready and all variables are defined, that is when you create your csbuilder and stick it in the label text. That is what my example does, unless I am not understanding fully the issue

Look at this code:
B4X:
    Dim cs As CSBuilder
    Dim strvar1 As String ="BRAZIL IS IN WORLD CUP 2018."
    Dim strvar2 As String =" FERNANDO IS LOOKING FORWARD"
    Dim strvar3 As String =" TO A WIN."
    cs.initialize.Color(Colors.Red).Append(strvar1).Color(Colors.Blue).Append(strvar2) _
    .Color(Colors.Green).Append(strvar3).PopAll
    Label1.Text=cs                    ' <=== WORKS FINE
  
    Dim strvar4 As String
    Dim strvar5 As String

    strvar4 = cs.initialize.Color(Colors.Red).Append(strvar1).Color(Colors.Blue).Append(strvar2) _
    .Color(Colors.Green).Append(strvar3).PopAll

    strvar5 = cs.initialize.Color(Colors.Red).Append(" Mahares is").Color(Colors.Blue).Append(" a nice ") _
    .Color(Colors.Green).Append("guy").PopAll
  
    Label2.Text = strvar4 & strvar5    ' <=== DOES NOTHING - And I can only set label2.text at the end, otherwise the code would become a monster

In my case, a single label2.text is the result of concatenation of up to 10 strvars...
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
In my case, a single label2.text is the result of concatenation of up to 10 strvars

You can create multiple csbuilders (one for each variable string) and concatenate them into the label text . See example below:
B4X:
Dim cs As CSBuilder
    Dim strvar As String ="BRAZIL CAN WIN THE WORLD CUP IN 2018"
    cs.initialize.Color(Colors.Red).Append(strvar.SubString2(0, 5)).Color(Colors.Blue).Append(strvar.SubString2(5, 8)) _
    .Color(Colors.Green).Append(strvar.SubString(8)).PopAll
    Dim cs1 As CSBuilder
    Dim strvar As String ="  FERNANDO"
    cs1.initialize.Color(Colors.Magenta).Append(strvar).PopAll
    Dim cs3 As CSBuilder
    cs3.Initialize.Append(cs).Append(cs1)
    lbl.Text=cs3
 
Upvote 0
Top