Android Question How to set text alignment in B4XTable?

Discussion in 'Android Questions' started by Marlou Fin, Mar 19, 2019.

  1. Marlou Fin

    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)
     
  2. mangojack

    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/

    Code:
    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: Mar 19, 2019
    Erel likes this.
  3. Marlou Fin

    Marlou Fin Member Licensed User

    Hi Mangojack,

    Thanks you for the sample, I will test this one when I get home.

    Thanks,
    Marlou
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    No reason to use platform specific code unless you must.
    Code:
    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""LEFT")
        
    Next
     
    mangojack likes this.
  5. Marlou Fin

    Marlou Fin Member Licensed User

    Hi Erel and Mango,

    This is working perfect.


    Thanks,
    Marlou
     
  6. Alvsky

    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:

    Code:
    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
     
    AHilberink and Hilton like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice