Button text not aligned properly

sconlon

Active Member
Licensed User
Longtime User
For several of the buttons in my app I need to calculate and vary the textsize based on the actual button text. In the designer script I have set the button size and default textsize and set the text alignment to CENTER_HORIZONTAL and the button Drawable property to ColorDrawable. Later in the app I recalc the textsize using MeasureMultilineTextHeight. When I run this on a 320 x 480 screen (which is the layout variant I use) in one case the textsize is 18 and the button height is 24 but the text is not centered within the button so that the bottom edge of it is missing.

I recall that in a previous post someone informed me that when the Drawable property was set to ColorDrawable the inner margin would be the same as for a label. However, when I change the button to a label with the same properties the text displays perfectly. What am I missing this time?

Ta
 

sconlon

Active Member
Licensed User
Longtime User
I originally set the horizontal alignment in the designer and didn't change it in code. Now I tried your suggestion but it doesn't seem to make any difference.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I see it too.
I made some tests and come to the conclusion that even when changing the background to ColorDrawble or BitmapDrawble the background remains a 9 patch image.
The outer dimensions are the same as the Label but not the inner dimensions for the text.

Best regards.
 
Upvote 0

sconlon

Active Member
Licensed User
Longtime User
I'm glad I wasn't imagining it! I don't suppose there is any way of finding out the inner dimensions so that I can adjust the textsize to fit or is it a fixed % of the outer height/width?
 
Upvote 0

sconlon

Active Member
Licensed User
Longtime User
Thanks. I'll give it a go. I presume if I create one 9-patch with max inner text height the ratio of inner height to button height should be constant for all such buttons, or is that maybe too much to hope for!
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
No, for me the margins remain constant.
You could try to find out their heights and then substract the value from the available height.
Have you thought about using AutoScale because the text size is scaled with the same scaling as the dimansions.
Or you could use the AutoScale Module where using a sclae rate of 1 is the same as using %x and %y but with text scaling.

Best regards.
 
Upvote 0

sconlon

Active Member
Licensed User
Longtime User
I did some Google searching about text padding within buttons and there seems to be quite a lot of discussion about it, especially on stackoverflow.com where people have suggested various solutions, such as this one but I don't know if this can somehow be used within B4A.

I'll try your AutoScale Module for now and see if that provides the solution.

Thanks again.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You can get and change the padding of a view with the routines below.
B4X:
'Gets the Left padding of the given view
Sub GetPaddingLeft(v As View) As Int
    Dim refl As Reflector
    
    refl.Target = v
    Return refl.RunMethod("getPaddingLeft")
End Sub

'Gets the Top padding of the given view
Sub GetPaddingTop(v As View) As Int
    Dim refl As Reflector
    
    refl.Target = v
    Return refl.RunMethod("getPaddingTop")
End Sub

'Gets the Right padding of the given view
Sub GetPaddingRight(v As View) As Int
    Dim refl As Reflector
    
    refl.Target = v
    Return refl.RunMethod("getPaddingRight")
End Sub

'Gets the Bottom padding of the given view
Sub GetPaddingBottom(v As View) As Int
    Dim refl As Reflector
    
    refl.Target = v
    Return refl.RunMethod("getPaddingBottom")
End Sub

'Sets the padding of a view
Sub SetPadding(v As View, Left As Int, Top As Int, Right As Int, Bottom As Int)
    Dim refl As Reflector
    
    refl.Target = v
    Dim args(4) As Object
    Dim types(4) As String
    args(0) = Left
    args(1) = Top
    args(2) = Right
    args(3) = Bottom
    types(0) = "java.lang.int"
    types(1) = "java.lang.int"
    types(2) = "java.lang.int"
    types(3) = "java.lang.int"
    refl.RunMethod4("setPadding", args, types)
End Sub
Attached a small test project.

Best regards.
 
Upvote 0

sconlon

Active Member
Licensed User
Longtime User
Thanks so much for that. I had noticed that in both buttons and editTexts the text didn't seem to be vertically centered even though I had set this alignment in the designer. By using your GetPadding subs I found that in all cases the bottom margin was greater than the top one (eg top = 13, bottom = 23 for a button height of 99 on my Samsung S3) whereas the left and right was always the same.

So now I can use the SetPadding sub to adjust these and hopefully end up with text that can be read in smallish buttons even on the lower resolution screens.

Thanks again for all your help.
 
Upvote 0
Top