ScrollView question

bontchev

New Member
Hello folks,

I am very new to Android, so forgive me if I sound like a complete newbie - it's because I am one.:sign0104: In addition, I don't know Java, so this IDE was the natural choice. I do know several dialects of Basic, though.

I am trying to design an app which presents the user with a column of vertically arranged buttons. I did that with the Designer and it looked OK there, but when I put the app on a real phone, the phone's screen turned out to be smaller and some of the buttons were not visible. So, I guess I need to implement a scrollable view. Unfortunately, all attempts to do so have failed. :(

This is what I have done so far:

With the Designer, I have created a Panel. I have also created the Buttons, with the Panel being the parent of each one of them. I guess that should put them on the Panel. I have also created a ScrollView, the parent of which is the main Activity.

It is my understanding that it is not possible to specify in the Designer that the parent of the Panel is the ScrollView, so I have specified that the parent of the Panel is the main Activity.

Is this design correct so far?

And how do I support it programmatically? I tried something like this:

B4X:
Sub Globals
   Dim MainPanel As Panel
   Dim MainScrollView As ScrollView
End Sub
Sub Activity_Create (FirstTime As Boolean)
   Activity.LoadLayout ("ASS")
   MainPanel.Initialize ("MainPanel")
   MainScrollView.Initialize (420)
   Activity.AddView (MainScrollView, 0, 0, 100%x, 100%y)
   MainPanel.RemoveView
   MainScrollView.Panel.AddView (MainPanel, 0, 0, MainScrollView.Width, MainScrollView.Panel.Height)
End Sub

but it doesn't work. :( What am I doing wrong? I've tried reading the various ScrollView examples, but most of them are way too complicated for my current level of knowledge and I couldn't isolate the parts that are strictly related to my current problem.
 

bontchev

New Member
What is the height of MainScrollView.Panel.Height?
According to the Designer, the MainPanel's dimensions are 320x480 in portrait layout (320x480, scale=1, 160 dpi) and 480x320 in landscape layout (480x320, scale=1, 160 dpi).

What do you mean by 'it doesnt work'? Do you just see a blank screen?
I see only the empty ScrollView (I determined that this is what I'm seeing by giving it a distinct color) with the Activity's title on the top. No buttons and, presumably, no panel.

At startup, something that looks like a tall scrollbar appears for a moment (I presume that it is tall because the ScrollView has the same dimensions as the emulator's screen) and immediately disappears.

I tried reducing the size of the ScrollView, and then the elements of the Panel (some of the buttons) are visible below its end. However, neither of the visible buttons is clickable (I do have events that activate when a button is clicked and that display a simple Msgbox), the panel is clearly not on the ScrollView, and the ScrollView doesn't scroll anything.
 
Upvote 0

klaus

Expert
Licensed User
Try this code:
B4X:
Sub Globals
 Dim MainScrollView As ScrollView
End Sub

Sub Activity_Create (FirstTime AsBoolean)
  Activity.LoadLayout ("ASS")
  MainScrollView.Panel.LoadLayout("MainPanel")
End Sub
As your ScrollView is already in the ASS layout no need to initialize it.
For the ScrollView.Panel, just load the layout file.
Be shure that you have set the ScrollView.Panel.Height correctly in the Designer, otherwise you must set in the code.

Best regards.
 
Upvote 0

bontchev

New Member
Ah, your solution is even worse than mine.:D Mine only doesn't work - yours causes compile-time and run-time errors.

First of all, apparently

B4X:
Dim MainPanel As Panel
must be present in Sub Globals - otherwise the compiler protests that it is not defined.

Second, the program causes a run-time error, which I'm too lazy to type (and one can't cut-and-paste from the emulator) but which basically means that the file mainpanel.bal was not found. Why do you use

B4X:
MainScrollView.Panel.LoadLayout ("MainPanel")
? I have only one Layout (well, two variants of it - for portrait and for landscape orientation of the device) and it is called "ASS". It contains everything - the main activity, the panel, the scroll view and the buttons. There is no separate layout for the panel.

However, now that I know what the main problem is (I must not initialize something that is present in the layout), there were few enough variants of the rest of the code, so by commenting and swapping lines around I think I got it to work. The final variant looks like this:

B4X:
Sub Globals
   Dim MainPanel As Panel
   Dim MainScrollView As ScrollView
End Sub
Sub Activity_Create (FirstTime As Boolean)
   Activity.LoadLayout ("ASS")
   MainPanel.RemoveView
   MainScrollView.Panel.AddView (MainPanel, 0, 0, MainScrollView.Width, MainScrollView.Panel.Height)
End Sub

It seems to work - meaning that the buttons are visible and clickable and the view can be scrolled, if I define it to be small enough. At least in the emulator. Haven't tried it on a real device yet.

BTW, what do you mean by

Be shure that you have set the ScrollView.Panel.Height correctly in the Designer, otherwise you must set in the code.
? What are the "correct" values for them? I set them large enough so that no scroll bar appears on the emulator. I presume, one will appear on smaller screens?

And what do you mean by "set in the code"? Just assign them values? Or is there a way to say "set them equal to the physical size of the device the app is currently running on"?
 
Upvote 0

klaus

Expert
Licensed User
Sorry, I didn't read enough carefully your post.
I thought that you had a layout for the ScrollView.Panel (because that's how I do it).
You could define in the Designer a layout with one Panel that could be higher than the screen and load this layout file to ScrollView.Panel.
Then you must set the ScrollView.Panel.Height to the height of the Panel in the layout either in the designer or in the code.

Best regards.
 
Upvote 0
Top