Setting ScrollView height

walterf25

Expert
Licensed User
Longtime User
Hello all, i need some advice on how to set the internal height of a scrollview so that it adjusts to the height of the text being displayed, i tried scrollview.panel.height but it doesn't seem to work, can anyone please tell me what i'm doing wrong, i have a label and i'm displaying data being pulled from a database file, the text varies in height, i'm using the .MeasureMultilineTextHeight method to measure the height of the label, but i can not seem to make the height of the scrollview adjust to the height of the text.

Please help.
here's part of the my code

thanks everyone!!!!!


Sub NextButton_Click
Dim qq As Int
Dim lblheight1 As Int
Counter = Counter + 1
If Counter > 99 Then Counter = 99 'The sample database has 4 records
DetailLabel.Text = RecordsList.Get(Counter)
If AnswerList.Get(Counter) = Null Then
Label1.Text = "Empty Answer"
lblheight1 = lblheight.MeasureMultilineTextHeight(Label1, Label1.Text)
Label1.Height = lblheight1
Label1.Text = "Empty Answer"
ScrollView1.Panel.Height = lblheight1 + 800dip
Else
Label1.text = AnswerList.Get(Counter)
lblheight1 = lblheight.MeasureMultilineTextHeight(Label1, Label1.Text)
Label1.Height = lblheight1
Label1.text = AnswerList.Get(Counter)
ScrollView1.Panel.Height = lblheight1 + 800dip
End If
If AnswerList.Get(Counter) = Null AND Counter = 43 Then
Label1.Text = StateModule.CapitalName
lblheight1 = lblheight.MeasureMultilineTextHeight(Label1, Label1.Text)
Label1.Height = lblheight1
Label1.Text = StateModule.CapitalName
ScrollView1.Panel.Height = lblheight1 + 800dip
Else If AnswerList.Get(Counter) = Null AND Counter = 22 Then
Label1.Text = StateModule.RepNames
lblheight1 = lblheight.MeasureMultilineTextHeight(Label1, Label1.Text)
Label1.Height = lblheight1
Label1.Text = StateModule.RepNames
ScrollView1.Panel.Height = lblheight1 + 800dip

End If
End Sub
 

NJDude

Expert
Licensed User
Longtime User
You will need the StringUtils lib to do this:

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

    Dim su As StringUtils
    Dim ScrollView1 As ScrollView
    Dim Label1 As Label
    
End Sub

Sub Activity_Create(FirstTime As Boolean)

    Label1.Initialize("Label1")
    
    ScrollView1.Width = 100%x
    ScrollView1.Height = 100%y
   
    Label1.Text = "some long text here"

    ScrollView1.Panel.AddView(Label1, 0dip, 0dip, ScrollView1.Width, ScrollView1.Panel.Height)
    
    ScrollView1.Panel.Height = su.MeasureMultilineTextHeight(Label1, Label1.Text)
    Label1.Height = su.MeasureMultilineTextHeight(Label1, Label1.Text)     

End Sub

I hope I got it right, I've written this quickly
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
NJDude i'am using the StringUtils library, look at my code again and you'll see, but for some reason the scrollview's height doesn't seem to adjust to the height of the label, basically i have two buttons one labeled "Previous" and the other one "Next" this buttons load the data from a database file, i then use the label to display the data from the database, for example in one record i have the names of all 50 states and i want the user to be able to scroll all the way down to see all 50 state names but i want the height of the scrollview to adjust accordingly. any ideas where i might be doing something wrong!
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Hmmm, try changing the order, set the scrollview panel height first and then the label height, the sample I posted works for me.

Tried that also and it doesn't work!!!! :sign0148:
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
How come you set Label1 height to lblheight1 then change the text after?

Sent from my DROIDX
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Ok, i'm going crazy here, i can not figure out what am doing wrong, i tried all the suggestions and other different things and still i can not get the scrollview height to adjust to the content of the label, i'm using scrollview1.panel.height = label1.height + 20dip, i'm using a toastmessageshow(scrollview1.panel.height, false) and it does indeed shows that it sets the height the way i want to, but physically i don't see any difference when i run the app, however if i set the height at the Sub Activity_Create(FirstTime As Boolean) to whatever height i want, it sets the height to that specific number but i can't change it from a button1_click subroutine, hope i'm making any sense, but if anyone can please help me figure this out, i will really appreciate it guys.

thanks,
Walter
:sign0163:
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…