Android Question Adding views to a Scrollview

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All

My first attempt to use Scrollview to hold a Label, a Button and an Imageview.
All four views have been defined in the designer. When I attempt to add anything to the scrollview I get an error log that the view already has a Parent and I should use removeview.
The label, Button and Imageview are Parented by the Main Activity.

I have read up the tutorials and examples but obviously missed something.
Any help appreciated.

B4X:
Sub BtnFiles_click
    Vibrate.Vibrate (75)         ' Vibrate phone for 75 ms  === Phone Library
    Private b As Beeper                ' Audio Library
    b.Initialize(50, 600)         ' 50 milliseconds, 600 hz
    b.Beep

    If File.Exists(File.DirRootExternal&"/ABT/", "") = False Then File.MakeDir(File.DirRootExternal&"/ABT/", "")
'Find file names. Select file to import
    If FileName_flag = 0 Then
        FileListing1.Clear
        FileListing1 = FileLists.ListFiles(File.DirRootExternal & "/ABT/", "*.png", True, True)
        FileSelect                        'Returns Filename1
        Return                                    'Ignore
    End If
'CODE TO COLLECT XXX.png FOR PANEL REQUIRED HERE
    ImgScreen.Width = 100%X
    ImgScreen.Height = 100%y


    ImgScreen.bitmap = LoadBitmap(File.DirRootExternal&"/ABT/", Filename1)                       'Load Bmp to Imageview
    lblTitle.Text = Filename1                                                                    'Load text to label
   
    lblTitle.Height = 10%x
    lblTitle.Width = 100%x
    ListBack.Height = 10%x
   
    ScreenScrollView.Height = 110%y
    ScreenScrollView.Left = 0
    ScreenScrollView.Top = 0
    ScreenScrollView.BringToFront
   
    ScreenScrollView.Panel.AddView(lblTitle,0,0,100%x,lblTitle.Height)                                'Add Label
    ScreenScrollView.Panel.AddView(ListBack,0,0,ListBack.Width, lblTitle.Height)                    'Add Button
    ScreenScrollView.Panel.AddView(ImgScreen,0, lblTitle.Height,ImgScreen.Width, ImgScreen.Height)    'Add Imageview
   

End Sub

Failed.jpg


Regards
Roger
 

mangojack

Expert
Licensed User
Longtime User
Have not used Scrollview .. but i think you should remove Label, Button and Imageview from designer layout .. the Scrollview can remain.
Dynamically add views to your scrollview in code... needs a tidy up but ie :

B4X:
Activity.LoadLayout("main")

   ScreenScrollView.Height = 110%y
  ScreenScrollView.Left = 20dip
  ScreenScrollView.Top = 20dip
  ScreenScrollView.BringToFront
  
   For i = 0 To 3
  
     Dim ImgScreen As ImageView
     ImgScreen.Initialize("ImgScreen")
     ImgScreen.Color = Colors.Blue
  
     Dim lblTitle As Label
     lblTitle.Initialize("")
     lblTitle.Color = Colors.Black
     lblTitle.Text = "Item #  " & (i + 1)
  
     Dim ListBack As Button
     ListBack.Initialize("ListBack")
     ListBack.Color = Colors.Green
     ListBack.Text = "Button # " & (i + 1)
  
       ' ImgScreen.bitmap = LoadBitmap(File.DirRootExternal&"/ABT/", Filename1)  'Load Bmp to Imageview
       ' lblTitle.Text = Filename1  'Load text to label
     ScreenScrollView.Panel.AddView(lblTitle, 0, i * 52dip, 100dip, 50dip)  'Add Label
     ScreenScrollView.Panel.AddView(ListBack, 110dip, i * 52dip, 100dip, 50dip)  'Add Button
     ScreenScrollView.Panel.AddView(ImgScreen,220dip, i * 52dip , 100dip, 50dip)  'Add Imageview
  
   Next
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Easy to understand:

When your app starts there is a "view" called Activity. Quite empty. The you can decide if you add views on your own and/or adding it by "Loadlayout". Loadlayout adds the views you've designed, initializes them for you and adds them to the parent called Activity. If you try it again on another the message comes up saying "Sorry, I've already a parent".

Without the designer (no Loadlayout!):

Dim SV as Scrollview
Dim L1, L2, L3 as Label

SV.Initialize(1000dip) ' make it big at the start (to guarantee you see everything and that it scrolls. 100dip would cut it and it will not scroll)
L1.Initialize("")
L1.Initialize("")
L3.Initialize("")

All 4 views are just created and have no parent right now

Activity.Add(SV,0,0,100%x,100%y) ' add the SV fullscreen

Now the SV was added and has a parent

SV.Panel.Add(L1,0,0,100%x,10%y) ' add the label to the panel with a height of 10%y
SV.Panel.Add(L2,0,11%y,100%x,10%y) ' add the label to the panel under L1 with a height of 10%y
SV.Panel.Add(L3,0,22%y,100%x,10%y) ' add the label to the panel under L2 with a height of 10%y

L1's parent is SV.Panel and SV's parent is Activity.

I never use the designer because I want to do everything on my own (full control). I can create own views and don't need other libs. The creator of CustomListView (good job!) did it the same: Putting other views together to a very nice Listview.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Did you have a look at this thread ScrollView examples summary?
And a more specific thread:
https://www.b4x.com/android/forum/t...-panel-higher-than-the-screen.9539/#post52833https://www.b4x.com/android/forum/t...-panel-higher-than-the-screen.9539/#post52833

You can use the Designer to add views to a ScrollView.
But you need to two layout files one for the Activity containing the ScrollView and another one for the ScrollView.Panel
You must load the second layout onto the ScreenScrollViews internal Panel with ScreenScrollView.Panel.LoadLayout(MyLAyout")
This is used in the specific link above.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Did you have a look at this thread ScrollView examples summary?
And a more specific thread:
https://www.b4x.com/android/forum/t...-panel-higher-than-the-screen.9539/#post52833

You can use the Designer to add views to a ScrollView.
But you need to two layout files one for the Activity containing the ScrollView and another one for the ScrollView.Panel
You must load the second layout onto the ScreenScrollViews internal Panel with ScreenScrollView.Panel.LoadLayout(MyLAyout")
This is used in the specific link above.
Thanks Klaus,
I had looked at the example but it didn't click. Brain drain.
Regards Roger
 
Upvote 0
Top