B4J Question How To Set B4XTable Column Width By Percent of Parent Container

cklester

Well-Known Member
Licensed User
My search of the forum has proved fruitless, so now I must ask:

I want to set the two columns of my B4XTable to 20% and 80% of the width of the table container or base (?). Here's what I've tried that didn't work:

B4X:
    Dim BaseAssetColumn As B4XTableColumn = table_ExchangePairs.AddColumn("Base Asset",table_ExchangePairs.COLUMN_TYPE_TEXT)
    Dim QuoteAssetColumn As B4XTableColumn = table_ExchangePairs.AddColumn("Quote Asset(s)",table_ExchangePairs.COLUMN_TYPE_TEXT)

    BaseAssetColumn.Width = table_ExchangePairs.mBase.width * 0.2
    QuoteAssetColumn.Width = table_ExchangePairs.mBase.Width - BaseAssetColumn.Width

How do I set the column widths by percent of the table parent?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private B4XTable1 As B4XTable
    Private col1, col2 As B4XTableColumn
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    col1 = B4XTable1.AddColumn("Col 1 ", B4XTable1.COLUMN_TYPE_NUMBERS)
    col2 = B4XTable1.AddColumn("Col 2 ", B4XTable1.COLUMN_TYPE_NUMBERS)
    Dim data As List
    data.Initialize
    For i = 1 To 100
        data.Add(Array(i, i))
    Next
    B4XTable1.SetData(data)
End Sub

Private Sub B4XPage_Resize (Width As Int, Height As Int)
    col1.Width = B4XTable1.mBase.Width * 0.2
    col2.Width = B4XTable1.mBase.Width - col1.Width - 2dip
    B4XTable1.Refresh
End Sub
Note that B4XPage_Resize is only raised in B4J and B4i (the page is never resized in B4A).
 
Upvote 0

cklester

Well-Known Member
Licensed User

Works great! I have a problem, though... The B4XTable is on the right side of a splitter view, and it doesn't get resized when the splitter is adjusted.

I tried using a
B4X:
Private Sub splitter_Exchanges_Resize (Width As Double, Height As Double)
sub, but that didn't seem to work.

Is there a way to get the B4XTable to resize when the splitter is adjusted?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User


The trick is to put the table inside a panel and handle its resize event:
B4X:
Private Sub TableParent_Resize (Width As Double, Height As Double)
    col1.Width = B4XTable1.mBase.Width * 0.2
    col2.Width = B4XTable1.mBase.Width - col1.Width - 2dip
    B4XTable1.Refresh
End Sub
 

Attachments

  • Project.zip
    10.2 KB · Views: 131
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…