ScrollView - change the panel height?!

Quillok

Member
Licensed User
Longtime User
Hi,
I've got a little problem using scrollview. I'm adding labels to scrollview.panel and after this i want to change the height of the panel according to the number and height of the added labels. But it's not working. When I delete everything from the panel I can change the height easily but is it not possible when it has some views on it?

B4X:
Sub FillScrollViewPlus
Dim label1,label2,label3 As Label
Dim seperator As ImageView 
Dim StartHeight As Int
Dim NextEntryPosition As Int
   NextEntryPosition = 15
   
      If ScrollViewPlus.Panel.NumberOfViews - 1 > 0 Then
      For x = ScrollViewPlus.Panel.NumberOfViews - 1 To 0 Step -1
      ScrollViewPlus.Panel.RemoveViewAt(x)
      Next
      End If
      
      ScrollViewPlus.Panel.Height = 500
      
      Dim i As Int 
      i = 0
      Do While ValueArrayPlus(i,0) <> "" 
      i = i + 1
      Loop
      
      For x = 0 To i - 1
         
         seperator.Initialize("Seperator")
         seperator.Bitmap = LoadBitmap(File.DirAssets,"trenner.png")
         seperator.Gravity = Gravity.FILL
         
         StartHeight = NextEntryPosition
         
         label1.Initialize("LabelPlusClick")
         label2.Initialize("LabelPlusClick")
         label3.Initialize("LabelPlusClick")
      
         label1.TextColor = Colors.black
         label2.TextColor = Colors.black
         label3.TextColor = Colors.RGB(0,150,0)
                  
         label1.Text = ValueArrayPlus(x,0)
         label2.Text = ValueArrayPlus(x,1)
         label3.Text = "+" & ValueArrayPlus(x,2)
         
         label1.TextSize = 10
         label2.TextSize = 13
         label3.TextSize = 20
         
         label1.Gravity = Gravity.RIGHT 
         label2.Gravity = Gravity.RIGHT 
         label3.Gravity = Gravity.RIGHT 
         
         label1.Typeface = Typeface.DEFAULT_BOLD 
         
         label1.Tag = x
         label2.Tag = x
         label3.Tag = x
         
         Dim Reflect As Reflector 
                  
         ScrollViewPlus.Panel.AddView(label1,5,StartHeight,ScrollViewPlus.width-15,-2)
         DoEvents
         Reflect.Target = label1
                           
         ScrollViewPlus.Panel.AddView(label2,5,label1.top+Reflect.RunMethod("getHeight"),ScrollViewPlus.width-15,-2)
         DoEvents
         Reflect.Target = label2
                           
         ScrollViewPlus.Panel.AddView(label3,5,label2.top+Reflect.RunMethod("getHeight"),ScrollViewPlus.width-15,-2)
         DoEvents
         Reflect.Target = label3
               
         ScrollViewPlus.Panel.AddView(seperator,0,label3.Top+Reflect.RunMethod("getHeight"),ScrollViewPlus.Width,-2)
         DoEvents
         Reflect.Target = seperator
         
         NextEntryPosition = seperator.top+Reflect.RunMethod("getHeight")
         
      Next

      ScrollViewPlus.Panel.Height = 2000
   
End Sub

Thats what I'm trying, but the last command (ScrollViewPlus.Panel.Height = 2000) seems to be ignored, because there is no change in my program. When I change the panel.height at the beginning of the code to 2000 i can scroll more downwards as if I try to change the height at the end of the code.

What am I doing wrong? Does the panel.height not really affect how much I can scroll down? I tried different things and now I have to ask for help.

I bet it's a logical mistake by me... like that with the label.tag. When I click on the added labels in the Scrollview every label has the same number in it's tag, the last number that was in x. But I thought when I initialize the labels in for-to-next this shouldn't happen?! Can anybody help me here to?

thanks in advance :)
 

Quillok

Member
Licensed User
Longtime User
Hi klaus,

thanks for the tip with dim, works now :)

But my problem with scrollview still exists. I added DoEvents after the command as you said, but nothing is changing. I also tried to change the scrollview.panel.height with an extra button but it doesn't work also. I can't scroll deeper than before. But when I turn of the screen and turn it on, then the panel.height is changed and I can scroll more downwards. I think that's what DoEvents should do... but as i said, nothing happens :(
 
