Android Question How can I add AddView to a ScrollView?

MikeSimpson

Member
Licensed User
Longtime User
I want to add pictures to a scrollview based on this example from Klaus http://www.b4x.com/android/forum/th...-a-panel-higher-than-the-screen.9539/#content

But the picture stays on top of the scrollview and don't move with it. How can I add a picture via code to a specific Layout, so that the picture is moving with it? Attached is the small code I made to load the picture. I only added the two variables and the Sub "Add_Smiley" as a sample.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim scvTest As ScrollView
    Dim pnlTest As Panel
   
    Dim SmileyImgView(1, 1) As ImageView
    Dim Smiley As Bitmap
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    scvTest.Panel.LoadLayout("ScrollViewLayout")
    scvTest.Panel.Height = pnlTest.Height
   
    Add_Smiley
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub edtItem_FocusChanged (HasFocus As Boolean)
    Dim Send As EditText
   
    If HasFocus Then
        Send = Sender
       
        scvTest.ScrollPosition = Send.Top - 10dip
    End If
End Sub
'**************************************************************************************
Sub Add_Smiley
    Dim width, offsetX, offsetY As Int
   
    width = GetDeviceLayoutValues.width /2
   
    Smiley = LoadBitmap(File.DirAssets,"smiley.gif")
       
           
    offsetX = 0'(100%x - width * 3 - 10dip * 2) / 2
    offsetY = 0'(100%y - width * 3 - 10dip * 2) / 2
   
       
    For x = 0 To 0
        For y = 0 To 0
            Dim s As ImageView
            s.Initialize("ImageView") 'All SuggarImgView share the same event sub
            's.Color = Colors.Transparent
            s.SetBackgroundImage(Smiley)
                       
           
                   
            Activity.AddView(s,offsetX + x * (width), offsetY + y * (width), width, width)
            SmileyImgView(x, y) = s 'store a reference to this view
                       
        Next
    Next
End Sub
'****************************************************************************************
 

Attachments

  • ScrollViewBigPanelSmiley.zip
    10.6 KB · Views: 153

klaus

Expert
Licensed User
Longtime User
You must add the Smileys onto the ScrollView.Panel and not onto the Activity !
You must also set the ScrollView.Panel.Height property to the correct value.
Attached the modified program.
 

Attachments

  • ScrollViewBigPanelSmiley1.zip
    11.4 KB · Views: 215
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Thank you Klaus, it's working now in my programm:)
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Now I have another problem. Strangely on my two phones with 480 and 240 width the Smiley looks the same, but on my tablet with a width of 480 pix (same as one of my phones) it looks different. Can someone explain this behavior and can it easily fixed in the above sample? I was prepaired to create different pictures for different screen sizes, but I don't know how to deal with it when it is different on devices with the same resolution, but different outcome.
 

Attachments

  • droid@screen-1.png
    droid@screen-1.png
    42.6 KB · Views: 144
  • droid@screen-2.png
    droid@screen-2.png
    75.1 KB · Views: 149
  • droid@screen-3.png
    droid@screen-3.png
    18.3 KB · Views: 156
Upvote 0

klaus

Expert
Licensed User
Longtime User
Add s.Gravity = Gravity.FILL after s.SetBackgroundImage(Smiley)
B4X:
Dim s As ImageView
s.Initialize("ImageView") 'All SuggarImgView share the same event sub
s.SetBackgroundImage(Smiley)
s.Gravity = Gravity.FILL
 
Upvote 0
Top