Android Question Using MeasureStringWidth to set button's width

DrAlex

Member
Licensed User
Longtime User
Hello,

I am trying to set a button's width to accommodate it's text by using the MeasureStringWidth method. Below is my code. Apparently, I am doing something wrong, because the resulting button's width is only about a half of text's width.

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.


End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim pnl As Panel
    pnl.Initialize("")
    Activity.AddView(pnl,0,0, 100%x, 100%y)
    Dim cvs As Canvas
    cvs.Initialize(pnl)
    Dim btn As Button
    btn.Initialize("")
    btn.Text = "Hello Friends"
    btn.TextSize = 14
    Dim TextWidth As Int = cvs.MeasureStringWidth("Hello Friends", Typeface.DEFAULT, 14)
    pnl.AddView(btn, 10dip, 10dip,  TextWidth, 40dip)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 

Attachments

  • MeasureStringWidthExample.zip
    1.2 KB · Views: 131

klaus

Expert
Licensed User
Longtime User
There are two problems in your code:
- Buttons have a Padding, which means that the text area is smaller than the button width.
- The TextType of buttons is Bold by default.
The code below works:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim pnl As Panel
    pnl.Initialize("")
    Activity.AddView(pnl,0,0, 100%x, 100%y)
    Dim cvs As Canvas
    cvs.Initialize(pnl)
    Dim btn As Button
    btn.Initialize("")
    btn.Text = "Hello Friends"
    btn.TextSize = 14
    Dim TextWidth As Int = cvs.MeasureStringWidth("Hello Friends", btn.Typeface, 14) + getPaddingLeft(btn) + getPaddingRight(btn)
    pnl.AddView(btn, 10dip, 10dip, TextWidth, 40dip)

End Sub

'Gets the Left padding of the given view
Sub getPaddingLeft(v As View) As Int
    Dim jo = v As JavaObject
    Return jo.RunMethod("getPaddingLeft", Null)
End Sub

'Gets the Right padding of the given view
Sub getPaddingRight(v As View) As Int
    Dim jo = v As JavaObject
    Return jo.RunMethod("getPaddingRight", Null)
End Sub

'Sets the padding of a view
'v = view
'Left, Top, Right, Bottom padding values in pixels
Sub setPadding(v As View, Left As Int, Top As Int, Right As Int, Bottom As Int)
    Dim jo = v As JavaObject
    jo.RunMethod("setPadding", Array As Object(Left, Top, Right, Bottom))
End Sub
It measures also the padding and it uses btn.TypeFace instead of Typeface.DEFAULT.

You could also remove the padding of the button, but the you need to change also the background.

Attached my small test program to see what happens.
 

Attachments

  • TestMeasureString.zip
    7.2 KB · Views: 157
Upvote 0

DrAlex

Member
Licensed User
Longtime User
There are two problems in your code:
- Buttons have a Padding, which means that the text area is smaller than the button width.
- The TextType of buttons is Bold by default.
The code below works:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim pnl As Panel
    pnl.Initialize("")
    Activity.AddView(pnl,0,0, 100%x, 100%y)
    Dim cvs As Canvas
    cvs.Initialize(pnl)
    Dim btn As Button
    btn.Initialize("")
    btn.Text = "Hello Friends"
    btn.TextSize = 14
    Dim TextWidth As Int = cvs.MeasureStringWidth("Hello Friends", btn.Typeface, 14) + getPaddingLeft(btn) + getPaddingRight(btn)
    pnl.AddView(btn, 10dip, 10dip, TextWidth, 40dip)

End Sub

'Gets the Left padding of the given view
Sub getPaddingLeft(v As View) As Int
    Dim jo = v As JavaObject
    Return jo.RunMethod("getPaddingLeft", Null)
End Sub

'Gets the Right padding of the given view
Sub getPaddingRight(v As View) As Int
    Dim jo = v As JavaObject
    Return jo.RunMethod("getPaddingRight", Null)
End Sub

'Sets the padding of a view
'v = view
'Left, Top, Right, Bottom padding values in pixels
Sub setPadding(v As View, Left As Int, Top As Int, Right As Int, Bottom As Int)
    Dim jo = v As JavaObject
    jo.RunMethod("setPadding", Array As Object(Left, Top, Right, Bottom))
End Sub
It measures also the padding and it uses btn.TypeFace instead of Typeface.DEFAULT.

You could also remove the padding of the button, but the you need to change also the background.

Attached my small test program to see what happens.

Klaus, Thank you very much for the prompt useful lesson and the examples. You are great!
 
Upvote 0
Top