Android Code Snippet Automatically set the text size

This is based on a nice new feature in Android Support library. It automatically adjusts the view's text size to make it fit.

You can call it with any of the following views: Label, Button, CheckBox, RadioButton, ToggleButton
Note that the size will change whenever you set the text.

B4X:
#AdditionalJar: com.android.support:support-compat

Sub SetAutoSizeBasedOnText(v As View)
   Dim jo As JavaObject
   jo.InitializeStatic("android.support.v4.widget.TextViewCompat")
   jo.RunMethod("setAutoSizeTextTypeWithDefaults", Array(v, 1))
End Sub

See post #3 for more information on the supported versions.
 
Last edited:

Erel

Administrator
Staff member
Licensed User
You are right Luca. It is not clear in Google documentation but for the above code to work on Android 4+ devices you need to use AppCompat with the various AC views.

You can use ACFlatButton instead of a label.
To prevent buttons from being upper-cased automatically:
B4X:
Sub RemoveAllCaps (Parent As Panel)
   For Each v As View In Parent.GetAllViewsRecursive
       If GetType(v) = "android.support.v7.widget.AppCompatButton" Then
           Dim jo As JavaObject = v
           jo.RunMethod("setAllCaps", Array(False))
       End If
   Next
End Sub

B4X:
RemoveAllCaps(Activity)
 

Cableguy

Expert
Licensed User
This is a rarity; not the fact that you say I'm right, but that I'm really right :p
Even a broken *mechanical" watch is right 2 times a day!
 

JohnC

Expert
Licensed User
You are right Luca. It is not clear in Google documentation but for the above code to work on Android 4+ devices you need to use AppCompat with the various AC views.

Can you please provide an example of what you mean because I am getting the error:
B4X:
java.lang.RuntimeException: Method: setAutoSizeTextTypeWithDefaults not found in: android.support.v4.widget.TextViewCompat
 

BarryW

Active Member
Licensed User
This is based on a nice new feature in Android Support library. It automatically adjusts the view's text size to make it fit.

You can call it with any of the following views: Label, Button, CheckBox, RadioButton, ToggleButton
Note that the size will change whenever you set the text.

B4X:
#AdditionalJar: com.android.support:support-compat

Sub SetAutoSizeBasedOnText(v As View)
   Dim jo As JavaObject
   jo.InitializeStatic("android.support.v4.widget.TextViewCompat")
   jo.RunMethod("setAutoSizeTextTypeWithDefaults", Array(v, 1))
End Sub

See post #3 for more information on the supported versions.

How to set limit on minimum text size and maximum text size?
 

FrankDev

Active Member
Licensed User
is there a way to limit the maximum font size ?

setAutoSizeTextTypeWithDefaults

'withdefaults' suggests that to me at least.

Greetings
Frank
 

JohnC

Expert
Licensed User
Maybe something like this...

B4X:
FontSize = MaxFontSize
lbl.TextSize = FontSize  'lets start off with maximum size you want to limit it to

'Then lets get smaller and smaller until it fits, but if it already fits with maxfontsize, then skip the below do loop
Do While su.MeasureMultilineTextHeight(lbl, lbl.Text) > lbl.Height
        FontSize = FontSize - 1
        lbl.TextSize = FontSize
Loop
 
Last edited:

JohnC

Expert
Licensed User
You're welcome :)
 
Top