Android Question Scrollview..how do you set the height dynamically?

Indy

Active Member
Licensed User
Longtime User
Hi Folks,

I need to set the height of a scrollview so the layout that I have loaded into is scrolls. This is what I have done to load the layout;

B4X:
Scrollview1.Panel.LoadLayout("formlayout")

This works absolutely fine and the form is now within the scrollview. However, I need it to scroll so the remain parts can be accessed. There are 3 buttons that I cannot see. As a test I did the following;

B4X:
Scrollview1.Panel.Height=1000dip

But that did nothing. Is there something I'm missing?

Thanks
 

Indy

Active Member
Licensed User
Longtime User
You need to give more information on what you have in the layout file.

I found what the issue was. I had declared and initialized the same ScrollView twice. Once in the Designer and again in code, which caused a conflict, that meant the command to resize was getting negated (or at least that's what appeared to be happening). Anyway, once I fixed this the resizing worked. However, having got passed this, I now have another question. Since I am loading a layout file into the Scrollview, is there anyway of determining using code what the height of the layout is so I can pass that to the Scrollview.Panel.Height method? At the moment I am getting the Bottom (Top+Height) value of the last Button view on the layout and passing that back. Quite happy to use this method as it works but, I always prefer to use system functions as they are far more reliable and take other factors into account, which I may not be aware of.

Thanks
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
There is no 'system function' to get directly the needed height.
One solution is to put all your views onto a Panel and use use the height of this Panel for Scrollview.Panel.Height.
If the views in your layout are not on a Panel the method you used is the right and only one.
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
One solution is to put all your views onto a Panel and use use the height of this Panel for Scrollview.Panel.Height.

Excellent idea - Thank you! Would I be correct in thinking a Panel height can be extended beyond the physical device screen? A bit like having video buffer which sits outside of the video RAM range but, when scrolled becomes visible?
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
Hi Klaus

I took your advice and added all the controls into a panel using the Designer and then used the following to load it into a Scrollview.

B4X:
ScrollView0.Panel.AddView(Panel0,0,0,100%x,100%y)

However, this results in an error which is basic saying the view (panel) must be initialised. But surely if the panel I'm referrencing was created in the designer, there's no need to initilise it, right?

Any thoughts?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If you have defined the panel in the designer and set all the views in it as childs you must use:
Scrollview1.Panel.LoadLayout("formlayout")
And set:
Scrollview1.Panel.Height = Panel10.Height
You may have a look at this example:
ScrollView example with a Panel higher than the screen
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
I'm beginning to loose my mind over this. Here's my code that I'm using;

B4X:
p.Initialize("Page0")
p.Color = Colors.DarkGray
PC.AddPage(p, "Incident")
       
ScrollView0.Initialize(200)
ScrollView0.Panel.LoadLayout("formlayout")
p.AddView(ScrollView0, 0, 0, 100%x, 100%y)

So instead of seeing the entire screen filled, I get only a fraction. See screenshot attached. I've also attached a screenshot of the Designer setup.

app.png DesignerCapture.JPG

Surely it can't be as difficult as I'm making it. All I want to do is, in the parent panel (p) I want insert a scrollview that has all the form views that should be scrollable because the device screen will be variable when used the customers.
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
Hi Erel,

I have uploaded a test project which exhibits the same behaviour. I would be grateful if you can take a look. I'm obviously doing something wrong.

Thanks
 

Attachments

  • scrollviewproblem.zip
    12 KB · Views: 178
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should first add the ScrollView to the activity and only then load the layout:
B4X:
ScrollView0.Initialize(100%y)   
   Activity.AddView(ScrollView0,0,0,100%x,100%y)
   ScrollView0.Panel.LoadLayout("formlayout")

Otherwise the ScrollView size will not be known.

BTW, you don't need any designer script for this layout. You can use anchors instead.
 
Upvote 0

Indy

Active Member
Licensed User
Longtime User
Hi Erel,

I see what I was doing wrong and it does make sense. However, having done what you suggested still didn't quite work but, no worries I abandoned the Scrollview and just stuck with the Panel. I think I need to spend a bit more time understanding Scrollviews and how to use them with other views.

Thanks for you help anyway.
 
Upvote 0
Top