Upvote 0

Quillok

Member
Licensed User
Longtime User
Hi klaus,

attached the program/problem ^^
I hope you can solve it.
I recognize that when I run the "Sub FillScrollViewPlus" twice the panel.height is changed. I think because I delete every view on it and then the height is changed to the given value. Because when I run it once and let a messagebox show me the panel.height it tells me the value is 2000, but the height isn't changed.

Thanks for help :)
 

Attachments

  • ScrollViewTest.zip
    8.6 KB · Views: 252
Upvote 0

klaus

Expert
Licensed User
Longtime User
Attached you find two working versions.
ScrollViewTest1.zip is your original program with some small modifications at the end of the routine.
ScrollViewTest2.zip is a version where I defined the label and seperator heights as variables.

I had encountered your problem some time ago and needed to add a ScrollView.Width change after the ScrollView.Panel.Height change as a workaround to make the change.
I suspect that the problem comes from the Height values of -2 and in that case even with a DoEvents the height is not changed, but adding a width change changes also the height.

Below the workaround from ScrollViewTest1.zip:
The ScrollViewPlusWidth variable is defined at the beginning of the routine.
B4X:
ScrollViewPlus.Panel.Height = NextEntryPosition

If ScrollViewPlusWidth = ScrollViewPlus.Width Then
   ScrollViewPlus.Width = ScrollViewPlus.Width - 1
Else
  ScrollViewPlus.Width = ScrollViewPlus.Width
End If

DoEvents
Best regards.
 
Upvote 0

Quillok

Member
Licensed User
Longtime User
Hi klaus,

thanks for your help, although you've forgotten to attach the files, the code you posted helped me out. Now everything works fine.
But I think there is a little mistake in your posted code isn't it? I think this line:
B4X:
ScrollViewPlus.Width = ScrollViewPlus.Width
should be
B4X:
ScrollViewPlus.Width = ScrollViewPlusWidth

or else the scrollview.width would become smaller and smaller... or is it again a mistake in my thoughts? No idea, i'm tired...
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Yes you are right, it should be like below.
B4X:
ScrollViewPlus.Panel.Height = NextEntryPosition

If ScrollViewPlusWidth = ScrollViewPlus.Width Then
   ScrollViewPlus.Width = ScrollViewPlus.Width - 1
Else
  ScrollViewPlus.Width = ScrollViewPlusWidth
End If

DoEvents
Best regards.
 
Upvote 0

pantaraf

Member
Licensed User
Longtime User
Hi,
I'm having the very same problem. I've tried to use the width trick but it doesn't work for me. I can't find a way to make the panel refresh its height on screen even if its value is correct. :(

On Activity_Create:
B4X:
Activity.LoadLayout("Scrollviewlayout")
altpannello=90%y
ScrollView1.Height=altpannello
ScrollView1.Panel.LoadLayout("BigPanel")
pnlAnagrafica.Height=ScrollView1.Height
pnlanagrafica height by design is set to 1000, on screen it is clearly much less than 1000 (some views are cut out at its bottom) even if its Height property says 1458 (?!).
I'm rather confused. o_O:D

Thanks!
Raffaele
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Where and how is pnlAnagrafica defined ?
If it's the panel which is loaded onto the internal ScrollView.Panel and if it's height is OK your code should be:
B4X:
Activity.LoadLayout("Scrollviewlayout")
altpannello=90%y
ScrollView1.Height=altpannello
ScrollView1.Panel.LoadLayout("BigPanel")
ScrollView1.Panel.Height = pnlAnagrafica.Height
 
Upvote 0

pantaraf

Member
Licensed User
Longtime User
SOLVED.
pnlAnagrafica is designed into the BigPanel layout and is set to 1000 by design. The strange thing is that after loading BigPanel layout I found Scrollview1.Panel1.Height being set to 500, same value for pnlAnagrafica.Height, that's why some views were cut off (they runs up to 680).
Obviously before the ScrollView1.Panel.Height = pnlAnagrafica.Height statement! ;)
I don't know why but loading BigPanel makes Scrollview1.Panel1.Height=500, pnlanagrafica was set to the same height after layout loading.
I've solved setting Scrollview1.Panel1.Height=1000 before loadlayout.

