Android Question ScrollView won't ScrollToPosition

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I have a ScrollView that is displaying the available Camera Preview Image Sizes as RadioButtons

The ScrollView displays the contents correctly and the proper RadioButton is checked, as desired.

But, I would like to scroll the ScrollView to make the checked RadioButton visible.

I've tried many different iterations of the code below. This is my most recent attempt.

My scrollview has a label as the first entry, then adds radiobuttons for each camera preview size supported by the device. It enables the checkmark for the radiobutton that corresponds to the currently selected preview size and saves the Top value for that radiobutton in my variable "pos".

I then attempt to scroll the scrollview to "pos", but it does not scroll.

I have tried with and without DoEvents, ScrollToNow and SetScrollPosition. Nothing seems to work. Obviously I'm doing something wrong.

Added Later:
I found a post from Erel from 2015 that mentions a change to the Scroll function of the ScrollView. I'm using B4A 4.30, which may be before the change. Might that account for the error I'm seeing?

B4X:
  lb.Initialize("")
  svPrevSz.Panel.AddView(lb, 0, 0, svPrevSz.Width, lbPrevSz.Height)
  lb.TextColor = lbPrevSz.TextColor
  lb.TextSize = rbPrevSz.TextSize
  lb.Gravity = Gravity.CENTER
  lb.Text = "Preview Size"

  Dim j as Int = lb.Height
  Dim pos As Int = j

  For i=0 To ls.Size-1 ' ls is a list of available preview sizes
    Dim rb As RadioButton

    rb.Initialize("")
    svPrevSz.Panel.AddView(rb, 0, j, svPrevSz.Width, rbPrevSz.Height)

    rb.TextSize = rbPrevSz.TextSize
    rb.TextColor = rbPrevSz.TextColor
    rb.Text = ls.Get(i)

    If (rb.Text = s) Then ' s holds the currently selected preview size
      rb.Checked = True
      pos = j
    Else
      rb.Checked = False
    End If

    j = j + rbPrevSz.Height
  Next

  svPrevSz.Panel.Height = j

  svPrevSz.ScrollToNow(pos)
  DoEvents
  svPrevSz.ScrollToNow(pos)
  DoEvents

At the bottom of the code, "pos" does hold a reasonable looking value that would correspond to the Top position within the scrollview panel for the selected radio button.

Can anyone see what I'm doing wrong?

Thanks,
Barry.
 
Last edited:

KMatle

Expert
Licensed User
Longtime User
I know that thing. Happens when you change the sv's panel a lot (adding views or so) and directly need to scroll down. Android does not update it properly. DoEvents is ignored at this point (I assume) because you sub is still "running" code.

For me this works:

B4X:
   '... 
   CallSubDelayed2(Me,"Scroll2Pos",SV.Panel.Height)
   
End Sub

Sub Scroll2Pos (pos As Int)
    SV.ScrollPosition=pos
End Sub
 
Upvote 0
Top