Android Question How to use AutoTextSizeLabel manually?

EduardoElias

Well-Known Member
Licensed User
Longtime User
I am trying to create AutoTextSizeLabel manually on the code. This code does not have layout.

I am using an expanded version I made of AutoTextSizeLabel to resemble more a Label and be more easily changed on the source code.

However I cannot find a way to make it work when I create manually.


B4X:
Sub Class_Globals

    Private cvs As Canvas

    Private mLbl As Label

    Private su As StringUtils

    Private EventName As String 'ignore

    Private CallBack As Object 'ignore

End Sub



Public Sub Initialize (vCallback As Object, vEventName As String)

    EventName = vEventName

    CallBack = vCallback

End Sub



public Sub Initialize2(lbl As Label, vEventName As String)

    EventName = vEventName

    CallBack = Null

    mLbl = lbl

    Dim bmp As Bitmap

    bmp.InitializeMutable(1,1) 'ignore

    cvs.Initialize2(bmp)

End Sub



Public Sub DesignerCreateView(Base As Panel, lbl As Label, props As Map)

    Dim bmp As Bitmap

    bmp.InitializeMutable(1,1) 'ignore

    cvs.Initialize2(bmp)

    Dim parent As Panel = Base.Parent

    

    parent.AddView(lbl, Base.Left, Base.Top, Base.Width, Base.Height)

    Base.RemoveView

    mLbl = lbl

    mLbl.Padding = Array As Int(0, 0, 0, 0)

    Dim jo As JavaObject = mLbl

    jo.RunMethod("setIncludeFontPadding", Array(False))

    setText(mLbl.Text)

End Sub





Public Sub setText(value As String)

    mLbl.Text = value

    Dim multipleLines As Boolean = mLbl.Text.Contains(CRLF)

    Dim size As Float

    For size = 2 To 80

        If CheckSize(size, multipleLines) Then Exit

    Next

    size = size - 0.5

    If CheckSize(size, multipleLines) Then size = size - 0.5

    mLbl.TextSize = size

End Sub



'returns true if the size is too large

Private Sub CheckSize(size As Float, MultipleLines As Boolean) As Boolean

    mLbl.TextSize = size

    If MultipleLines Then

        Return su.MeasureMultilineTextHeight(mLbl, mLbl.Text) > mLbl.Height

    Else

        Return cvs.MeasureStringWidth(mLbl.Text, mLbl.Typeface, size) > mLbl.Width Or _

            su.MeasureMultilineTextHeight(mLbl, mLbl.Text) > mLbl.Height

    End If

End Sub



Public Sub getText As String

    Return mLbl.Text

End Sub



Public Sub setVisible(Value As Boolean)

    mLbl.Visible = Value

End Sub



public Sub getVisible As Boolean

    Return mLbl.Visible

End Sub



public Sub setTextSize(Value As Int)

    mLbl.TextSize = Value

End Sub



public Sub getTextSize As Int

    Return mLbl.TextSize

End Sub



public Sub RequestFocus

    mLbl.RequestFocus

End Sub



Private Sub mLbl_Click

    Dim e As String = EventName&"_Click"

    If SubExists(CallBack, e) Then

        CallSub(CallBack, e)

    End If

End Sub



Public Sub getGravity As Int

    Return mLbl.Gravity

End Sub



Public Sub setGravity(value As Int)

    mLbl.Gravity = value

End Sub



Public Sub setTypeface(value As Typeface)

    mLbl.Typeface = value

End Sub



public Sub getTypeface As Typeface

    Return mLbl.Typeface

End Sub



public Sub getTextColor As Int

    Return mLbl.TextColor

End Sub



Public Sub setTextColor(value As Int)

    mLbl.TextColor = value

End Sub



public Sub View As Label

    Return mLbl

End Sub



public Sub setEnabled(Value As Boolean)

    mLbl.Enabled = Value

End Sub



public Sub getEnabler As Boolean

    Return mLbl.Enabled

End Sub



public Sub setHeight(Value As Int)

    mLbl.Height = Value

End Sub



public Sub getHeight As Int

    Return mLbl.Height

End Sub



public Sub BringToFront

    mLbl.BringToFront

End Sub

I am trying this code:

B4X:
                Dim lbl As Label
                lbl.Initialize("")
                Parent.AddView(lbl, bt(i, j).Left+2dip, bt(i, j).Top+10dip, FDefaultWidth-2dip, 30dip)
                
                Dim L As AutoTextSizeLabel
                L.Initialize2(lbl, "")

I am trying to use this ways, but it crashes:

B4X:
                Dim lbl As Label
                lbl.Initialize("")
                Parent.AddView(lbl, bt(i, j).Left+2dip, bt(i, j).Top+10dip, FDefaultWidth-2dip, 30dip)
                
                Dim L As AutoTextSizeLabel
                L.Initialize2(lbl, "")
L.Text = "example"

I have created that initialize2 because setText needs some stuff initialized.

However the CSV.Initialize causes this crash:

1660314766342.png


What can I do to create manually ?
 

EduardoElias

Well-Known Member
Licensed User
Longtime User
There is no such thing as Initialize2 for a class. You must first call Initialize.
I have posted my changed code and explicity said that I changed it.

Using the regular source code from the forum I could not successfuly create manually the AutoTextSizeLabel control and use it somewhat like a regular label.

It depends on the canvas to be pre created, and that is called by some trick from the forms design, if i understand right.

Then I created the initialize2 so it create the bitmap and canvas, however I get a crash.

How can I use AutoTextSizeLabel manually?
 
Upvote 0

EduardoElias

Well-Known Member
Licensed User
Longtime User
I dont see anything on this post about adding AUtoTextSizeLabel or related to the problem in this post.

I know how to manually do that, however I gave the details and the example that there is a problem that I do not understand why is not working.

With designer it does work, manually it requires the bitmap and canvas to be created and when I do that it crashes.

Please what is the right way to add manually this view?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Custom views are built to be added using the designer.

I dont see anything on this post about adding AUtoTextSizeLabel or related to the problem in this post.
That tutorial explains how to add as many custom views as needed.

With designer it does work, manually it requires the bitmap and canvas to be created and when I do that it crashes.
AutoTextSizeLabel doesn't support it. You will need to modify the code to do it. I cannot help you with it. Sorry.
 
Upvote 0

EduardoElias

Well-Known Member
Licensed User
Longtime User
AutoTextSizeLabel doesn't support it. You will need to modify the code to do it. I cannot help you with it. Sorry.

Could you please give an advice at this situation only:

B4X:
    Dim bmp As Bitmap

    bmp.InitializeMutable(1,1) 'ignore

    cvs.Initialize2(bmp)

Why when executed the cvs.initialize I get a "null receiver" error ?

This is the only reason for my modified view does not work.
 
Upvote 0
Top