Android Question AutoTextSizeLabel Event not trigerred

iCAB

Well-Known Member
Licensed User
Longtime User
Hi All

So far I have been doing all layouts by code, so this is my first time using the designer and Custom views.
I am just wondering why i am unable to capture the click event, even though the label is enabled in the designer.

B4X:
Private Sub AutoTextSizeLabel1_CLick
    Log("not trigerred by default ")
End Sub

For the above to work, I have to add the line below in DesignerCreateView
B4X:
lbl.Initialize("AutoTextSizeLabel1")

I am wondering if there is a better way perhaps using "Initialize"

Thanks
iCAB
 

iCAB

Well-Known Member
Licensed User
Longtime User
The label that is passed to DesignerCreateView doesn't listen to any events. You need to initialize or create a new label (it is the same thing).
Let me try to understand this properly.

In the sample code, when I open the designer, I find 3 Labels: AutoTextSizeLabel1, AutoTextSizeLabel2, AutoTextSizeLabel3

in Sub Globals

B4X:
Private AutoTextSizeLabel1 As AutoTextSizeLabel
Private AutoTextSizeLabel3 As AutoTextSizeLabel

in Activity Create

B4X:
Activity.LoadLayout("1")
    Dim cs As CSBuilder
    AutoTextSizeLabel1.Text = cs.Initialize.Color(Colors.Green).Append("Hello ").Pop.BackgroundColor(Colors.Yellow).Color(Colors.Black).Append("World! :-)").PopAll.Color(Colors.Red).Append("Hi").Pop
    
    AutoTextSizeLabel3.Text = cs.Initialize.Color(Colors.Green).Append("ONE").Popall

isn't this creating the labels?

Thanks
iCAB
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Untested code that you should replace in the class:
B4X:
#Event: Click
Sub Class_Globals
   Private cvs As Canvas
   Private mLbl As Label
   Private su As StringUtils
   Private mTarget As Object
   Private mEventName As String
End Sub

Public Sub Initialize (Target As Object, EventName As String)
   mTarget = Target
   mEventName = EventName
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
   lbl.Initialize("lbl")
   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

Sub Lbl_Click
   CallSub(mTarget, mEventName & "_Click")
End Sub
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Thanks Erel!
I think I Got it,

So in other words, in the actual designer, the fact that there is an "EventName" and the fact that the control is enabled doesn't mean anything in terms of event triggering.
Is this correct?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
So in other words, in the actual designer, the fact that there is an "EventName" and the fact that the control is enabled doesn't mean anything in terms of event triggering.
Is this correct?
Yes. Custom views by default do not raise any event. The events must be implemented.
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
Here is an updated version that also gets the font, ellipsize, gravity and text color from the designer:
B4X:
Public Sub Initialize (Target As Object, EventName As String)
    mCallback = Target
    mEventName = EventName
End Sub

Public Sub DesignerCreateView(Base As Panel, lbl As Label, props As Map)
    Dim Font As Typeface = lbl.Typeface
    Dim Ellipsize As String = lbl.Ellipsize
    Dim gvt As Int = lbl.Gravity
    Dim TextColor As Int = lbl.TextColor
  
    Dim bmp As Bitmap
    bmp.InitializeMutable(1,1) 'ignore
    cvs.Initialize2(bmp)
    Dim parent As Panel = Base.Parent
  
    lbl.Initialize("lbl")
    lbl.Typeface = Font
    lbl.Ellipsize = Ellipsize
    lbl.Gravity = gvt
    lbl.TextColor = TextColor
  
    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

Private Sub lbl_Click
    If SubExists(mCallback, mEventName & "_Click") Then
        CallSubDelayed(mCallback, mEventName & "_Click")
    End If
End Sub

edit: made a mistake with the font
 
Last edited:
Upvote 0
Top