Android Code Snippet Setting "perfect" textsize plus padding in a loop for ALL views

I was very tired of changing textsizes and padding for all views by code for each view. So I took Klaus' snippet for adjusting the "perfect" textsize to fit in a label and put a little automation to it.

It's a loop getting all views on a view (here it is a scrollview), check what type of view it is and then sets the "perfect" textsize plus padding.

I took 0,8 for 80% of "full fitting" (looks better).

This works fine for me (labels and edittext). For sure someone can do it better (code). Please feel free to extend/comment/discuss...




B4X:
Dim v As View
    Dim JO As JavaObject
  
    For i =0 To sv.Panel.NumberOfViews-1
        v=sv.Panel.getview(i)
        JO=v  
        Log(v)
      
        Select True
      
            Case v Is Button
                Log(">> Button")
          
            Case v Is Spinner
                Log(">> Spinner")
          
            Case v Is EditText
                Log(">> Edittext")
                Dim et As EditText
                et=v
                SetEditTextSize(et,et.Text)
                JO.RunMethod("setPadding",Array As Object(5,0,0,0))
          
            Case v Is Label
                Log(">> Label")
                Dim la As Label
                la=v
                SetLabelTextSize(la,la.Text)
                JO.RunMethod("setPadding",Array As Object(5,0,0,0))
  
        End Select  
          
    Next

Sub SetLabelTextSize(ex As Label, txt As String)
    Dim dt As Float
    Dim limit = 0.5 As Float
    Dim h As Int
  
    ex.Text = txt
    ex.TextSize = 72
    dt = ex.TextSize
    h = stu.MeasureMultilineTextHeight(ex, txt)  
    Do While dt > limit OR h > ex.Height
        dt = dt / 2
        h = stu.MeasureMultilineTextHeight(ex, txt)  
        If h > ex.Height Then
            ex.TextSize = ex.TextSize - dt
        Else
            ex.TextSize = ex.TextSize + dt
        End If
    Loop
    ex.TextSize=ex.textsize * 0.8
End Sub

Sub SetEditTextSize(ex As EditText, txt As String)
    Dim dt As Float
    Dim limit = 0.5 As Float
    Dim h As Int
  
    ex.Text = txt
    ex.TextSize = 72
    dt = ex.TextSize
    h = stu.MeasureMultilineTextHeight(ex, txt)  
    Do While dt > limit OR h > ex.Height
        dt = dt / 2
        h = stu.MeasureMultilineTextHeight(ex, txt)  
        If h > ex.Height Then
            ex.TextSize = ex.TextSize - dt
        Else
            ex.TextSize = ex.TextSize + dt
        End If
    Loop
    ex.TextSize=ex.textsize *0.8
End Sub
 

KMatle

Expert
Licensed User
Longtime User
Tested it in my actual app. Works very good. Actually it is from @klaus . I only put a loop at it for the lazy people like me.

Hint: When you change a bunch of views it's a good idea to memorize the textzise of the longest text to have all views have the same textsize. But this is a matter of taste.
 

Dave61

Member
Licensed User
Longtime User
Not sure if I am doing something wrong here but it seems the order of the Case components is important?

If I used the above code but put the 'Case v IsLabel' test as the first one in the Select/End Select then it decides that even a Button or EditText are labels!

Actually I was using the loop to either .SendToBack or .BringToFront based on View type and I found that either of these methods also changes the View order so the loop gets out of sequence so can't use the loop in conjunction with these methods.
 

KMatle

Expert
Licensed User
Longtime User
@Dave61 Sorry, didn't see you post. It's not case sensitive. The example is just for a demonstration.

In my apps I don't check if a view is a label, etc. I directly call "SetlabelTextSize" or "SetEdittextSize".

@arminkh : Good work, but for that simple function there is no need for a lib.
 
Top