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?
 

vb1992

Well-Known Member
Licensed User
Longtime User
Upvote 0

eps

Expert
Licensed User
Longtime User
Either your array is oddly formed or you're doing something else.. I would suggest using a piece of paper and writing down what is going on for each iteration.

From your first problem it looks like you were looping through each record by 1. Then accesing +0 and +1

For 1 this gives 1 and +1, ie 2

For 2 this gives 2 and +1, ie 3

Etc

There must be something else which you were expecting to be 1 or something is now 2, etc in your code which is throwing it out.. A piece of paper and scribbling can work wonders or step through the code and look at variables and their values whilst in the loop. It can save hours of frustration!
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
As you don't explain more in detail what you did we need to guess it, here is an example of filling a ScrillView with two Buttons on each row.
B4X:
Sub Globals
    Dim scvButtons As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim row, col As Int
    Dim btnHeight, btnWidth As Float
    Dim str() As String
    
    str = Array As String("A", "B", "C", "D", "E", "F", "G", "H")
    
    btnHeight = 120dip
    btnWidth = 120dip
    scvButtons.Initialize(0)
    Activity.AddView(scvButtons, 0, 0, 100%x, 100%y)
    For row = 0 To 3
        For col = 0 To 1
            Dim btn As Button
            btn.Initialize("Button")
            btn.Tag = str(row * 2 + col)
            btn.Text = str(row * 2 + col)
            scvButtons.Panel.AddView(btn, 20dip + col * 50%x, 20dip + row * (btnHeight + 20dip), btnWidth, btnHeight)
        Next
    Next
    scvButtons.Panel.Height = 20dip + row * (btnHeight + 20dip)
End Sub

Sub Button_Click
    Dim Send As Button
    
    Send = Sender
    Activity.Title = "Button " & Send.Tag & " clicked"
End Sub
Best regards.
 

Attachments

  • ScrollViewTwoButtons.zip
    5.6 KB · Views: 329
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
Well, on closer inspection...
I am getting an out of bounds exception error when my array contains an odd number of items. I believe its because its trying to create, for example, row 16, column 1, when there is only 17 items. Any ideas?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are.
There are tow examples:
ScrollViewTwoButtons : with two columns
ScrollViewTwoButtons1: with different number of columns

Code of ScrollViewTwoButtons1:
B4X:
Sub Globals
    Dim scvButtons As ScrollView
    Dim ButtonNumber, RowNumner, ColNumber As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim row, col, n As Int
    Dim btnHeight, btnWidth As Float
    Dim str() As String
    Dim Space As Float
    
    str = Array As String("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N")

    ButtonNumber = 13
    ColNumber = 3
    RowNumber = Ceil(ButtonNumber / ColNumber)
    
    Space = 10dip
    btnWidth = (100%x - (ColNumber + 1) * Space) / ColNumber
    btnHeight = btnWidth
    scvButtons.Initialize(0)
    Activity.AddView(scvButtons, 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 <= ButtonNumber - 1 Then
                btn.Tag = str(n)
                btn.Text = str(n)
                scvButtons.Panel.AddView(btn, Space + col * (btnWidth + Space), Space + row * (btnHeight + Space), btnWidth, btnHeight)
            End If
        Next
    Next
    scvButtons.Panel.Height = Space + row * (btnHeight + Space)
End Sub

Sub Button_Click
    Dim Send As Button
    
    Send = Sender
    Activity.Title = "Button " & Send.Tag & " clicked"
End Sub
Best regards.
 

Attachments

  • ScrollViewTwoButtons.zip
    5.7 KB · Views: 227
  • ScrollViewTwoButtons1.zip
    5.7 KB · Views: 238
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hi Klaus,

As usual: THANK YOU! and perfect timing.
Your post is two days before I knew that I needed this.

Helen.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
Here you are.
There are tow examples:
ScrollViewTwoButtons : with two columns
ScrollViewTwoButtons1: with different number of columns

Code of ScrollViewTwoButtons1:
B4X:
Sub Globals
    Dim scvButtons As ScrollView
    Dim ButtonNumber, RowNumner, ColNumber As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim row, col, n As Int
    Dim btnHeight, btnWidth As Float
    Dim str() As String
    Dim Space As Float
    
    str = Array As String("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N")

    ButtonNumber = 13
    ColNumber = 3
    RowNumber = Ceil(ButtonNumber / ColNumber)
    
    Space = 10dip
    btnWidth = (100%x - (ColNumber + 1) * Space) / ColNumber
    btnHeight = btnWidth
    scvButtons.Initialize(0)
    Activity.AddView(scvButtons, 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 <= ButtonNumber - 1 Then
                btn.Tag = str(n)
                btn.Text = str(n)
                scvButtons.Panel.AddView(btn, Space + col * (btnWidth + Space), Space + row * (btnHeight + Space), btnWidth, btnHeight)
            End If
        Next
    Next
    scvButtons.Panel.Height = Space + row * (btnHeight + Space)
End Sub

Sub Button_Click
    Dim Send As Button
    
    Send = Sender
    Activity.Title = "Button " & Send.Tag & " clicked"
End Sub
Best regards.

Work perfect! I appreciate your help. One note: instead of using ButtonNumber = 13, I used ButtonNumber = str.Length and got the same result. I think this makes it a little less error prone when adding more strings to the array. Thanks again!
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hello,

Used Klaus' code to create 2 columns of buttons and added a label next to each button.
What I want: Button_Click -> add a number in the corresponding label.
I did get it working with Label_Click as Sender but can't figure out how to connect the button with the corresponding label.

Thank you in advance.

Helen.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Could you post your project, it would be much easier to help you.

How did you add the Buttons and the Labels to the ScrollView.Panel?
To modify the Label.Text value you need to get the Label view, and this depends on how you added the Buttons and Labels.
Lets say:
- you add in the first row button, button, label
- second line button, button, label
- and so on.
The view indexes are :
- first row 0, 1, 2
- second row 3, 4, 5

If you added the views to the ScrollView.Panel that way you can get the Label of a given row in the Button_Click event.
You know the row where the Label is, given in the Tag property of the Button that raised the event,
and then you can calculate the label index with i = row * 3 + 2
and get the Label view with
Dim lbl As Label
lbl = ScrollView.Panel.GetView(i)
.
and change the text with lbl.Text = number.

Hope this is clear enough and best regards.
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hello Klaus,

Comme toujours, merci beaucoup.
I will post the code results or my project asap.

Until now I got as far as the first label's text filled with number 1, no matter which button is clicked, ha, ha.

Thank you,
Helen.
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hello,

Can't get it to work :BangHead:

I need the btn.tags (EN, PL, etc.) to manipulate a database.
So what I need is: press a language button -> fill the label next to the button with a number (1 and up)

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

   Dim talen As String
   Dim taal As String
   Dim lsSL As List
   Dim Selection As String
   Dim SelectionIndex As Int   : SelectionIndex = 5
   Dim j As Int      
   Dim BtnNr, RowNr, ColNr As Int

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim scrv As ScrollView
   
   Dim spSL As Spinner
   Dim Selector As Map

   Dim dip As String
   Dim btCA, btCS, btDA, btDE, btET, btEN, btES, btEU, btFR, btFY, btIT, btNL, btPL, btPT, btRU, btSC, btSL, btSV, btUK As Button
   Dim btnTxt() As String
   Dim lbEN, lbDE, lbFR, lbES, lbIT, lbPT, lbNL, lbDA, lbSC, lbSV, lbCA, lbEU, lbFY, lbRU, lbSL, lbUK, lbET, lbPL, lbCS As Label
   Dim lbVolgorde() As String
   Dim CA, CS, DA, DE, ET, EN, ES, EU, FR, FY, IT, NL, PL, PT, RU, SC, SL, SV, UK As String
   Dim grad As GradientDrawable
   Dim cls(2) As Int
   
End Sub

Sub Activity_Create(FirstTime As Boolean)

   Activity.LoadLayout("LOL")

   If FirstTime Then
      lsSL.Initialize
      lsSL.AddAll(Array As String("Català", "Česky", "Dansk", "Deutsch", "Eesti", "English", _
      "Español", "Euskara", "Français", "Frysk", "Italiano", "Nederlands", "Polski", "Português", _
      "Русский", "Scientific", "Slovenščina", "Svenska", "Українська"))
      j = 1
   End If
   
   Selector.Initialize
   Selector.Put("Language", spSL)
   
   Talen
   
End Sub
Sub Talen

Dim row, col, n As Int
Dim btnHeight, btnWidth As Float
Dim lbHeight, lbWidth As Float
Dim Space As Float

   btnTxt = Array As String("Català", "Česky", "Dansk", "Deutsch", "Eesti", "English", _
      "Español", "Euskara", "Français", "Frysk", "Italiano", "Nederlands", "Polski", "Português", _
      "Русский", "Scientific", "Slovenščina", "Svenska", "Українська")
   lbVolgorde = Array As String ("CA", "CS", "DA", "DE", "ET", "EN", "ES", "EU", "FR", "FY", "IT", "NL", "PL", "PT", "RU", "SC", "SL", "SV", "UK")
      scrv.Initialize(0)
      Activity.AddView(scrv, 0, 120dip, 100%x, 100%y)

   btnHeight = 45dip
   btnWidth = 110dip
   lbHeight = 40dip
   lbWidth = 30
   BtnNr = btnTxt.Length
   ColNr = 2
   RowNr = Ceil (BtnNr / ColNr)
   Space = 5dip

For row = 0 To RowNr -1
   For col = 0 To ColNr -1

   Dim btn As Button
   Dim lb As Label
         btn.Initialize("Button")
         lb.Initialize("Label")
         btn.TextSize = 17
         lb.TextColor = Colors.Black
         lb.TextSize = 18
         
      n = row * ColNr + col
      If n <= BtnNr -1 Then
      btn.Tag = lbVolgorde(row * 2 + col)
      lb.Tag = lbVolgorde(row * 2 + col)
            btn.Text = btnTxt(row * 2 + col)
            scrv.Panel.AddView(btn, Space + col * 50%x, 5dip + row * (btnHeight + 5dip), btnWidth, btnHeight)
            scrv.Panel.AddView(lb, 120dip + col * 50%x, 5dip + row * (lbHeight + 10dip), lbWidth, lbHeight) 
            cls(0) = Colors.RGB(152, 251, 152)
            cls(1) = Colors.RGB(152, 251, 152)
            grad.Initialize("LEFT_RIGHT", cls)
            grad.CornerRadius = 3
            lb.Background = grad
            lb.Gravity = Gravity.CENTER
         End If
      Next
   Next

scrv.Panel.Height = 20dip + row * (btnHeight + 20dip)
   
End Sub

Sub Activity_Resume

Dim lb As Label
lb.Initialize("Label")
lb = Sender

   Selection = Selector

   spSL.AddAll(lsSL)
   spSL.SelectedIndex = SelectionIndex
   Selection = spSL.SelectedIndex

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub spSL_ItemClick (Position As Int, Value As Object)

   Selection = Value
   SelectionIndex = Position
   
End Sub
Sub Button_Click

Dim btn As Button
Dim row, col, i As Int

btn = Sender
'For i = 0 To scrv.Panel.NumberOfViews -1

'i = 9   Puts 1 at Eesti

Dim lb As Label
lb = scrv.Panel.GetView(i+1)

   If lb.Text = "" Then
      lb.Text = j      
      
      j = j + 1
   Else

   End If

End Sub

Thanks in advance,
Helen.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Hi Helen,
Couldn't you post your project as a zip file ?
It would be much easier to help you because we could test it in the same conditions as you do,
without us having to make our own project that is not the same as yours.

Just a few questions about your code:
- What do you have in the "LOL" layout ?
-
Dim CA, CS, DA, . . As String What are these for ?
- What are the lsSL list and the spinner for ?
- You should Dim btnTxt() and lbVolgorde() in the Process_Globals routine
- Do you want two button plus one label or two buttons plus two labels, in your code you add two labels.

I don't understand exactly what you want to do with the buttons.
Do you just want to add an increasing number in the labels if it's empty ?

Best regards.
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hi Klaus,

Included is the .zip

With the spinner people can select the search language.

By pressing button(s) people can select the display languages in the order that they want (eg. French - 1, German - 2, English - 3, etc.).

Hope that the project makes it clear.

Thank you,
Helen.
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hi Klaus,

Thank you so much, also for the correction of the "Clear"-button.
To be honoust I got anoid (at myself, ha, ha) because I couldn´t figure it out.
Well, you created another great piece of code, much appreciated.

Have a nice evening!

Helen
 
Upvote 0
Top