Android Question How to set text alignment in B4XTable?

Marlou Fin

Member
Licensed User
Hi Guys,

How can we set the text alignement of the table.

I tried the B4XTable1.mBase.SetTextAlignment("TOP","LEFT") but getting this error below.

Error occurred on line: 93 (Customer_Form)
java.lang.RuntimeException: Type does not match (class anywheresoftware.b4a.BALayout)
at anywheresoftware.b4a.objects.B4XViewWrapper.typeDoesNotMatch(B4XViewWrapper.java:361)
at anywheresoftware.b4a.objects.B4XViewWrapper.asLabelWrapper(B4XViewWrapper.java:193)
at anywheresoftware.b4a.objects.B4XViewWrapper.SetTextAlignment(B4XViewWrapper.java:262)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:250)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
at anywheresoftware.b4a.BA$2.run(BA.java:370)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
(Exception) java.lang.Exception: java.lang.RuntimeException: Type does not match (class anywheresoftware.b4a.BALayout)
 

mangojack

Well-Known Member
Licensed User
I'm unsure if there is a more direct way .. also if below code is technically 'Correct'

Following example code shown here .. https://www.b4x.com/android/forum/threads/b4x-b4xtable-with-custom-cells-layout.102352/

B4X:
B4XTable1.AddColumn("State", B4XTable1.COLUMN_TYPE_TEXT)   
B4XTable1.AddColumn("Interesting Number", B4XTable1.COLUMN_TYPE_NUMBERS)
 
B4XTable1.MaximumRowsPerPage = 10
B4XTable1.BuildLayoutsCache(10)
 
SetColumnAlignment("State", Gravity.Left)  'columnID, Alignment
SetColumnAlignment("Interesting Number", Gravity.RIGHT)  'columnID, Alignment
 
'more code ...........
   
End Sub

Sub SetColumnAlignment(columnID As String, alignmentX As Int)
 
    Dim column As B4XTableColumn = B4XTable1.GetColumn(columnID)
    For i = 1 To column.CellsLayouts.Size - 1        'starts at 1 due to header
        Dim pnl As B4XView = column.CellsLayouts.Get(i)
        Dim lbl As Label = pnl.GetView(0)   
        lbl.Gravity = Bit.Or(Gravity.CENTER_VERTICAL, alignmentX)
    Next
 
End Sub
 
Last edited:

Alvsky

Member
Licensed User
I have multiple tables in one activity so I needed something more universal.
I also noticed that, from some reason, alignment needs to be in upper case.
So this is my solution and I hope it will help someone:

B4X:
Sub SetColumnAlignment(tableName As B4XTable, columnID As String, alignment As String)
 
    Dim column As B4XTableColumn = tableName.GetColumn(columnID)
    For i = 1 To column.CellsLayouts.Size - 1        'starts at 1 due to header
        Dim pnl As B4XView = column.CellsLayouts.Get(i)
        pnl.GetView(0).SetTextAlignment("CENTER", alignment.ToUpperCase)
    Next
    
End Sub
 
Top