Android Question Scrolling a whole screen

App Dude

Active Member
Licensed User
Longtime User
I'm so confused about this... I have more widgets on my page than can fit, so I want to scroll the whole screen. I'm using B4XViews to be cross-platform.
AIUI, on my page/screen, I would have a scrollview (which can fill the screen) and in that scrollview I would load a panel. That panel can be larger than the actual device screen and hold all the widgets. The panel then scrolls inside of the scrollview.

Here's my experiment, which is not working:
- I created a new layout in the designer, with a couple buttons on it, one near the top, and one far down. Saved it as "MainScreenPanel"
- I have a MainScreen layout that's the first one that comes up when my app loads. In that, I added a Scroll View and called it ScrollView1
- Now, in my B4XMainPage file under B4XPage_Appear I added:

B4X:
    Private ScrollView1 As B4XView
    Private Panel1 As B4XView

    Panel1 = xui.CreatePanel("MainScreenPanel")                        ' Will initialize the panel and load the layout with both buttons
    Panel1.Height = 1000dip                                            ' Random height for now
    ScrollView1.AddView(Panel1, 10dip, 10dip, 100dip, 300dip)        ' Add the panel into the scrollview, random dimensions for now

The app crashes on startup, and the dump shows...
java.lang.IllegalStateException: ScrollView can host only one direct child

I also tried ScrollView1.RemoveAllViews before the AddView, and no crash, but the scrollview does not appear (the rest of the widgets already on the screen does).
What am I doing wrong here?
Thanks.
 
Last edited:

App Dude

Active Member
Licensed User
Longtime User
Responding to my own question, this works...
B4X:
    ScrollView1.GetView(0).LoadLayout("MainScreenPanel")

So apparently there's already a panel in place there.
Not sure where the doc for this is, but maybe I should've known this...??
 
Upvote 0

App Dude

Active Member
Licensed User
Longtime User
Just an additional comment. If possible, it is recommended to use customlistview for things like this

Can you explain why? Isn't the purpose of scrollview to scroll anything? Or is there something about it I don't know?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

klaus

Expert
Licensed User
Longtime User
Instead creating a new Panel with this code.
B4X:
    Panel1 = xui.CreatePanel("MainScreenPanel")                        ' Will initialize the panel and load the layout with both buttons
    Panel1.Height = 1000dip                                            ' Random height for now
    ScrollView1.AddView(Panel1, 10dip, 10dip, 100dip, 300dip)        ' Add the panel into the scrollview, random dimensions for now
You should load the layout onto the internal Panel of the ScrollView.
B4X:
    scvTest.Panel.LoadLayout("YourLayout")
    scvTest.Panel.Height = YourHeight
YourcLayout can be a Panel with views on it, or only views.
YourHeight is the Height you need, this is normally the bottom edge of the view in the most bottom position.
You could have a look at this thread: https://www.b4x.com/android/forum/threads/scrollview-examples-summary.8423/#content
It is an old one but still valid.
xCustomListView has the advantage to be cross-platform and has quite some more functionalities.
 
Last edited:
Upvote 0
Top