B4A Library [custom view] AutoTextSizeLabel

Not open for further replies.
When you set the text of this custom label the text size is automatically modified to the largest possible size so that all the text is visible.


To use this view in your project, you should take the AutoTextSizeLabel class from the attached example and add it to your project. You should then add a Custom View with the designer and set its type to this class.

The following libraries are required:
- JavaObject
- StringUtils


  • AutoTextSizeLabel.zip
    8.5 KB · Views: 1,223
Last edited:


Well-Known Member
Licensed User
Thx Erel...needed this really just-in-time :sign0087:


Active Member
Licensed User

as several of my programs live and die by this, I had to come up with a faster method to calculate sizes.

However, i wrote my views well before 2.0, and did not take advantage of classes nor Designer compatibility like this one.

Attached, is your code, modified with another Function called setText2(very original?)

I include a couple of buttons:

One is for users to test out how a set of words appear in randomly placed CustomViews (to see that the entire word(s) fit nicely) with different languages (this is working quite nicely with Thai!, thanks for that Erel!)

Second is to run a stress test to see the difference in time using the two different methods on their own device. (must be connected to see result in the log)

My own testing shows this binary algorithm to take 25 to 30% of the time of the straight linear approach, especially as the reason I use it is for different devices to look good with unknown resolution/screen size combined with different values that go in each view, so I don't know what the optimum "Start Size" is.

I will be using this new class going forward when I need to develop with this method. Thanks much!



  • BinaryAutoTextSizeLabel.zip
    13.5 KB · Views: 640


Staff member
Licensed User
This modified code adds a transparent panel over the view and handle the events:
Sub Class_Globals
   Private cvs As Canvas
   Private mLbl As Label
   Private su As StringUtils
   Private mTarget As Object, 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
   Base.AddView(lbl, 0, 0, Base.Width, Base.Height)
   Dim p As Panel
   p.Color = Colors.Transparent
   Base.AddView(p, 0, 0, Base.Width, Base.Height)
   mLbl = lbl
   Dim r As Reflector
   r.Target = mLbl
   r.RunMethod4("setPadding", Array As Object(0,0,0,0), Array As String("java.lang.int", "java.lang.int", "java.lang.int", "java.lang.int"))
   r.RunMethod4("setIncludeFontPadding", Array As Object(False), Array As String("java.lang.boolean"))
End Sub

Private Sub p_LongClick
   CallSub(mTarget, mEventName & "_LongClick")
End Sub

Private Sub p_Click
   CallSub(mTarget, mEventName & "_Click")
End Sub


New Member
Licensed User
Hi Erel,

Very useful class, but I'm trying to get a transparent AutoTextSizeLabel.

I've set mLbl.Color = Colors.Transparent in DesignerCreateView and I've also experimented with creating a setter

Public Sub setColor(value As Int)
mLbl.Color = value
End Sub

But the label still has a white background.

I'm fairly new to B4A, but what I've tried works as expected with a normal label.

Any ideas?



New Member
Licensed User
Thanks Erel.

I did have the custom view color set to transparent, but now I've played with it a bit more, I've found I had to set the alpha to 0 as well.

btw: I was using the modified version of the code which handles events.

Cheers, David.


Active Member
Licensed User
I 'm writing a code where some buttons are genarated by code (a particular button click).
Is there any way to remove/delete them before create new ones???


Active Member
Licensed User
Perhaps my problem is how to identify which buttons has been created by code and then to remove thm by code before procceeding in recreating some new ones by code too.

By the way, as a suggestion I think it would be a good idea to write a little tutorial regarding the syntax of command's libraries with some representing examples to cover the often-usen cases.


Active Member
Licensed User
ok erel...but how can do that. doing a conversation? please guide me as i am a newbbie here.


Licensed User
To use this view in your project, you should take the AutoTextSizeLabel class from the attached example and add it to your project. You should then add a Custom View with the designer and set its type to this class.

How can I move this object around the screen programatically?

I can't find the usual .top, .left and the other properties that a typical label has.
Not open for further replies.