Android Question Resize Label text with Java Object

Discussion in 'Android Questions' started by Roger Daley, May 20, 2015.

  1. Roger Daley

    Roger Daley Well-Known Member Licensed User

    Hi All,

    On the question of scaling text between devices.

    I have Klaus's code snippet for finding the best text size and works well. As all views use the same size font I use this code to find the text size for label1 and use it to set the text size of the other views.

    I also use code [posted by either Erel or Klaus] to set the padding of views. This code uses Java Objects in a loop, I would like to use something similar to set the text size rather than the long list I am currently using.

    The code below is what I am aiming for but can't find information on the "MethodName" or "Parameters" required.


    Code:
    ' Set padding on all views to zero.  
        For Each v As View In Activity.GetAllViewsRecursive
            
    If v Is Button Then
                 
    Dim jo As JavaObject = v
                 jo.RunMethod(
    "setPadding"Array As Object(0000))
            
    End If
           
    If v Is EditText Then
                 
    Dim jo As JavaObject = v
                 jo.RunMethod(
    "setPadding"Array As Object(0000))
           
    End If
           
    If v Is Label Then
                 
    Dim jo As JavaObject = v
                 jo.RunMethod(
    "setPadding"Array As Object(0000))
           
    End If
        
    Next

    'Klaus's code snippet
    SetTextSize(Label1, "Label1 Text")

    ' Set  text size to Label1.textsize.  
        For Each v As View In Activity.GetAllViewsRecursive
            
    If v Is Button Then
                 
    Dim jo As JavaObject = v
                 jo.RunMethod(
    "MethodName",Parameters)
            
    End If
           
    If v Is EditText Then
                 
    Dim jo As JavaObject = v
                 jo.RunMethod(
    "MethodName",Parameters)
           
    End If
           
    If v Is Label Then
                 
    Dim jo As JavaObject = v
                jo.RunMethod(
    "MethodName",Parameters)
           
    End If
        
    Next

    Any guidance greatfully appreciated.

    Regards Roger
     
  2. thedesolatesoul

    thedesolatesoul Expert Licensed User

  3. klaus

    klaus Expert Licensed User

    Try this code:
    Code:
    If (v Is ButtonOr (v Is EditTextOr (v Is LabelThen
        
    Dim jo As JavaObject = v
        jo.RunMethod(
    "setTextSize"Array As Object(Label1.TextSize))
    End If
    Not sue if the () are necessary in the If statement.

    This should work too:
    Code:
    If v Is Label Then
        
    Dim jo As JavaObject = v
        jo.RunMethod(
    "setTextSize"Array As Object(Label1.TextSize))
    End If
    If I remember well, EditTexts and Buttons are also considered as Labels (TextViews).
     
    thedesolatesoul and Roger Daley like this.
  4. Roger Daley

    Roger Daley Well-Known Member Licensed User

    Thanks thedesolatesoul and Klaus,

    All tried and result below, all Labels, Edittexts and Buttons done in one hit.

    Code:
    SetTextSize(lblOffset, "LANDMARK OFFSET")
       
        
    For Each v As View In Activity.GetAllViewsRecursive
            
    If v Is Label Then
                
    Dim jo As JavaObject = v
                jo.RunMethod(
    "setTextSize"Array As Object(lblOffset.TextSize))
            
    End If
        
    Next
    Regards Roger
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    You don't need JavaObject here:
    Code:
    If v Is Label Then
    Dim lbl As Label = v
    lbl.TextSize = lblOffset.TextSize
    End If
     
    RandomCoder and Roger Daley like this.
  6. Roger Daley

    Roger Daley Well-Known Member Licensed User

    Thanks Erel,

    That makes it even simpler. I like simple.

    Roger
     
  7. Roger Daley

    Roger Daley Well-Known Member Licensed User

    Just to round out the thread, below is the final code. It adjusts text size with out java.
    I found that using stringutils gave me a smaller than required text. I think that stringutils allows for the default padding, I have removed the padding to allow a larger text size. As an alternative to stringutils I have used Canvas.MeasureStringHeight, found I needed to use a 0.7 factor to get the correct sizing. I don't know why but it seems to work on different devices.

    Code:
    Sub SetTextSize(lbl As Label, txt As String)
        
    Dim DeltaTextSize, TextSizeNow, TextSizeScale  As Float
        
    Dim limit = 0.5 As Float
        
    Dim TextHeight As Int
        
    Dim Canvas1 As Canvas
        Canvas1.Initialize(
    Activity)

        TextSizeNow = lbl.TextSize
        lbl.Text = txt
        lbl.TextSize = 
    76
        DeltaTextSize = 
    76    
       
        
    'Calculate correct text size for lbl [Sucessive approximation]
        TextHeight = Canvas1.MeasureStringHeight(txt, Typeface.DEFAULT, lbl.TextSize)
        
    Do While DeltaTextSize > limit OR TextHeight > (lbl.Height * 0.7)    '0.7 arrived at by trial and error
            DeltaTextSize = DeltaTextSize / 2
            TextHeight = Canvas1.MeasureStringHeight(txt, 
    Typeface.DEFAULT, lbl.TextSize)
            
    If TextHeight > (lbl.Height * 0.7Then                '0.7 arrived at by trial and error
              lbl.TextSize = lbl.TextSize - DeltaTextSize
            
    Else
              lbl.TextSize = lbl.TextSize + DeltaTextSize
            
    End If
        
    Loop
        TextSizeScale = lbl.TextSize/TextSizeNow    
    'Ratio between starting and ending text size for lbl
        lbl.TextSize = TextSizeNow        'Restore lbl text size before applying text scale factor to all views

        
    For Each v As View In Activity.GetAllViewsRecursive  'Adjust all textviews by TextSizeScale
            If v Is Label Then
                
    Dim lbl As Label = v
                lbl.TextSize = lbl.TextSize * TextSizeScale
            
    End If
        
    Next

    End Sub




    I hope this makes sense and is usefull. Thanks for all the help and let me know if you find a problem.

    Regards Roger
     
    Last edited: May 21, 2015
  8. klaus

    klaus Expert Licensed User

    Unfortunately your code will not work in all cases because Canvas1.MeasureStringHeight returns different values depending on the text.
    Example:
    Canvas1.MeasureStringHeight("a", Typeface.DEFAULT, 16)
    Canvas1.MeasureStringHeight("A", Typeface.DEFAULT, 16)
    Canvas1.MeasureStringHeight("Aj", Typeface.DEFAULT, 16)
    These three statements return different values !
    The factor of 0.7 is probably due to the padding and perhaps the fact above.
    I am also not convinced that it will work for all cases. The padding in a Label is constant independant of the TextSize.
     
    thedesolatesoul likes this.
  9. Roger Daley

    Roger Daley Well-Known Member Licensed User

    Klaus

    You are probably correct, I had a gut feeling someone would find holes in it. Fortunately it appears to be working in this project.
    I don't think padding is an issue here as the padding should be zero.

    Code:
    ' Set padding on all views to zero.   
        For Each v As View In Activity.GetAllViewsRecursive
           
    If v Is Label Then
                 
    Dim jo As JavaObject = v
                 jo.RunMethod(
    "setPadding"Array As Object(0000))
           
    End If
        
    Next
       
        SetTextSize(lblOffset, 
    "LANDMARK OFFSET")
    Possibly in other cases the developer would need to find a factor other than 0.7 by trial and error for their specific application.
    Preferably stringutils would factor the actual padding of each view.
    In an instance produced using stringutils, I checked the sizes. Label.textsize = 26, the Label.height = 50. This is a considerable percentage of the label real estate not used.

    I guess the real problem is me trying to get bigger text in to a label. :)

    Thanks again for the feedback
    Roger
     
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