Android Question Large button text won't center text vertically

Widget

Well-Known Member
Licensed User
Longtime User
I notice if I use a large font size around 30 point for the button text, it won't center the text properly (vertically) inside the button.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:

    Private btnResult As Button
    btnResult.Initialize("") 
    Activity.AddView(btnResult, 20dip, 180dip, 75dip, 50dip) 

    btnResult.Text      = "1.3"
    btnResult.TextSize = 30
    btnResult.Gravity  = Bit.Or(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL)
End Sub

This puts the text "1.3" at the very bottom of the button with space at the top of the button. Why isn't the text centered vertically inside the button? If I change the font size to 20 it is centered just fine. I am using a large font because I want the text to occupy most of the button.

TIA
(B4A v6.0)
 

Attachments

  • LargeButtonText.PNG
    LargeButtonText.PNG
    1.1 KB · Views: 199

JordiCP

Expert
Licensed User
Longtime User
Strange. What happens if you make the button larger (more height)?

Perhaps there is a vertical padding...
 
Upvote 0

An Schi

Well-Known Member
Licensed User
Search the forum for 'button padding'. You will find a piece of code with java object which lets you remove the padding by setting everything to zero. Then your text should be centered.
 
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
The height of the Button is too small!

Yes the button height is too small. I increased the button height to 60dip and it looks much better with a 30 point font.

It appears the button text cannot occupy the entire height of the button. From what I can tell, the button text can only occupy about 50% of the button's height which leaves a 25% gap at the top and a 25% gap at the bottom of the button.
 
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
Search the forum for 'button padding'. You will find a piece of code with java object which lets you remove the padding by setting everything to zero. Then your text should be centered.

I found it, thanks.
BTW, I thought the Button would have a Padding property so it can be easily changed.
At least the SetPadding() sub of Erel's will free up some extra space around the button text.:)

https://www.b4x.com/android/forum/threads/padding-on-views.37252/#post-219562
https://www.b4x.com/android/forum/t...width-to-set-buttons-width.68153/#post-432165
 
Upvote 0
Top