Android Question Edit Text and ScrollView with String Utility

Scantech

Well-Known Member
Licensed User
Longtime User
I am having an issue with Edit Text height not the same as Scroll View Panel height. I am using the string Utility to measure the height of the Edit Text and setting the height on both Edit Text and Scroll View.Panel. The Scroll View Panel is somehow larger?

Any help will be appreciated.


B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    'Private imgDiag As BetterImageView
    Private etText As EditText
    Private svScroll As ScrollView
   
End Sub

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("1")
       
    svScroll.Initialize(1dip)
    Activity.AddView(svScroll, 0, 0, 100%x, 50%y)
   
    etText.Initialize("")
    svScroll.Panel.AddView(etText, 0, 0, 100%x, 50%y)
    'etText.InputType = etText.INPUT_TYPE_NONE
    etText.SingleLine = False
    etText.Wrap = False
   
    For x = 0 To 50
        etText.Text = etText.Text & "testing " & x & " xxxxxxxxxxxxx...............................................xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" & Chr(10)
    Next

    Dim su As StringUtils
    Dim edheight As Int     = su.MeasureMultilineTextHeight(etText, etText.Text)

    'Log(edheight)
   
    svScroll.Panel.Height = (edheight/3) + 20dip
    etText.Height = svScroll.Panel.Height

   
End Sub
 

Scantech

Well-Known Member
Licensed User
Longtime User
I was testing and by dividing by 3 the edit text fits in the scroll panel, but still leaves spacing.
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
Example attached.

I want to eliminate most of the spacing below the text. Dividing by 3 method applies to Samsung S6, trying other variants still shows excessive spacing.
 

Attachments

  • Scroll EditText Sample.zip
    7.2 KB · Views: 166
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
MeasureMultilineTextHeight calculates the height assuming that the text is wrapped.

In this code you should use this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("1")
     
   svScroll.Initialize(1dip)
   Activity.AddView(svScroll, 0, 0, 100%x, 50%y)
   
   etText.Initialize("")
   svScroll.Panel.AddView(etText, 0, 0, 100%x, 50%y)
   etText.InputType = etText.INPUT_TYPE_NONE
   etText.Gravity = Gravity.TOP
   
   etText.SingleLine = False
   etText.Wrap = False
   Dim lblText, edtText As StringBuilder
   lblText.Initialize
   edtText.Initialize
   For x = 0 To 100
     edtText.Append("testing " & x & " xxxxxxxxxxxxx...............................................xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" & CRLF)
     lblText.Append("t").Append(CRLF)
   Next
   Dim lbl As Label
   lbl.Initialize("")
   Activity.AddView(lbl, 0, 0, 100%x, 100%y)
   etText.Text = edtText.ToString
   lbl.TextSize = etText.TextSize
   Dim su As StringUtils
   Dim edheight As Int    = su.MeasureMultilineTextHeight(lbl, lbl.Text)
   lbl.RemoveView
   etText.Height = edheight
   svScroll.Panel.Height = edheight
End Sub
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("1")
 
   svScroll.Initialize(1dip)
   Activity.AddView(svScroll, 0, 0, 100%x, 50%y)
 
   etText.Initialize("")
   svScroll.Panel.AddView(etText, 0, 0, 100%x, 50%y)
   etText.InputType = etText.INPUT_TYPE_NONE
   etText.Gravity = Gravity.TOP
   etText.SingleLine = False
   etText.Wrap = False
 
   Dim sbText2 As StringBuilder
   sbText2.Initialize
 
   For x = 0 To 100
        Select Case x
            Case 4, 8, 10, 12, 16, 21, 22, 23, 24, 25, 26, 27, 28
                sbText2.Append("Helloxxxxxxxxxxxxx...............................................xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" & Chr(10) & Chr(10))
            Case Else
                sbText2.Append("testing " & x & " xxxxxxxxxxxxx...............................................xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" & CRLF)
        End Select
   Next
  
   Dim lbl As EditText
   lbl.Initialize("")
   Activity.AddView(lbl, 0, 0, 100%x * 3, 50%y)
 
   etText.Text = sbText2.ToString
   lbl.TextSize = etText.TextSize
   lbl.Text = sbText2.ToString
 
   Dim su As StringUtils
   Dim edheight As Int    = su.MeasureMultilineTextHeight(lbl, lbl.Text)
 
   lbl.RemoveView
 
   svScroll.Panel.Height = edheight
   etText.Height = edheight
 
End Sub

This one works really well.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…