Text Width??

Bill Norris

Active Member
Licensed User
Longtime User
In stringutils, we have the MeasureMultilineTextHeight feature to help determine the proper height of a label with multiple lines of text. I would like a similar feature to get the width of a text string, so as to set the width of a label dynamically based on the length of the text assigned to it. Any ideas on getting some kind of "text width" ??
 

Bill Norris

Active Member
Licensed User
Longtime User
RE:

To experiment with this, I put in the following code in a button click event:

B4X:
Dim C As Canvas
Dim X As Int
X=C.MeasureStringWidth("textwidth",default_font,30)
Msgbox(x,"")

I received a Null Pointer Exception error when I ran it. BTW, value of default_font is assigned in activity_create, and functions as desired throughout the app.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You must initialize the Canvas:
B4X:
Dim C As Canvas
C.Initialize(Activity)
Dim X As Int
X = C.MeasureStringWidth("textwidth", default_font, 30)
Msgbox(x,"")
Best regards.
 
Upvote 0
Top