B4J Code Snippet TableView, Dynamic reSizing of Columns proportionally

Description: TableView, Dynamic Resizing of Columns proportionally

When a Form is Resized and the TableView needs to Resize this method adjusts

the Columns proportionally to their original sizes.

Columns can be set up this way:

B4X:
' Create TableView
Private Sub CreateTableView
    Dim RList As List
   
    RList.Initialize
    RList.Add("Venue")
    RList.Add("Code")
    RList.Add("RNO")
    RList.Add("Aband")
    RList.Add("Going")
    RList.Add("Dist.")
    RList.Add("Time")
    RList.Add("Status")
   
    MyTV.SetColumns(RList)
   
    ' Add column widths up to obtain total width alloted = 566
    MyTV.SetColumnWidth(0,140.0)    ' Venue    (Formula: 140 / 566 * 100 = 24.73498)
    MyTV.SetColumnWidth(1, 50.0)    ' Code
    MyTV.SetColumnWidth(2, 40.0)    ' RNO
    MyTV.SetColumnWidth(3, 60.0)    ' Aband
    MyTV.SetColumnWidth(4, 55.0)    ' Going
    MyTV.SetColumnWidth(5, 60.0)    ' Distance
    MyTV.SetColumnWidth(6, 81.0)    ' Time
    MyTV.SetColumnWidth(7, 80.0)    ' Status
   
    ' Do not want sorting on Columns
    For i= 0 To 7
        MyTV.SetColumnSortable(i, False)
    Next
   
    RList.Clear
End Sub

The TableView Resize can be set up this way to obtain Resizing.

B4X:
' Resize TableView
' Take Vertical Scroll width off Width and divide remainder by 100%
' being the total width of the Columns
Sub MyTV_Resize (Width As Double, Height As Double)
    Dim wd As Double
'    Log(Width)
    ' Adjust = 4, scroll = 14
    wd = (Width - 18) / 100.00
    ' Work Percentage Column Width
    MyTV.SetColumnWidth(0, wd * 24.73498)    ' Venue  (24.73498)
    MyTV.SetColumnWidth(1, wd *  8.83392)    ' Code
    MyTV.SetColumnWidth(2, wd *  7.06713)    ' RNO
    MyTV.SetColumnWidth(3, wd * 10.60070)    ' Aband
    MyTV.SetColumnWidth(4, wd *  9.71731)    ' Going
    MyTV.SetColumnWidth(5, wd * 10.60070)    ' Distance
    MyTV.SetColumnWidth(6, wd * 14.31095)    ' Time
    MyTV.SetColumnWidth(7, wd * 14.13427)    ' Status
    ' Percentages total to approx. 100.00
End Sub

Example program attached with detailed comments.

Tags: TableView, Column Resize
 

Attachments

  • TableViewResize.zip
    3 KB · Views: 443

ValDog

Active Member
Licensed User
Longtime User
Description: TableView, Dynamic Resizing of Columns proportionally

When a Form is Resized and the TableView needs to Resize this method adjusts

the Columns proportionally to their original sizes.

Columns can be set up this way:

B4X:
' Create TableView
Private Sub CreateTableView
    Dim RList As List
  
    RList.Initialize
    RList.Add("Venue")
    RList.Add("Code")
    RList.Add("RNO")
    RList.Add("Aband")
    RList.Add("Going")
    RList.Add("Dist.")
    RList.Add("Time")
    RList.Add("Status")
  
    MyTV.SetColumns(RList)
  
    ' Add column widths up to obtain total width alloted = 566
    MyTV.SetColumnWidth(0,140.0)    ' Venue    (Formula: 140 / 566 * 100 = 24.73498)
    MyTV.SetColumnWidth(1, 50.0)    ' Code
    MyTV.SetColumnWidth(2, 40.0)    ' RNO
    MyTV.SetColumnWidth(3, 60.0)    ' Aband
    MyTV.SetColumnWidth(4, 55.0)    ' Going
    MyTV.SetColumnWidth(5, 60.0)    ' Distance
    MyTV.SetColumnWidth(6, 81.0)    ' Time
    MyTV.SetColumnWidth(7, 80.0)    ' Status
  
    ' Do not want sorting on Columns
    For i= 0 To 7
        MyTV.SetColumnSortable(i, False)
    Next
  
    RList.Clear
End Sub

The TableView Resize can be set up this way to obtain Resizing.

B4X:
' Resize TableView
' Take Vertical Scroll width off Width and divide remainder by 100%
' being the total width of the Columns
Sub MyTV_Resize (Width As Double, Height As Double)
    Dim wd As Double
