Android Question SpannableString Typeface Normal - Whats Wrong

konradwalsh

Active Member
Licensed User
Longtime User
Referring to this thread - https://www.b4x.com/android/forum/t...formatted-text-is-possible.66976/#post-601440

I am trying to adapt the code to set the text to Normal. I want to create a very lite word processor feature. The user selects text and sets a typeface. I want the user to be able to 'undo' or change the style.

Why doesnt the following code work to do this:
B4X:
Sub setTextToNormal
    refRTF.Target = notepad_txt_NoteWriting
    Dim selStart As Int = refRTF.RunMethod("getSelectionStart")
    Dim selEnd As Int = refRTF.RunMethod("getSelectionEnd")
    Dim SpannableString As JavaObject ' in Java this object is called SpannableStringBuilder
    SpannableString = refRTF.RunMethod("getText") ' includes the text with all spans
    ' We create a span object from type StyleSpan
   
    Dim boldSpan As JavaObject

    boldSpan.InitializeNewInstance("android.text.style.StyleSpan", Array As Object(Typeface.STYLE_NORMAL))
    SpannableString.RunMethod("setSpan", Array As Object(boldSpan, selStart , selEnd, 33))
    End Sub


when this code works to set it to bold
B4X:
Sub setTextToBold
    refRTF.Target = notepad_txt_NoteWriting
    Dim selStart As Int = refRTF.RunMethod("getSelectionStart")
    Dim selEnd As Int = refRTF.RunMethod("getSelectionEnd")
    Dim SpannableString As JavaObject ' in Java this object is called SpannableStringBuilder
    SpannableString = refRTF.RunMethod("getText") ' includes the text with all spans
    ' We create a span object from type StyleSpan
    Dim boldSpan As JavaObject
    boldSpan.InitializeNewInstance("android.text.style.StyleSpan", Array As Object(Typeface.STYLE_BOLD))
    SpannableString.RunMethod("setSpan", Array As Object(boldSpan, selStart , selEnd, 33))
End Sub
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
From a quick look, it appears that the spans are cumulative. In other words, you need to manage the spans so they don't overlap which would mean removing and re-adding spans that would otherwise overlap.

I think It would be easier to do using CSBuilder.
 
Upvote 0
Top