Android Code Snippet Automatically set the text size

Discussion in 'Code Snippets' started by Erel, May 22, 2018.

  1. Erel

    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.

    Code:
    #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: Nov 14, 2018
    paragkini, Beja, Pencil3 and 9 others like this.
  2. LucaMs

    LucaMs Expert Licensed User

    This means: from Android 8.0 !

    (Stuff for rich :D)
     
    Beja and Erel like this.
  3. Erel

    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:
    Code:
    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
    Code:
    RemoveAllCaps(Activity)
     
    LucaMs likes this.
  4. LucaMs

    LucaMs Expert Licensed User

    This is a rarity; not the fact that you say I'm right, but that I'm really right :p
     
  5. Cableguy

    Cableguy Expert Licensed User

    Even a broken *mechanical" watch is right 2 times a day!
     
    LucaMs likes this.
  6. JohnC

    JohnC Well-Known Member Licensed User

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

    Erel Administrator Staff Member Licensed User

    Use B4A Sdk Manager and make sure that all recommended items are updated.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice