Scrollview with 2 buttons on each row

JonPM

Well-Known Member
Licensed User
Longtime User
Hello. I am trying to make a scrollview that has 2 buttons on each row. The problem is I am using an array for the text of the buttons, and when I use a For loop I can't get the text to match up correctly. For instance, I have array(0) = "a","b","c","d",....

I would like the buttons to read:

a b
c d
e f

Using i+1 for the second button doesn't work, the result I get is like this:
a b
b c
c d

Can someone offer some advice?
 

Merlot2309

Active Member
Licensed User
Longtime User
Hello,

For everyone who is using this masterpiece of Klaus:

- I changed lbl to lb in the Sub Button_Click
- and uncommented line 125

Both because I use a database to save the display language settings and the lb.Tags are equal to the databases column names.

Regards,
Helen.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Hi Helen,
I have updated the code in post#19 with your modifications.
Just for your information, in the Button_Click event the name of the label has no importance, it's a nuw instance refering to the right label in the ScrollView and you use its Tag and Text properties which are OK.

Best regards.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
Been working like a dog on this same project, now hit another brick wall I can't get over. Hopefully klaus or someone else can offer some insight.

So now I have gotten this main screen created with many buttons, all populated from 2 different lists (DefaultList & UserList). DefaultList is what is included with the app, UserList is what the user has added himself. Now I am trying to give the user a way to hide a button if they choose. This code hides the button:

B4X:
Sub Button_LongClick
    Dim Send As Button
   Dim MsgHide As Int
    Send = Sender
    MsgHide = Msgbox2("Hide Drug?","","Yes","","No",Null)
      If MsgHide = DialogResponse.NEGATIVE Then
         Return True
      Else
         HideList.Add(Send.Text)
         File.WriteList(File.DirInternal,"HideList.txt",HideList)
         Send.RemoveView
      End If
   
End Sub

However what's left in its place is a blank spot. I would like to refresh the main page and repopulate it with the remaining 'non-hidden' buttons. I just can't seem to figure out how. Any help?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Couldn't you post your project as a zip file? It would be much easier to help you.
There are quite some open questions and the solotion(s) will depend on the answers.
- How do you add the butons by row or by column ?
- How do you manage hidden and visible buttons ?
- Do you want to set a hidden button visible again ?

Best regards.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
It's kind of a large project that isn't commented well. I'll post the relevant parts.

Buttons are added using the code you provided in this post:
B4X:
'start of creation of main screen buttons
    Dim row, col, n As Int
    Dim btnHeight, btnWidth As Float
    Dim Space As Float
    
    ColNumber = 2
    RowNumber = Ceil(ButtonNumber / ColNumber)
     
    Space = 5dip
    btnWidth = (100%x - (ColNumber + 1) * Space) / ColNumber
    btnHeight = 40dip
    scvMain.Initialize(0)
    Activity.AddView(scvMain, 0, 0, 100%x, 100%y)
    For row = 0 To RowNumber - 1
       For col = 0 To ColNumber - 1
         Dim btn As Button
       btn.Initialize("Button")
         n = row * ColNumber + col
         If n <= MainList.Size - 1 Then
            btn.Tag = MainList.Get(n)
            btn.Text = MainList.Get(n)
            scvMain.Panel.AddView(btn, Space + col * (btnWidth + Space), Space + row * (btnHeight + Space), btnWidth, btnHeight)
         End If
      Next
   Next
   scvMain.Panel.Height = Space + row * (btnHeight + Space)
   'end of creation of main screen buttons

And, in short, MainList = (DefaultList + UserList) - HideList


Buttons are hidden like this:
B4X:
Sub Button_LongClick
    Dim Send As Button
   Dim MsgHide As Int
    Send = Sender
    MsgHide = Msgbox2("Hide Drug?","","Yes","","No",Null)
      If MsgHide = DialogResponse.NEGATIVE Then
         Return True
      Else
         HideList.Add(Send.Text)
         File.WriteList(File.DirInternal,"HideList.txt",HideList)
         Send.RemoveView
      End If   
End Sub

Lastly, yes, I would need to give the user a way to show the buttons they've previously hidden. My main problem right now lies in the last code above. Again, once the user selects to hide the button, the button does get removed but it leaves a blank spot in its place. What I would need is for the other buttons to move around to fill in the space. I'm thinking I would need to add the button to the HideList, then remove ALL buttons from the screen, then repopulate the screen with the new MainList (subtract the hidden buttons). Is that about right? I just can't seem to figure out how to remove all the button views and repopulate them.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You can remove all buttons with following code:
B4X:
Sub ClearButtons
    Dim i As Int
    
    For i = scvButtons.Panel.NumberOfViews - 1 To 0 Step -1
        scvButtons.Panel.RemoveViewAt(i)
    Next
End Sub
To repopulate you should split your populating code in two parts, initialization and populating. The populating should be in a separate routine so you can reuse it without initialization.

Best regards.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
Thanks! Didn't realize you can do Step-1, that'll be handy in other projects :)

So for the initialization part, do you mean separate these snippets:

B4X:
scvMain.Initialize(0)
    Activity.AddView(scvMain, 0, 0, 100%x, 100%y)
    Dim btn As Button
    btn.Initialize("Button")
 
Upvote 0
Top