Android Question Label New Height and Width

BarryW

Active Member
Licensed User
Longtime User
hi. i set my label width = -2 and height = -2 and i set a long text. Is there a way to get its actual width and height. It always give me a -2 width and height.
 

ilan

Expert
Licensed User
Longtime User
hi. i set my label width = -2 and height = -2 and i set a long text. Is there a way to get its actual width and height. It always give me a -2 width and height.

the label size doesnot change when you add text to it.
measuring the textsize with C.MeasureStringWidth is an option.

something like this:

B4X:
Sub measureText (txt As String, txtSize As Int, txtTypeface As Typeface) As Float()
    Private c As Canvas
    c.Initialize(Activity)
    Private wh(2) As Float
    wh(0) = c.MeasureStringWidth(txt, Typeface.DEFAULT, txtSize)
    wh(1) = c.MeasureStringHeight(txt, Typeface.DEFAULT, txtSize+2dip)
    Return wh
End Sub

how to use:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Text = "Get my width/height please"
    lbl.TextColor = Colors.White
    lbl.TextSize = 18
    lbl.Color = Colors.Blue
   
    Dim size(2) As Float = measureText(lbl.Text, lbl.TextSize, Typeface.DEFAULT)
    Log(size(0) & ":" & size(1))
    Activity.AddView(lbl,0,0,size(0),size(1))
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…