Android Question [SOLVED] Set Equal Position of Views in Panel

konradwalsh

Active Member
Licensed User
Hi
I have searched and search and cannot find a specific answer to my question;

I have a panel. It may have 1 checkbox or up to 8.
I add the checkbox dynamically.

How can I position them so that it goes
1 2 5 6
3 4 7 8

This is the code I have used to achieve this:
B4X:
If i Mod 2 <> 1 Then
         panLive_ListedUsers.AddView(userCheckBox,posLeft + 5dip, posTop + 10dip , 100dip, 30dip)
         posLeft = posLeft + 100dip
 Else
          panLive_ListedUsers.AddView(userCheckBox,posLeft + 5dip, posTop + 10dip, 100dip, 30dip)
           posTop = posTop + 30dip
                       
 End If
 

Attachments

klaus

Expert
Licensed User
This is one possibility:
To add the texts, use an array of string.
B4X:
Private Space As Int = 5dip
Private Width As Int = Min(100dip, (panLive_ListedUsers.Width - 5 * Space) / 4)
Private Height As Int = 30dip
Private posLeft As Int
Private i, j, k As Int
For i = 0 To 1
    For j = 0 To 1
        k = 2 * i + j
        posLeft = Space
        Private userCheckBox As CheckBox
        userCheckBox.Initialize("userCheckBox")
        userCheckBox.Tag = k
        userCheckBox.Text = k + 1
        panLive_ListedUsers.AddView(userCheckBox, posLeft + j * (Width + Space), Space + i * (Height + Space), Width, Height)
        
        k = k + 4
        posLeft = 3 * Space + 2 * Width
        Private userCheckBox As CheckBox
        userCheckBox.Initialize("userCheckBox")
        userCheckBox.Tag = k
        userCheckBox.Text = k + 1
        panLive_ListedUsers.AddView(userCheckBox, posLeft + j * (Width + Space), Space + i * (Height + Space), Width, Height)
    Next
Next
 
Top