Android Code Snippet Automatically set the text size

Erel

Administrator
Staff 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.
 
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)
 

JohnC

Well-Known 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.
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?
 
Top