Android Question How to create a table with fixed column and rows

hasanaydin52

Member
Licensed User
Longtime User
i hope this code is usefull

B4X:
Dim scv As ScrollView
Dim hsv As HorizontalScrollView
 
scv.Initialize(0)
Activity.AddView(scv,0,0,100%x,100%y)
 
hsv.Initialize(0,"")
scv.Panel.AddView(hsv,100dip,0,100%x-100dip,100%y)
 
 
For i=0 To 50
        Dim lbl As Label
        lbl.Initialize(0)
        lbl.Text="A"&i
        lbl.Color = Colors.LightGray
        lbl.TextColor=Colors.Black
        scv.Panel.AddView(lbl,0,i*20dip,49dip,19dip)

        Dim lbl As Label
        lbl.Initialize(0)
        lbl.Text="B"&i   
        lbl.Color = Colors.LightGray
        lbl.TextColor=Colors.Black
        scv.Panel.AddView(lbl,50dip,i*20dip,49dip,19dip)
    
        For j=0 To 10
            Dim lbl As Label
            lbl.Initialize(0)
            lbl.Text=i & ":" & j
            lbl.Color = Colors.White
            hsv.Panel.AddView(lbl,j*100dip,i*20dip,99dip,19dip)
        Next
Next
scv.Panel.Height=50*20dip
hsv.Height=50*20dip
hsv.Panel.Width=j*100dip
Image1.jpg WhatsApp Image 2018-03-09 at 01.33.14.jpeg
 
Last edited:

MarkusR

Well-Known Member
Licensed User
Longtime User
nice,thanks,its very useful.
suggestion, the first label should have a eventname and a id/nr in tag
at click you can get this row number via cast sender to label
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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

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.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Page2")

    Dim scv As ScrollView
    Dim hsv As HorizontalScrollView
 
    scv.Initialize(0)
    Activity.AddView(scv,0,0,100%x,100%y)
 
    hsv.Initialize(0,"")
    scv.Panel.AddView(hsv,100dip,0,100%x-100dip,100%y)
 
 
    For i=0 To 50
        Dim lbl As Label
        lbl.Initialize("MyRow")
        lbl.Tag=i 'Row ID
        lbl.Text="A"&i
        lbl.Color = Colors.LightGray
        lbl.TextColor=Colors.Black
        scv.Panel.AddView(lbl,0,i*20dip,49dip,19dip)

        Dim lbl As Label
        lbl.Initialize(0)
        lbl.Text="B"&i
        lbl.Color = Colors.LightGray
        lbl.TextColor=Colors.Black
        scv.Panel.AddView(lbl,50dip,i*20dip,49dip,19dip)
  
        For j=0 To 10
            Dim lbl As Label
            lbl.Initialize(0)
            lbl.Text=i & ":" & j
            lbl.Color = Colors.White
            hsv.Panel.AddView(lbl,j*100dip,i*20dip,99dip,19dip)
        Next
    Next
    scv.Panel.Height=50*20dip
    hsv.Height=50*20dip
    hsv.Panel.Width=j*100dip
  
End Sub

Sub MyRow_Click
  
    Dim l As Label
    If Sender Is Label Then l=Sender
  
    Log(l.Tag)
      
End Sub
 
Upvote 0

hasanaydin52

Member
Licensed User
Longtime User
Fixed Columns and Fixed Rows Schematic

fixedrowcol.jpg WhatsApp Image 2018-03-09 at 17.26.05.jpeg


Example Code
B4X:
Sub Globals
    Dim hsvFixed As HorizontalScrollView
    Dim hsv As HorizontalScrollView
    Dim ActiveView As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim pnlFixed As Panel
 
    Dim scv As ScrollView
     
    pnlFixed.Initialize(0)
    pnlFixed.Enabled=False
    Activity.AddView(pnlFixed,0,0,100dip,40dip)
 
    hsvFixed.Initialize(0,"hsvFixed")
    Activity.AddView(hsvFixed,100dip,0,100%x-100dip,40dip)
 
    scv.Initialize(0)
    Activity.AddView(scv,0,41dip,100%x,100%y-41dip)
 
    hsv.Initialize(0,"hsv")
    scv.Panel.AddView(hsv,100dip,0,100%x-100dip,scv.Height)
 
    'fixed left top
    For i=0 To 1
        Dim lbl As Label
        lbl.Initialize(0)
        lbl.Text="H:"&i
        lbl.Color = Colors.Blue
        lbl.TextColor=Colors.White
        lbl.Gravity=Gravity.CENTER
        pnlFixed.AddView(lbl,i*50dip,0,49dip,19dip)
     
        Dim lbl As Label
        lbl.Initialize(0)
        lbl.Text="FR:"&i
        lbl.Color = Colors.LightGray
        lbl.TextColor=Colors.Black
        pnlFixed.AddView(lbl,i*50dip,20dip,49dip,19dip)    
    Next
 
    'fixed top right
    For i=0 To 11
        Dim lbl As Label
        lbl.Initialize(0)
        lbl.Text="Header:"&(i+2)
        lbl.Color = Colors.Blue
        lbl.TextColor=Colors.White
        lbl.Gravity=Gravity.CENTER
        hsvFixed.panel.AddView(lbl,i*100dip,0,99dip,19dip)
     
        Dim lbl As Label
        lbl.Initialize(0)
        lbl.Text="FR:"&(i+2)
        lbl.Color = Colors.LightGray
        lbl.TextColor=Colors.Black
        hsvFixed.Panel.AddView(lbl,i*100dip,20dip,99dip,19dip)
    Next
 
 
    For i=0 To 50
        Dim lbl As Label
        lbl.Initialize(0)
        lbl.Text="A"&i
        lbl.Color = Colors.LightGray
        lbl.TextColor=Colors.Black
        scv.Panel.AddView(lbl,0,i*20dip,49dip,19dip)

        Dim lbl As Label
        lbl.Initialize(0)
        lbl.Text="B"&i    
        lbl.Color = Colors.LightGray
        lbl.TextColor=Colors.Black
        scv.Panel.AddView(lbl,50dip,i*20dip,49dip,19dip)
     
        For j=0 To 10
            Dim lbl As Label
            lbl.Initialize("lblRightCells")
            lbl.Text=i & ":" & j
            lbl.Color = Colors.White
            hsv.Panel.AddView(lbl,j*100dip,i*20dip,99dip,19dip)
        Next
    Next
    scv.Panel.Height=51*20dip
    hsv.Height=51*20dip
    hsvFixed.Panel.Width=11*100dip
    hsv.Panel.Width=11*100dip

End Sub

Sub hsv_ScrollChanged(Position As Int)
    hsvFixed.ScrollToNow(Position)
End Sub
 
Upvote 0

hasanaydin52

Member
Licensed User
Longtime User
nice,thanks,its very useful.
suggestion, the first label should have a eventname and a id/nr in tag
at click you can get this row number via cast sender to label

Thank you MarkusR,

This is only idea. it can be improved.

For example i used this idea my program as below.

WhatsApp Image 2018-03-09 at 17.40.17.jpeg
 
Upvote 0
Top