Android Question Save/Load text formatted

ilan

Expert
Licensed User
Longtime User
hi

is it possible to load/save formatted text? like text with specific color, text size, bold, underlined,...
so save it and load it from a text file?

thanx
 

DonManfred

Expert
Licensed User
Longtime User
No. But you can save the informations to rebuild them using CSBuilder
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
No. But you can save the informations to rebuild them using CSBuilder

yes i could do that, i just thought that it would be awesome if i could create a formatted text using CSbuilder and just save/load it instead of recreating it again.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you can save as html file but then you need a webview or some htmltextview if that exists to see the fancy stuff.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
you can save as html file but then you need a webview or some htmltextview if that exists to see the fancy stuff.

Yes, i was thinking of such a solution but i need to store the properties and use that formatted text on native views like labels, buttons,... so will need to think on a different solution.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
So this is not the solution.
Oh well. The only other thing I could find on the web as it relates to CS-Strings is "Parcel", but that only relates to inter-process communications. The only other thing I could think of is to create a class around CSBuilder that you call instead of CSBuilder. This class then records methods and parameters internally that could be written out to some sort of command string. That command string can then be fed to the same class to recreate the CS-String.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Make something in Java (Java online) that passes as a CSBuild parameter as charsequence and then transform it into a Byte Array to be saved in a file and vice versa
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Make something in Java (Java online) that passes as a CSBuild parameter as charsequence and then transform it into a Byte Array to be saved in a file and vice versa


I am planning to make such a lib soon and will share it when i have something :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't see how Java will help here. Always prefer to write B4X code over Java...

1. Create a class named PCSBuilder (Persistent CSBuilder):
B4X:
Sub Class_Globals
   Private cs As CSBuilder  
   Private data As List
   Private ser As B4XSerializator
End Sub

Public Sub Initialize As PCSBuilder
   cs.Initialize
   data.Initialize
   If False Then
       'to avoid obfuscation issues.
       CallSub(Me, "Color")
       CallSub(Me, "Append")
       CallSub(Me, "Pop")
       CallSub(Me, "PopAll")
   End If
   Return Me
End Sub

Public Sub FromBytes(b() As Byte) As PCSBuilder
   Dim list As List = ser.ConvertBytesToObject(b)
   For Each o() As Object In list
       If o.Length = 1 Then
           CallSub(Me, o(0))
       Else
           CallSub2(Me, o(0), o(1))
       End If
   Next
   Return Me
End Sub

Public Sub ToBytes As Byte()
   Return ser.ConvertObjectToBytes(data)
End Sub

Public Sub Color (clr As Int) As PCSBuilder
   cs.Color(clr)
   data.Add(Array("color", clr))
   Return Me
End Sub

Public Sub Append (Text As Object) As PCSBuilder
   cs.Append(Text)
   data.Add(Array("append", Text))
   Return Me
End Sub

Public Sub Pop As PCSBuilder
   cs.Pop
   data.Add(Array("pop"))
   Return Me
End Sub

Public Sub PopAll As PCSBuilder
   cs.PopAll
   data.Add(Array("popall"))
   Return Me
End Sub

Public Sub ToCharSequence As Object
   Return cs
End Sub

Usage example:
B4X:
Dim p As PCSBuilder
Activity.Title = p.Initialize.Append("hello ").Color(Colors.Red).Append("world").PopAll.ToCharSequence
Dim b() As Byte = p.ToBytes
Label.Text = p.Initialize.FromBytes(b).ToCharSequence

Save and load bytes: [B4X] Bytes To File
Or use KVS2.

The class is not complete. You should add the other methods as well.
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
This is how i wanted to make it. Convert object to bytes save with kvs and read back.

Erel doesnot leave any challenge for us he is hunting all likes in the forum :)
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
ok i have something to show :)
i have added all possible spans (please see bellow the limitations)
i have not compiled the classes to a b4a lib to let you the possibility to see the code and maybe modify it. So now you can create any formatted text in b4a and save it to a DB and then load it again and show on any view. (Have Fun with it!)

BIG BIG BIG thanx to @Erel!!

EDIT: there are 2 limitations:

1, When you use the Clickable span you can put only 1 EventName in 1 PCSBuilder object!
2, i have not added the Image span (i was too tired, sorry)

(I think this belongs to the snipped section)
 

Attachments

  • CSBuilderPlus.zip
    5.7 KB · Views: 447
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I think this belongs to the snipped section
It is best to post it in the additional libraries forum.

i have not compiled the classes to a b4a lib to let you the possibility to see the code and maybe modify it.
There is no reason to compile it. It is better to distribute the source code. Developers can always compile it to a library if they like.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
i have not added the Image span (i was too tired, sorry)
i have added the Image span. Please make correction. I think it is missing adding the bitmap to the list. I added the code to the class as shown below:
B4X:
'Adds image span: bitmap,width,height,True or False
Public Sub Image(bmp As Bitmap, Width As Int, Height As Int, Baseline As Boolean) As PCSBuilder  'Mahares
    cs.Image(bmp, Width, Height, Baseline)
    'Some additional code must be missing here to add the image to the list
    Return Me
End Sub
If my code is correct, which I doubt it is, when you save a pcsbuilder with an image to a file, then retrieve the file and display it on a label for instance, it does not show the image. It only shows the formatted text. Something is not kosher.
 
Last edited:
Upvote 0
Top