Android Question Label Text Size Problem

AbbasMohammed

Member
Licensed User
Longtime User
Dears,
Depending on AutTextSize class i tried this code but it didnt work till now
Main:
B4X:
Sub Globals
'    Dim CustomView1 As AutoTextSizeLabel
 
    Dim TarLabel As Label
    Dim Pan As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
'    Activity.LoadLayout("1")
'    CustomView1.Text = "This is a very long sentence."
  
    TarLabel.Initialize("")
    Pan.Initialize("")
    TarLabel.Text="Hellowasdfasdfas"&CRLF&"asdfasdf"
    TarLabel.TextSize=SetTextSize(TarLabel)
    Pan.AddView(TarLabel,0,0,100%x,10%y)
    Activity.AddView(Pan,0,0,100%x,100%y)
 
 
 
End Sub

Private Sub SetTextSize(Lbl As Label) As Float
    Dim TempPan As Panel
    TempPan.Initialize("")
    Activity.AddView(TempPan,0,0,100%x,100%y)
    TempPan.AddView(Lbl,0,0,100%x,10%y)
    Dim x As Float
    x=TxtSiz(Lbl,Lbl.Text)
    For i=0 To Activity.NumberOfViews-1
    Activity.RemoveViewAt(i)
    Next
    Return x
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Public Sub TxtSiz(lbl As Label,value As String) As Float
    lbl.Text = value
    Dim multipleLines As Boolean = lbl.Text.Contains(CRLF)
    Dim size As Float
    For size = 2 To 80
        If CheckSize(size, multipleLines,lbl) Then Exit
    Next
    size = size - 0.5
    If CheckSize(size, multipleLines,lbl) Then size = size - 0.5
    Return size
End Sub

'returns true if the size is too large
Private Sub CheckSize(size As Float, MultipleLines As Boolean,labl As Label) As Boolean
    Private cvs As Canvas
    Private bmp As Bitmap
    bmp.InitializeMutable(1,1) 'ignore
    cvs.Initialize2(bmp)
    Private su As StringUtils
    labl.TextSize = size
    If MultipleLines Then
        Return su.MeasureMultilineTextHeight(labl, labl.Text) > labl.Height
    Else
        Return cvs.MeasureStringWidth(labl.Text, labl.Typeface, size) > labl.Width Or _
            su.MeasureMultilineTextHeight(labl, labl.Text) > labl.Height
    End If
End Sub

Log Error is:

** Activity (main) Create, isFirst = true **
** Activity (main) Create, isFirst = true **
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
at android.view.ViewGroup.addView(ViewGroup.java:3210)
at android.view.ViewGroup.addView(ViewGroup.java:3186)
at anywheresoftware.b4a.objects.PanelWrapper.AddView(PanelWrapper.java:63)
at b4a.example.main._activity_create(main.java:355)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at b4a.example.main.afterFirstLayout(main.java:100)
at b4a.example.main.access$100(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:78)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
 
Top