Android Question CSBuilder : Saving to file / Reading from file with preservation of formatting

Semen Matusovskiy

Well-Known Member
Licensed User
Hi, guys --

This topic indirectly linked with https://www.b4x.com/android/forum/t...ng-selected-csbuilder-text-in-edittext.98236/

To save/restore int , string, double is enough simple. For example, to create a map, then File.WriteMap, File.ReadMap.

But what about CSBuilder ? If to try map.put ("foo", cs), it will be an error in run-time - java.lang.ClassCastException: java.lang.String cannot be cast to android.text.SpannableStringBuilder

Any ideas ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
To save/restore int , string, double is enough simple. For example, to create a map, then File.WriteMap, File.ReadMap.
B4XSerializator is actually the best option.

But what about CSBuilder ?
There is no automatic way to serialize a CharSequence.

Eventually it is your code who creates the styled text. This means that you have a sub that receives some input and returns the CharSequence. You should save the input instead of the output.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Completely forgot about this.
I remembered it very well because it was useful to me, only I struggled to find it.
Maybe you could create a tutorial thread or add an example in the Thread that explains CharSequence.
Because it is very useful.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Yesterday I looked https://developer.android.com/reference/java/lang/CharSequence

Most functions doesn't look helpful for conversion CharSequence to a set of bytes, but, for example, codePoints and maybe chars look interesting. Unfortunatelly, API 24+ and I did not found examples.

codePoints
added in API level 24

public IntStream codePoints ()
Returns a stream of code point values from this sequence. Any surrogate pairs encountered in the sequence are combined as if by Character.toCodePoint and the result is passed to the stream. Any other code units, including ordinary BMP characters, unpaired surrogates, and undefined code units, are zero-extended to int values which are then passed to the stream.

chars
added in API level 24

public IntStream chars ()
Returns a stream of int zero-extending the char values from this sequence. Any char which maps to a surrogate code point is passed through uninterpreted.


Strange that Android did not offer any way from the beginning. I hoped that I lost something.
 
Upvote 0

npsonic

Active Member
Licensed User
There is way to preserve formatting with saved string as I have done this multiple times, but you can't use CSBuilder.

What I have done is using RichString and RichStringFormatter to create tokens that are defining formatting of the string.
Here's small example.

B4X:
Sub ReadFormattedText (Dir As String, Filename As String) As RichString
    Dim rsf As RichStringFormatter
    rsf.AddToken("{blue}",rsf.FORMAT_COLOR,Colors.Blue)
    rsf.AddToken("{bold}",rsf.FORMAT_STYLE,rsf.STYLE_BOLD)
    rsf.AddToken("{sub}",rsf.FORMAT_SUBSCRIPT,Null)
    Dim rs As RichString : rs.Initialize(File.ReadString(Dir,Filename))
    Return rsf.Format(rs,rsf.SPAN_INCLUSIVE_INCLUSIVE)
End Sub
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Hi, guys --

This topic indirectly linked with https://www.b4x.com/android/forum/t...ng-selected-csbuilder-text-in-edittext.98236/

To save/restore int , string, double is enough simple. For example, to create a map, then File.WriteMap, File.ReadMap.

But what about CSBuilder ? If to try map.put ("foo", cs), it will be an error in run-time - java.lang.ClassCastException: java.lang.String cannot be cast to android.text.SpannableStringBuilder

Any ideas ?

Thanks; I will add this class (PCSBuilder) and serialize my troublesome CSBuilder and post it as a file.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Thanks; I will add this class (PCSBuilder) and serialize my troublesome CSBuilder and post it as a file.

RBS

Using the PCSBuilderPlus class, as posted here:
https://www.b4x.com/android/forum/threads/save-load-text-formatted.92263/#post-583864

Nearly got this working but get this error when trying to retrieve the byte array with ToBytes (made Public):

java.lang.RuntimeException: Cannot serialize object: android.widget.EditText{4adef6f VFED..CL. ......ID 0,264-1056,1023 #b}

EditText is my SQL EditText. Error is resolved if I don't make the CSBuilder clickable, so if I don't do in my formatting Sub:

B4X:
cs.EnableClickEvents (edtSQL)

Any idea how to solve this?

RBS
 
Upvote 0
Top