B4J Question Width - textfield - change

jose sanjuan

Member
Licensed User
Longtime User
Hello, how can I change the width of a textfield control?
When I write: textfield1.width=100, it tells me that it is read-only
 

stevel05

Expert
Licensed User
Longtime User
Take a look at the tooltip int the textfields width property:



You can set the PrefWidth instead.

But, the best way would be to declare the Textfield as a B4xView in globals and then use the width property or do:

B4X:
Textfield1.As(B4XView).Width = 150
 
Upvote 0

jose sanjuan

Member
Licensed User
Longtime User
Thanks! but it does not work

B4X:
Sub Class_Globals
    Dim codmtotxt1 As B4XView
End Sub
....
codmtotxt1.As(B4XView).Width = 20

this keeps the same size

This works but I am not able to set different data sizes even if I use the font "CourierNew" (fixed size in pixels):

B4X:
Dim jo As JavaObject = codmtotxt1
jo.runMethod("setMaxWidth",Array(datasize*9.00d))
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you have constrained the size of the textarea in the designer, then it will not change, or it will change until the layout needs to be refreshed.

You shouldn't put the d after the value, using 9.00 is enough to determine that it is a double value and not an int. Datasize should also be declared as a double value.

What are you trying to do? Why do you want to change the size of the textfield?

You could try using B4XCanvas.MeasureText method and set the width to the result, with appropriate padding.
 
Upvote 0

jose sanjuan

Member
Licensed User
Longtime User
Thanks, I try to adjust the size in pixels of the text field to the size of the table field (n characters). For example, 10 characters * 11 pixels (font size) = 110 pixel width that the control must have
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Is there a reason to do that or is it just for looks?

What happend if someone types more text. Is it expected to grow with each new character?
 
Upvote 0

jose sanjuan

Member
Licensed User
Longtime User
It is utility and appearance, if it exceeds a maximum width the user will continue writing and nothing happens. I'm making an application and I generate all its controls dynamically, so I want to show the user how far they can write. I have been programming for 35 years and now I no longer make any forms that are not dynamic and everything is defined in tables. A template form in which everything is created dynamically, parameterized in external tables.

Thanks! y sorry! for my bad English...
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Now I understand. i hope you get it sorted. If not, you could do what appears on a lot of web pages and show a count of characters remaining.
And your English is far better than my Spanish
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User

'Note: if proportional font, the characters need to be counted as entered to prevent more than maximum
'There might be extra space because of spaces and narrow letters

B4X:
'What should be the pixel width of a label/textfield that can have a maximum of n characters of a particular font and size?
Private Sub computeWidth(n As Int, fnt As B4XFont) As Int
    Dim testString As String = "XXXXXXXXXX"
    Dim cv As B4XCanvas        'ignore, since we don't need to initialize it for this purpose
    Dim w As Float = cv.MeasureText(testString, fnt).width
    Return Floor((n + 1) * w / 10)
End Sub

Public Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Dim fnt As B4XFont = xui.CreateDefaultFont(11)
    Dim tf As TextField
    tf.Initialize("input")
    Dim tfx As B4XView = tf
    tfx.Font = fnt
    tfx.Text = "The World Turns"
    Root.AddView(tf, 100, 200, tf.Width, 30)
    'After added to the panel the view is fully formed and can be modified
    tfx.Width = computeWidth(tfx.Text.Length, fnt) 
    tfx.RequestFocus
    tfx.SetSelection(tfx.Text.Length, 0)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…