Android Question How to ensure, that text fits on all buttons on all devices?

Midimaster

Active Member
Licensed User
some days after the product launch I received some screenshoots from google with my app on different devices.

One of them looks like this...

NokiaButtonProblem.png

Nokia 1 Android 8.1


How can I ensure, that the "S" fits on each device?

Does anybody know, how these screenshots are made? I have a lot of them with empty "instrument labels" (see picture above all entries in the middle). Are they Auto-generated? This state is only possible in the moment of loading the next song. One second later there will be entries. But now I'm not sure if this screenshot shows coincidentally this moment or whether it shows a real bug.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Midimaster

Active Member
Licensed User
Now I visited the Developer Console and there I found a "Pre-Launch report", where Google suggests me not to use element heights smaller than 48dip. In the screen above I really used 45dip. I will change the minimun height of elements to 48dip and see, what happens. Maybe this is also enough for this problem...

But... can I calculate the hight of string using a certain font and compare it with the size of the element? Something like "TextWidth" or "TextHeight"? It would help to know a way to find the "biggest avaiable textsize" inside a element.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
But... can I calculate the hight of string using a certain font and compare it with the size of the element?
Try StringUtils.MeasureMultilineTextHeight. I don't know if this is affected by accessibility options.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
But... can I calculate the height of string using a certain font and compare it with the size of the element?
Yes.
As Brian Dean already suggested with StringUtils.MeasureMultilineTextHeight.
But you need to also take into account the padding.

The code below calculates the max text size for a given TextView (Label), includes Labels, EditText and Button views.
Depends on the StringUtils and JavaObject libraries.
You can calculate the TextSize once and set the value to all other Buttons.

B4X:
Private Sub GetMaxTextSize(lbl As Label) As Float
    Private jo As JavaObject
    Private su As StringUtils
    Private AvailableHeight, TextHeight, PaddingTop, PaddingBottom As Int
    
    jo = lbl
    PaddingTop = jo.RunMethod("getPaddingTop", Null)
    PaddingBottom = jo.RunMethod("getPaddingBottom", Null)
    TextHeight = su.MeasureMultilineTextHeight(lbl, "M")
    AvailableHeight = lbl.Height - PaddingTop - PaddingBottom
    
    Return lbl.TextSize / TextHeight * AvailableHeight
End Sub

Attached my small test program.
It works also if the user sets bigger text sizes.
 

Attachments

  • TestButton.zip
    9.4 KB · Views: 194
Upvote 0
Top