B4X:
ScrollView1.Panel.Height=1000
ScrollView1.Panel.LoadLayout("FioritureBigPanel")
ScrollView1.Panel.Height = pnlAnagrafica.Height
Now everything works.

Ciao.
Raffaele
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If you add a ScrollView in the Deigner its internal Panel height is set to 500 by default.
For pnlAnagrafica, you should set its height to the desired value to show all views and then after loading the layout file in the code just set ScrollView1.Panel.Height = pnlAnagrafica.Height.
In your last post ScrollView1.Panel.Height=1000 is not useful.
 
Upvote 0

pantaraf

Member
Licensed User
Longtime User
Klaus, I've tried as you suggested but the problem remained. Right now I've commented out the ScrollView1.Panel.Height=1000 statement. The panel just cuts at 500 so does pnlAnagrafica. With ScrollView1.Panel.Height=1000 it works flawlessly. Now I'm really confused! :D

B4X:
ScrollView1.Height=altpannello
'ScrollView1.Panel.Height=1000
ScrollView1.Panel.LoadLayout("BigPanel")
ScrollView1.Panel.Height = pnlAnagrafica.Height
NOT WORKING

B4X:
ScrollView1.Height=altpannello
ScrollView1.Panel.Height=1000
ScrollView1.Panel.LoadLayout("BigPanel")
ScrollView1.Panel.Height = pnlAnagrafica.Height
WORKING

o_O
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
This still sounds strange to me.
But as I don't know how you have defined the FioritureBigPanel layout its difficult to know why you need ScrollView1.Panel.Height=1000.
If you are interested in, you could post your project and i'll have a look at it.
 
Upvote 0

pantaraf

Member
Licensed User
Longtime User
I'm using a scrollview to manage views that, in case of low resolutions devices, reside out of the physical screen. So I need a very tall panel (pnlAnagrafica) that is loaded into scrollview1.panel using LoadLayout method. I didn't know that Scrollview.panel was set to 500 by default and that makes sense to the behaviour I was facing. Scrollview1 is defined into the layout which is loaded with Activity.LoadLayout. I can't upload my project, beacuse is going to be a commercial app. :)

Thank you a lot!
Raffaele
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Just last night I had the very same problem with a scrollview not scrolling even though the panel height had been set. I determined that the problem was caused by using a DoEvents after each view was added to the scrollview panel. Removing the DoEvents command fixed the fault and showed no noticeable difference in loading the views.

Regards,
RandomCoder.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I was unable to make a sample program earlier which demonstrates the problem caused by using DoEvents whilst populating a ScrollView, but I've had a few minutes to spare this evening and knocked up this sample program which highlights the fault. There's probably a documented reason for this and it's no doubt something that I have done wrong but thought that it might help someone else out if experiencing the same problem. Code shown below and sample project attached.
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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 sv As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    sv.Initialize(Activity.Height)
    Activity.AddView(sv, 0, 0, 100%x, 100%y)
    Dim svpnl As Panel
    Dim x , listBottom As Int
    svpnl=sv.Panel
    listBottom=0
    ' Add labels to scrollview
    For x=0 To 50
        Dim pnl As Panel
        Dim lbl As Label

        pnl.Initialize("item")
        pnl.Tag=x
        svpnl.AddView(pnl, 0, listBottom, svpnl.Width, 80dip)
        ' Show label in scrollview
        lbl.Initialize("item")
        lbl.Tag=x
        pnl.AddView(lbl, 1dip, 1dip, Activity.Width-2dip, 80dip)
        lbl.TextSize=60
        lbl.Gravity=Gravity.CENTER_VERTICAL
        lbl.Text="Item No. " & x

        listBottom=pnl.Top+85dip
        DoEvents ' Commenting out will allow scrollview to work!
    Next
    sv.Panel.Height=listBottom
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub item_Click()
    Dim lblItem As Label
    lblItem=Sender
    Msgbox(lblItem.Tag,"You clicked item No.")
End Sub

The device I'm using is a Galaxy S3 running on Android version 4.1.2

Kind regards,
RandomCoder
 

Attachments

  • ScrollView Fault.zip
    6.2 KB · Views: 191
Upvote 0
Top