Android Question How to add "QuoteSpan" to CSBuilder textstyle?

fredo

Well-Known Member
Licensed User
Since Erel posted the really useful snippet "CSBuilder with leading margin" I've got curious if there were more styles possible to use with Erel's method.
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Layout")
   Sleep(0)
   Dim cs As CSBuilder
   cs.Initialize
   CallSub3(Me, "AddLeadingMarginSpan", cs, Array As Int(0dip, 30dip))
   cs.Append("1) First do text text text, text text text text text text text").PopAll
   CallSub3(Me, "AddLeadingMarginSpan", cs, Array As Int(0dip, 30dip))
   cs.Append(CRLF).Append("2) Second do text text text, text text text text text text text").PopAll
   Label1.Text = cs
End Sub

Sub AddLeadingMarginSpan(cs As Object, FirstAndRest() As Int)
   Dim span As JavaObject
   span.InitializeNewInstance("android.text.style.LeadingMarginSpan.Standard", Array(FirstAndRest(0), FirstAndRest(1)))
   Dim jo As JavaObject = cs
   jo.RunMethod("open", Array(span))
End Sub

As the "android.text.style.LeadingMarginSpan.Standard" is well documented on developer.android.com I looked for a quote-solution and found the "QuoteSpan" style.

But my attempt to imitate the wrapped SpannableStringBuilder
B4X:
...
    CallSub3(Me, "AddQuoteSpan", cs, Array As Int(Colors.Green, 20dip, 40dip))
    cs.Append(CRLF).Append("Quote:").Append(CRLF).Append("Second do text text text, text text text text text text text").PopAll
...   

Sub AddQuoteSpan(cs As Object, intColorGapwidthStripewidth() As Int)
   Dim span As JavaObject
   '
   ' --------------- --------------- --------------- ---------------
   'Not working:
   span.InitializeNewInstance("android.text.style.QuoteSpan", Array(intColorGapwidthStripewidth(0), intColorGapwidthStripewidth(1), intColorGapwidthStripewidth(2)))
   ' --------------- --------------- --------------- ---------------
   '
   Dim jo As JavaObject = cs
   jo.RunMethod("open", Array(span))
End Sub
led to a RuntimeException:
~e:java.lang.RuntimeException: Constructor not found.
~e: at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:94)
As it seems, my modest quarter-knowledge how JAVA and Android works under the hood is not enough to find a solution myself.

So what would be the right way to add "QuoteSpan" or "BulletSpan" to CSBuilder textstyle?
 

fredo

Well-Known Member
Licensed User
You should call the constructor that expects a single int parameter for the color.
Thank you for giving me the chance to learn a bit about this.

I wasn't aware of those subtle but important details.

So it works indeed:
11-03-_2018_12-40-17.png


with this code:
B4X:
...
Dim cs As CSBuilder
    cs.Initialize
    cs.Append(CRLF).Append("Quote:")
    CallSub3(Me, "AddQuoteSpan", cs, Colors.Blue)
    cs.Append(CRLF).Append("Second do text text text, text text text text text text text").PopAll
    Label1.Text = cs
    
...

Sub AddQuoteSpan(cs As Object, intColorGap As Int)
    Dim span As JavaObject
    span.InitializeNewInstance("android.text.style.QuoteSpan", Array(intColorGap))
    Dim jo As JavaObject = cs
    jo.RunMethod("open", Array(span))
End Sub
 
Top