Possibly really dumb question about edittext in scrollview

mistermentality

Active Member
Licensed User
Longtime User
I have a line of code

B4X:
scvText.Panel.AddView(textbox,1%x, 0, scvText.Width-1%x, 100%y)

which adds an edit text box called "textbox" to a scrollview (scvText) but whatever text is in the edit field is always displayed way down the scrollview near the bottom, not at the top as it should be.

If I change the code to

B4X:
scvText.Panel.AddView(textbox,1%x, -39%y, scvText.Width-1%x, 100%y)

then the edit text contents are displayed at the top of the scrollview. My question is what am I doing wrong in the first code that would cause this as there are no other views added to the scrollview.

The full code (if it helps) is

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
End Sub

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

   Dim decoded_message As String ' holds message
   Dim textbox As EditText
   Dim scvText As ScrollView
   Dim copy_button As Button
   Dim decode_button As Button
   Dim paste_button As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("main")
   decoded_message = "Top" & CRLF & CRLF & "Hello" & CRLF & CRLF & "Testing"
   
   textbox.Initialize("")
   scvText.Panel.AddView(textbox,1%x, 0, scvText.Width-1%x, 100%y)
   
   textbox.TextSize = 18
   textbox.text = decoded_message

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub paste_button_Click
   textbox.text = decoded_message
End Sub
Sub decode_button_Click
   
End Sub
Sub copy_button_Click
   
End Sub

I know there is probably a really stupidly simple reason but for some reason I just can't see it and I have searched the forums before posting but didn't find it so I have to ask as I won't understand why it's not doing what I expect (putting text at top of scrollview) if I don't.

Thanks,

Dave
 

barx

Well-Known Member
Licensed User
Longtime User
You are setting the height of the edittext to 100%y, try something like 40dip.

if the edittext must be 100%y then change it's Gravity to TOP.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You can have a look at this example LongTextSimple.
It's with a Label and not with an EditText view but the principle is the same.
You should adapt the EditText.Height and the ScrollView.Panel.Height to the text height.
You must also set Gravity to Top as already suggested by barx.
Do you really need an EditText view or wouldn't a Label do what you need.

Best regards.
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
Thanks for the help, changing the gravity sorted the problem but I do still wonder why it did it.

Changing the height value from 100%y to something smaller naturally brings the text closer to the top, but it meant there was also less of the text displayed when I tried with longer strings as it then cut off text that didn't fit.

With a label instead of an edit text the same commands work exactly as expected but I need it to be an edit text as it handles user input which can be lots of pasted or typed text so changing the gravity did the trick.

Thank you both for the help, it helped a lot :)

Dave
 
Upvote 0
Top