B4J Question [Solved] TextSize in button change button's size

Serinter

Member
Licensed User
Longtime User
Hello everyone:

I am trying to assign text to a button, as large as possible, so that the text fills it.

Buttons must be added at runtime, not in designer.

When I try, if the text exceeds a measure, which is not the size of the button, I do not know why, the button is oversized.

Is there any way that the button does not become larger than the measure specified for it, before assigning the text to it?

Otherwise, can we know in advance the maximum size that the button allows for the text without changing its size?

I'd like set the "internal margins" (padding?) of button to the mínimum posible.

I searched the forum for a couple of days, but I can not find a solution.

I found the following code in the forum and changed "SetMinSize" to "SetMaxSize", and obviosusly my desired size for button, but it does not do anything:

B4X:
'define the button and add to the rootpane of the mainform
Dim btn As Button
btn.Initialize("btn")
btn.Text = "Button 1"
'h and w must have initial value > 0
MainForm.RootPane.AddNode(btn, 20,20, 1, 1)
Dim jo As JavaObject = btn
Dim h As Double = 15.0
Dim w As Double = 15.0
jo.RunMethod("setMinSize", Array(h, w))

I have taken the example of DynamicControls to illustrate my problem:

B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    'actually square root of the actual number
    Private NumberOfButtons As Int = 10
    Private buttons(NumberOfButtons, NumberOfButtons) As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.Show
    MainForm.Title = "Dynamic Controls"
    For x = 0 To NumberOfButtons - 1
        For y = 0 To NumberOfButtons - 1
            Dim b As Button
            b.Initialize("btn")
            b.Text = 0
            b.TextSize = 18
            buttons(x, y) = b
            MainForm.RootPane.AddNode(b, 0, 0, 10, 10)
        Next
    Next
End Sub

Private Sub MainForm_Resize (Width As Double, Height As Double)
    Dim BtnWidth As Double = (Width - 10)/ NumberOfButtons
    Dim BtnHeight As Double = (Height - 10)/ NumberOfButtons
    For x = 0 To NumberOfButtons - 1
        For y = 0 To NumberOfButtons - 1
            buttons(x, y).SetLayoutAnimated(0, 5 + x * BtnWidth, 5 + y * BtnHeight, _
                BtnWidth - 1, BtnHeight - 1)
        Next
    Next
End Sub

Private Sub btn_Action
    Dim b As Button = Sender
    b.Text = b.Text + 1
End Sub



Default Size:



Changed Size (b.TextSize = 18): Looks like it's the maximum alowed in this example.
Top and Bottom wasted space!!. I'd like set a Bigger Sized number inside the button



Too Big Sized Text (b.TextSize = 25): Changes button's size. Can it be avoided?



I do not know what else to do ... Can you help me, please?
Thanks in advance.
 

Attachments

  • ChangedSize.png
    14.4 KB · Views: 274
  • DefaultSize.png
    12.6 KB · Views: 266
  • TooBigSize.png
    15.9 KB · Views: 281
Last edited:

Serinter

Member
Licensed User
Longtime User
Hello. Sorry for the delay in answering, but the work has not allowed me to do it before.
Thank you, Daestrum, for your answer, but even if the button does not change its size,
I see that what I want to do is not possible, so I opted for another less elegant but more effective solution: Put an imageview under a transparent button.
Thank you very much for your interest.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…