Android Question HorizontalScrollView.Panel.Width problem

danijel

Active Member
Licensed User
I have a problem with horizontal HorizontalScrollView.Panel.Width atribute.
This is a simplified example of what I want to achieve:
First I want to show 50 labels in HorizontalScrollView.
Then generate a random number and show to user only that many labels to choose from.
(Choosing just mean to center that number/label)

Problem 1:
When random number is generated, HorizontalScrollView is not reduced immediately to that number of Labels.
HorizontalScrollView is reduced only when User click on certain Label.

Problem 2:
HorizontalScrollView is resized only once. Code is not working after that at all. I want to repeat that process many times.

Problem 3:

If User first click on certain Label and then on 'get random number' HorizontalScrollView is never reduced. Not even once.

Fact:
If BTN_Click is simulated from Activity_Create then HorizontalScrollView is reduced immediately as it should after regular User Click.

My code is below and I hope someone can clarify what is going on here :/

B4X:
#Region  Project Attributes
    #ApplicationLabel: HSV Example
    #VersionCode: 1
    #VersionName: 1
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: true
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: False   
#End Region

Sub Process_Globals
End Sub

Sub Globals
    
    Dim HSV As HorizontalScrollView
    Dim BTN As Button
    Dim LBL(50) As Label
    Dim Ht As Int
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
        
    Ht=100%x/9    'Label Width and Height
    HSV.Initialize(50*Ht, "HSV")
    Activity.AddView(HSV, 0, 20%x, 100%x, Ht)
    HSV.Color=Colors.RGB(0, 151, 0)

    For i=0 To 49
        LBL(i).Initialize("LBL")
        HSV.Panel.AddView(LBL(i), i*Ht, 0, Ht, Ht)
        LBL(i).Text=i+1
        LBL(i).TextSize=20
        LBL(i).Gravity=Gravity.CENTER
        LBL(i).TextColor=Colors.White
    Next
    
    BTN.Initialize("BTN")
    Activity.AddView(BTN, 10%x, 50%x, 80%x, 30%x)
    BTN.Text="Get Random Number"
    
    'BTN_Click
    
End Sub

Sub LBL_Click
    
    Dim L As Label
    L=Sender
    
    For i=0 To 49
        LBL(i).Color=Colors.ARGB(0,0,0,0)
    Next
    
    L.Color=Colors.RGB(0, 191, 0)
    HSV.ScrollPosition=L.Left+L.Width/2-50%x
    
End Sub

Sub BTN_Click
    
    Dim Rnd_Num As Int
    Rnd_Num=Rnd(10,49)
    BTN.Text="Get Random Number" & CRLF & Rnd_Num
    HSV.ScrollPosition=0
    HSV.Panel.Width=Rnd_Num*Ht
    
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub
 

danijel

Active Member
Licensed User
This code works:
B4X:
Sub BTN_Click
   Dim Rnd_Num As Int
   Rnd_Num=Rnd(10,49)
   BTN.Text="Get Random Number" & CRLF & Rnd_Num
   HSV.ScrollToNow(0)
   HSV.Panel.Width=Rnd_Num*Ht
   Dim jo As JavaObject = HSV.Panel
   jo.RunMethod("requestLayout", Null)
End Sub
Erel, I love You :)

I tried something with Invalidate(), Sleep(0) and forbidden DoEvents with no success.
RequestLayout is something in that direction (kinda).
Can you point me on some lesson to better understand what went wrong in my code?

Thx again <3
 
Top