Android Question B4XTable Fit Screen Size

jahswant

Well-Known Member
Licensed User
Longtime User
Is there a simple way to make B4XTable Fit Screen Size. I don't want horizontal scroll !
 
Solution
I just want a snippet to make it auto adapt to screen size.
Here is another way:
B4X:
Dim w As  Int = B4XTable1.mBase.Width
    w = Root.Width
    Dim l As List
    l.Initialize
    Dim k As Int
    l.AddAll(Array As Double(.20, .15, .10, .40, .10, .05))
    For Each col As B4XTableColumn In B4XTable1.Columns
        col.Width=l.Get(k)*w
        k=k+1
    Next

And another:
B4X:
Dim l As List
l.Initialize
Dim k As Int
l.AddAll(Array As Int(20%x, 15%x, 10%x, 40%x, 10%x, 5%x))
For Each col As B4XTableColumn In B4XTable1.Columns
   col.Width=l.Get(k)
   k=k+1
Next

Magma

Expert
Licensed User
Longtime User

You mean this ?
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Thanks I just want a snippet to make it auto adapt to screen size. Thanks for the links.


OK This was for stretch as the screenshot of link - post (if you get in you will see it - i am not trying to sell anything)... check the snippet... here:
B4X:
    Dim allcolwidth As Int=editcol.Width
    Dim colpadding As Int=0
    Dim newwidth As Int = 0
    Dim formwidth As Int = B4XPages.GetNativeParent(Me).Width - (table1.mBase.Left * 2) 'remove the distance of b4xtable1 from width

    For i = 0 To B4XTable1.Columns.Size-1
        Dim thiscol As B4XTableColumn = B4XTable1.Columns.get(i)
        allcolwidth=allcolwidth+thiscol.width+colpadding
    Next
    If allcolwidth<formwidth Then 'this will need the opposite...
        Dim cw As Int = ((formwidth)-(allcolwidth)) / (B4XTable1.Columns.Size.Length)  'also here opposite... then you will divide it by the size of columns
        'Log(cw)
        For i = 0 To B4XTable1.Columns.Size-1
           Dim thiscol As B4XTableColumn = B4XTable1.Columns.get(i)
            newwidth=thiscol.width+cw ' and you will use (-)
            anarraywiththesizes(i)=newwidth 'you will use after the loops... to set them
        Next
    End If

You can use this... but the opposite ...if the size-total of widths is bigger
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
simple way to make B4XTable Fit Screen Size
Bonjour: Another quick and simple way:
B4X:
Dim w As  Int = B4XTable1.mBase.Width 
    w = Root.Width
    Dim numberofcols As Int =6  'or any other number for your columns
    For Each col As B4XTableColumn In B4XTable1.Columns
        col.Width=w/numberofcols  'you can alternatively  assign different widths to dif cols
    Next
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I just want a snippet to make it auto adapt to screen size.
Here is another way:
B4X:
Dim w As  Int = B4XTable1.mBase.Width
    w = Root.Width
    Dim l As List
    l.Initialize
    Dim k As Int
    l.AddAll(Array As Double(.20, .15, .10, .40, .10, .05))
    For Each col As B4XTableColumn In B4XTable1.Columns
        col.Width=l.Get(k)*w
        k=k+1
    Next

And another:
B4X:
Dim l As List
l.Initialize
Dim k As Int
l.AddAll(Array As Int(20%x, 15%x, 10%x, 40%x, 10%x, 5%x))
For Each col As B4XTableColumn In B4XTable1.Columns
   col.Width=l.Get(k)
   k=k+1
Next
 
Upvote 0
Solution

jahswant

Well-Known Member
Licensed User
Longtime User
@Magma or @Mahares could you help me make this code work properly ?

B4X:
(ArrayIndexOutOfBoundsException) java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0

B4X:
Public Sub MakeTableFitScreenSize(B4XTable1 As B4XTable,B4XTableEditCol As B4XTableColumn)
    Dim anarraywiththesizes() As Int
    Dim allcolwidth As Int = B4XTableEditCol.Width
    Dim colpadding As Int=0
    Dim newwidth As Int = 0
    Dim formwidth As Int = B4XPages.GetNativeParent(Me).Width - (B4XTable1.mBase.Left * 2) 'remove the distance of b4xtable1 from width

    For i = 0 To B4XTable1.Columns.Size-1
        Dim thiscol As B4XTableColumn = B4XTable1.Columns.get(i)
        allcolwidth=allcolwidth+thiscol.width+colpadding
    Next
    If allcolwidth<formwidth Then 'this will need the opposite...
        Dim cw As Int = ((formwidth)-(allcolwidth))  / (B4XTable1.Columns.Size)  'also here opposite... then you will divide it by the size of columns
        Log(cw)
        For i = 0 To B4XTable1.Columns.Size-1
            Dim thiscol As B4XTableColumn = B4XTable1.Columns.get(i)
            newwidth=thiscol.width+cw ' and you will use (-)
            anarraywiththesizes(i)=newwidth 'you will use after the loops... to set them
        Next
    End If
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
could you help me make this code work properly ?
I hope this helps if I am understanding your code and post. But if you set the number of columns in this below line, in your sub
the code will work and not crash. If the table number of cols is 4 put 4
Dim anarraywiththesizes(Numberofcolumns) As Int
You could also do it this way: Dim anarraywiththesizes(B4XTable1 .Columns.Size) As Int
 
Last edited:
Upvote 0
Top