'    Log(Width)
    ' Adjust = 4, scroll = 14
    wd = (Width - 18) / 100.00
    ' Work Percentage Column Width
    MyTV.SetColumnWidth(0, wd * 24.73498)    ' Venue  (24.73498)
    MyTV.SetColumnWidth(1, wd *  8.83392)    ' Code
    MyTV.SetColumnWidth(2, wd *  7.06713)    ' RNO
    MyTV.SetColumnWidth(3, wd * 10.60070)    ' Aband
    MyTV.SetColumnWidth(4, wd *  9.71731)    ' Going
    MyTV.SetColumnWidth(5, wd * 10.60070)    ' Distance
    MyTV.SetColumnWidth(6, wd * 14.31095)    ' Time
    MyTV.SetColumnWidth(7, wd * 14.13427)    ' Status
    ' Percentages total to approx. 100.00
End Sub

Example program attached with detailed comments.

Tags: TableView, Column Resize


I am using DBUtils.ExecuteTableView to populate my TableView, which shows all the columns in the database squished together. I've tried to use the SetColumnWidth to size each column, but it does do anything. Any suggestions?
 

BPak

Active Member
Licensed User
Longtime User
I am using DBUtils.ExecuteTableView to populate my TableView, which shows all the columns in the database squished together. I've tried to use the SetColumnWidth to size each column, but it does do anything. Any suggestions?

Make a new Sub in DBUtils

B4X:
' uses same code as above but uses pre-set TableView Columns
Public Sub LoadTableView(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, _
    TableView1 As TableView)
   
    TableView1.Items.Clear
   
    Dim cur As ResultSet
    If StringArgs = Null Then
        Dim StringArgs(0) As String
    End If
    cur = SQL.ExecQuery2(Query, StringArgs)
'    Dim cols As List
'    cols.Initialize
'    For i = 0 To cur.ColumnCount - 1
'        cols.Add(cur.GetColumnName(i))
'    Next
'    TableView1.SetColumns(cols)
   
    Do While cur.NextRow
        Dim values(cur.ColumnCount) As String
        For col = 0 To cur.ColumnCount - 1
            values(col) = cur.GetString2(col)
        Next
        TableView1.Items.Add(values)
        If Limit > 0 AND TableView1.Items.Size >= Limit Then Exit
    Loop
    cur.Close
End Sub

It is the same as the ExecuteTableView with some code commented out.
B4X:
    Dim cols As List
    cols.Initialize
    For i = 0 To cur.ColumnCount - 1
        cols.Add(cur.GetColumnName(i))
    Next
    TableView1.SetColumns(cols)

That code is where the Columns are set in ExecuteTableView and are not needed if the CodeSnippet coding style is used for setting up Tables.
 

ValDog

Active Member
Licensed User
Longtime User
BPak, so I guess you r saying to run CreateTableView(), above, and then LoadTableView() - is that right?

UPDATE: So, I ran it as above and got the table to display - but the column width settings don't seem to take - they are all the same width - still squished together...
 
Last edited:

BPak

Active Member
Licensed User
Longtime User
Use CreateTableView First! It sets up the Table.

Then call DBUtils.LoadTableView instead of the ExecuteTableView. This Loads the data into the TableView using the CreateTableView settings.

Post your code up on forum - Use File->Export as zip.
 

ValDog

Active Member
Licensed User
Longtime User
Thanks. DB files are in Object folder.


UPDATE:

BPak, found my problem - I was resizing it with older code. Thanks!
 

Attachments

  • GSPC.zip
    113.2 KB · Views: 372
Last edited:

Kevin Golding

Member
Licensed User
Longtime User
A bit late to the party, but today I came across the column automatic resize requirement, and I wrote the following sub that inspects the table data and resize's the columns according to the width of the string data. Pass the TableView object, the minimum & maximum column width in characters:

B4X:
Sub AutoSizeTableCols(tv As TableView, minWidthChars As Int, maxWidthChars As Int)
    Dim colChars(tv.ColumnsCount) As Int
  
    ' Get the max length of the string data by looking in all rows and cols
    For Each row() As Object In tv.Items
        For c = 0 To tv.ColumnsCount - 1
            Dim s As String = row(c)
            colChars(c) = Max(s.Length, colChars(c))
        Next
    Next
  
    Dim totalChars As Int    ' Total length in chars of all the widest cols
    For i = 0 To colChars.Length - 1
        If colChars(i) < minWidthChars Then
            colChars(i) = minWidthChars        ' Enforce minimum width
        Else If colChars(i) > maxWidthChars Then
            colChars(i) = maxWidthChars        ' Enforce maximum width
        End If
        totalChars = totalChars + colChars(i)
    Next
  
    ' Set the size of the columns proportionally based on character widths
    For i = 0 To colChars.Length - 1
        tv.SetColumnWidth(i, colChars(i) / totalChars * (tv.Width - 20))    ' -20 to allow for scroll bar
    Next
End Sub
 
Last edited:
Top