Android Question Wordwrap to a canvas

B4AinNY

Member
Licensed User
Hi, I'm trying to find a way to print some long string on a canvas with wordwrapping.
Eventually I'll be sending this canvas to a standard 8.5 x 11 inch printer.

I have seen the post
https://www.b4x.com/android/forum/threads/draw-multiline-text-on-canvas.42933/
which is great if you know where to put the CRLF, but what if you just have a long
string without embedded CRLF. Is there some useful approach someone could suggest.

So I have
' Create a bitmap ( bdw ) to draw on, and then initialize it
Dim bdw As Bitmap ' This is the Bitmap we are drawing on
Dim Const intPageWidth As Int = 850dip
Dim Const intPageHeight As Int = 1100dip
Dim Const intOneInch As Int = 100dip
bdw.InitializeMutable( pageWidth, pageHeight)

Dim strLngStr as String = " ...... this may be a few paragraphs "​
Now if this string could fit on a single printed line I could write
intLeft = pageWidth /2 ,
cvs.DrawText("strLngStr",intLeft, intTop, _
Typeface.DEFAULT_BOLD, 20, Colors.Blue, "CENTER")​
But let's say I want to fit into a box with a 15% of the pagewidth
I don't know how much text fits in that width so I don't know
where to break the lines.

Any ideas ?

- Jeff
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
How can it be? It is very useful.

Here:
B4X:
Sub DrawView (cvs As Canvas, v As View)
   Dim cjo As JavaObject = cvs
   Dim jv As JavaObject = v
   jv.RunMethod("draw", Array(cjo.GetFieldJO("canvas")))
End Sub

Example:
B4X:
Sub Activity_Click
   Dim c As Canvas
   c.Initialize(Activity)
   DrawView(c, Label1)
   Activity.Invalidate
End Sub

Quoting Android documentation:
"The view must have already done a full layout before this function is called."
 
Upvote 0
Top