I want to set the label width at runtime; initially, I set it to a maximum I want to allow (85%x) but if the text width (+ label hor.paddings) is less than this max the label width should be set to this measure.What exactly do you want to do with cnv.MeasureStringWidth?
Sub btnShow_Click
IME1.HideKeyboard
lbl9Patch.Text = EditText1.Text
Dim cnv As Canvas
cnv.Initialize(lbl9Patch)
Dim MeasuredStringWidth As Int
MeasuredStringWidth = cnv.MeasureStringWidth(lbl9Patch.Text, lbl9Patch.Typeface, lbl9Patch.TextSize)
SetNinePatchDrawable(lbl9Patch, "whiteleft")
If MeasuredStringWidth < mBalloonMaxWidth Then
lbl9Patch.Width = MeasuredStringWidth + lbl9Patch.Padding(0) + lbl9Patch.Padding(2)
lbl9Patch.Background = Null
SetNinePatchDrawable(lbl9Patch, "whiteleft")
End If
AdjustLabelHeight(lbl9Patch)
End Sub
Tried but it's not enough (tried but watching Germany - Serbia U21, 3-0 first half )I'm right now thinking that maybe I should use the same trick you used in the AdjustLabelHeight routine, I have to add a temporary label.
Sub btnShow_Click
IME1.HideKeyboard
lbl9Patch.Text = EditText1.Text
Dim lblDummy As Label
lblDummy.Initialize("")
lblDummy.Visible = False
Activity.AddView(lblDummy, 0, 0, lbl9Patch.Width, lbl9Patch.Height)
lblDummy.Typeface = lbl9Patch.Typeface
lblDummy.TextSize = lbl9Patch.TextSize
lblDummy.Text = lbl9Patch.Text
SetNinePatchDrawable(lblDummy, "whiteleft")
Dim cnv As Canvas
cnv.Initialize(lblDummy)
Dim MeasuredStringWidth As Int
MeasuredStringWidth = cnv.MeasureStringWidth(lblDummy.Text, lblDummy.Typeface, lblDummy.TextSize)
MeasuredStringWidth = MeasuredStringWidth + lblDummy.Padding(0) + lblDummy.Padding(2)
lblDummy.RemoveView
If MeasuredStringWidth < mBalloonMaxWidth Then
lbl9Patch.Width = MeasuredStringWidth
End If
SetNinePatchDrawable(lbl9Patch, "whiteleft")
AdjustLabelHeight(lbl9Patch)
End Sub
But then I cannot use MeasureStringWidth on it.Here you are independant of the Label and use the minimum memory space.
I probably shouldn't do these tests while watching the football game and without having lunch yet ...Sure you can!!!
In: MeasuredStringWidth = cnv.MeasureStringWidth(lblDummy.Text, lblDummy.Typeface, lblDummy.TextSize)
you give everything you need for the calculation, independant of the target view!
Have you test it?
I am sure you haven't!
But I have tested it!
Certanly! I have to use:I probably
No, since lbl9Patch has the default padding, zeros.Certanly! I have to use:
MeasuredStringWidth = cnv.MeasureStringWidth(lbl9Patch.Text, lbl9Patch.Typeface, lbl9PatchTextSize)
Sub AdjustLabel(Lbl As Label)
Dim cnv As Canvas
Dim bmp As Bitmap
bmp.InitializeMutable(2dip, 2dip)
cnv.Initialize2(bmp)
Dim MeasuredStringWidth As Int 'ignore
MeasuredStringWidth = cnv.MeasureStringWidth(Lbl.Text, Lbl.Typeface, Lbl.TextSize)
If MeasuredStringWidth < Lbl.Width - Lbl.Padding(0) - Lbl.Padding(2) Then
Lbl.Width = MeasuredStringWidth + Lbl.Padding(0) + Lbl.Padding(2)
Else
Dim lblDummy As Label
lblDummy.Initialize("")
Dim Parent As Panel = Lbl.Parent
Parent.AddView(lblDummy, 0, 0, Lbl.Width - Lbl.Padding(0) - Lbl.Padding(2), Lbl.Height - Lbl.Padding(1) - Lbl.Padding(3))
lblDummy.Text = Lbl.Text
lblDummy.TextSize = Lbl.TextSize
lblDummy.Typeface = Lbl.Typeface
Dim su As StringUtils
Lbl.Height = su.MeasureMultilineTextHeight(lblDummy, Lbl.Text) + Lbl.Padding(1) + Lbl.Padding(3)
lblDummy.RemoveView
End If
End Sub
Sleep(2000)
Label2.Text = "Perfect"
AdjustLabel(Label2)
Sub AdjustLabel(Lbl As Label, MaxWidth As Int)
Dim cnv As Canvas
Dim bmp As Bitmap
bmp.InitializeMutable(2dip, 2dip)
cnv.Initialize2(bmp)
Dim MeasuredStringWidth As Int 'ignore
MeasuredStringWidth = cnv.MeasureStringWidth(Lbl.Text, Lbl.Typeface, Lbl.TextSize)
If MeasuredStringWidth < MaxWidth - Lbl.Padding(0) - Lbl.Padding(2) Then
Lbl.Width = MeasuredStringWidth + Lbl.Padding(0) + Lbl.Padding(2)
Else
Lbl.Width = MaxWidth 'reset the original width
Dim lblDummy As Label
lblDummy.Initialize("")
Dim Parent As Panel = Lbl.Parent
Parent.AddView(lblDummy, 0, 0, Lbl.Width - Lbl.Padding(0) - Lbl.Padding(2), Lbl.Height - Lbl.Padding(1) - Lbl.Padding(3))
lblDummy.Text = Lbl.Text
lblDummy.TextSize = Lbl.TextSize
lblDummy.Typeface = Lbl.Typeface
Dim su As StringUtils
Lbl.Height = su.MeasureMultilineTextHeight(lblDummy, Lbl.Text) + Lbl.Padding(1) + Lbl.Padding(3)
lblDummy.RemoveView
End If
End Sub
With "max width" I think you're referring to what I named mBalloonMaxWidth in my test project; I tried your new routine, passing this value to it, but the problem seems to be the same:Your result is not surprising to me.
The current adjustment routine compares the text width with the current label width.
In your case, the Label width was already reduced and the new text width is wider than the current label width.
If you want to call the routine several times you must add the max width to the routine and memorize the original width of the labels.
Sub AdjustLabel(Lbl As Label, OriginalWidth As Int, OriginalHeight As Int)
Dim cnv As Canvas
Dim bmp As Bitmap
bmp.InitializeMutable(2dip, 2dip)
cnv.Initialize2(bmp)
Dim MeasuredStringWidth As Int 'ignore
MeasuredStringWidth = cnv.MeasureStringWidth(Lbl.Text, Lbl.Typeface, Lbl.TextSize)
Lbl.Width = OriginalWidth
Lbl.Height = OriginalHeight
If MeasuredStringWidth < OriginalWidth - Lbl.Padding(0) - Lbl.Padding(2) Then
Lbl.Width = MeasuredStringWidth + Lbl.Padding(0) + Lbl.Padding(2)
Else
Dim lblDummy As Label
lblDummy.Initialize("")
Dim Parent As Panel = Lbl.Parent
Parent.AddView(lblDummy, 0, 0, Lbl.Width - Lbl.Padding(0) - Lbl.Padding(2), Lbl.Height - Lbl.Padding(1) - Lbl.Padding(3))
lblDummy.Text = Lbl.Text
lblDummy.TextSize = Lbl.TextSize
lblDummy.Typeface = Lbl.Typeface
Dim su As StringUtils
Lbl.Height = su.MeasureMultilineTextHeight(lblDummy, Lbl.Text) + Lbl.Padding(1) + Lbl.Padding(3)
lblDummy.RemoveView
End If
End Sub
In the Activity_create I set a global variable (mBalloonMaxWidth) to 85%x and the Label width to this value.How did you get the image above?
The width of the Label is too small!
Tested (project attached).[I have yet to test your new routine...]
No, it gets even worse.maybe should I make the 9png smaller?
Sub AdjustLabel(Lbl As Label, OriginalWidth As Int, OriginalHeight As Int)
Dim cnv As Canvas
Dim bmp As Bitmap
bmp.InitializeMutable(2dip, 2dip)
cnv.Initialize2(bmp)
Dim MeasuredStringWidth As Int 'ignore
MeasuredStringWidth = cnv.MeasureStringWidth(Lbl.Text, Lbl.Typeface, Lbl.TextSize) + 2dip
Lbl.Width = OriginalWidth
Lbl.Height = OriginalHeight
If MeasuredStringWidth < OriginalWidth - Lbl.Padding(0) - Lbl.Padding(2) Then
Lbl.Width = MeasuredStringWidth + Lbl.Padding(0) + Lbl.Padding(2)
Else
Dim lblDummy As Label
lblDummy.Initialize("")
Dim Parent As Panel = Lbl.Parent
Parent.AddView(lblDummy, 0, 0, Lbl.Width - Lbl.Padding(0) - Lbl.Padding(2), Lbl.Height - Lbl.Padding(1) - Lbl.Padding(3))
lblDummy.Text = Lbl.Text
lblDummy.TextSize = Lbl.TextSize
lblDummy.Typeface = Lbl.Typeface
Dim su As StringUtils
Lbl.Height = su.MeasureMultilineTextHeight(lblDummy, Lbl.Text) + Lbl.Padding(1) + Lbl.Padding(3)
lblDummy.RemoveView
End If
End Sub
Empirically, adding an "X" to lblDummy.Text, seems to give the desired result, but for the moment I have tried only one device and I don't really like the "empirical mode".
"Melius est abundare quam deficere"; at most the ball will be slightly too wide, with empty space, always better than having illegible letters or cut words.Maybe you could increase the value.