Android Question [SOLVED] How do I set two decimals in a B4XTable number column?

Sergio Castellari

Active Member
Licensed User
Hello,

I need a numeric type column to have two decimal places.
I also need that in another column of type Date, I can format it as follows: "DD-mm-yyyy" (day-month-year)

Cheers
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I need a numeric type column to have two decimal places.
Add this code:
B4X:
Dim fd As B4XFormatData = B4XTable1.DefaultDataFormatter.GetDefaultFormat
fd.MaximumFractions = 2
fd.MinimumFractions = 2

I also need that in another column of type Date, I can format it as follows: "DD-mm-yyyy" (day-month-year)
Set DateTime.DateFormat.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
What if I have some columns with two decimals and others whole?
Thanks but i don't understand
Here is how I have been doing it: I created a function as below:
B4X:
Private Sub CreateCustomFormat (c As B4XTableColumn, Mx As Int, Mn As Int )
    Dim formatter As B4XFormatter
    formatter.Initialize
    c.Formatter = formatter
    c.formatter.GetDefaultFormat.MaximumFractions = Mx
    c.formatter.GetDefaultFormat.MinimumFractions = Mn
    c.Formatter.GetDefaultFormat.TextColor =Colors.Blue  'optional line
End Sub
And to format the columns:
B4X:
CreateCustomFormat(B4XTable1.GetColumn("latitude"),2,2)  'latitude is name of column
CreateCustomFormat(B4XTable1.GetColumn("population"),0,0)
 
Upvote 0

Sergio Castellari

Active Member
Licensed User
Hello @Mahares,
Many thanks!. This was what you needed and did not understand.
I implemented it like this:

B4X:
       'Les coloco un ancho personalizado a cada columna
       Col_Codigo.Width = 20%x 
       Col_Nombre.Width = 45%x 
       Col_Mov.Width    = 25%x
       Col_Saldo.Width  = 30%x
       B4XTable1.MaximumRowsPerPage = 15            'Limita a 15 registros por pagina
       B4XTable1.BuildLayoutsCache(15)              'Fuerza re-dibujar las celdas cuando cambia de tamaño la vista
       SeteoAlineacion("SALDO", "CENTER", "RIGHT")  'COLUMNA, vertical, horizontal
        SeteoDecimales("SALDO",2,2)                  'COLUMNA, Maximo,Minimo Decimales
        SeteoDecimales("CODIGO",1,1)                 'COLUMNA, Maximo,Minimo Decimales
        DateTime.DateFormat ="dd/MM/yyyy"            'Doy formato a la Fecha
        B4XTable1.Refresh

'****************************************************************************
'* SeteoAlineacion() Permite Setear ALINEACION a una columna.               *
'*          Recibe: columnID ---> Nombre COLUMNA, String                    *
'*                  alignmentV -> Alineacion Vertical, Strinng              *
'*                  alignmentH -> Alineacion Horizantal, Strinng            *
'************************************************************* 12-05-2020 ***
Sub SeteoAlineacion(columnID As String, alignmentV As String, alignmentH As String)
    Dim column As B4XTableColumn = B4XTable1.GetColumn(columnID)
    For i = 1 To column.CellsLayouts.Size - 1        'comienza en 1 debido al encabezado
    Dim pnl As B4XView = column.CellsLayouts.Get(i)
    pnl.GetView(0).SetTextAlignment(alignmentV, alignmentH)
    Next
End Sub

'****************************************************************************
'* SeteoDecimales() Permite Setear DECIMALES especificamente a una columna. *
'*          Recibe: columnID ---> Nombre COLUMNA, String                    *
'*                  nMax -------> Maximo Decimal, Int                       *
'*                  nMin -------> Minimo Decimal, Int                       *
'************************************************************* 12-05-2020 ***
Private Sub SeteoDecimales(columnID As String, nMax As Int, nMin As Int)
    Dim c As B4XTableColumn = B4XTable1.GetColumn(columnID)
  Dim formatter As B4XFormatter
  formatter.Initialize
  c.Formatter = formatter
  c.formatter.GetDefaultFormat.MaximumFractions = nMax
  c.formatter.GetDefaultFormat.MinimumFractions = nMin
  'c.Formatter.GetDefaultFormat.TextColor =Colors.White
End Sub

To complete this thread, I would like the possibility of making a "SUB" that can indicate column and color.
The thing is, I don't know how to send the color.

'c.Formatter.GetDefaultFormat.TextColor =Colors.White 'How to send the color?

Grateful!
Hugs!
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
The thing is, I don't know how to send the color.
You can also create a custom color like this for any given column:
B4X:
Sub CreateColorFormat (c As B4XTableColumn, clr As Int )
    Private formatter As B4XFormatter
    formatter.Initialize
    c.Formatter = formatter
    c.Formatter.GetDefaultFormat.TextColor =clr
End Sub
B4X:
CreateColorFormat(B4XTable1.GetColumn("longitude"), Colors.Magenta)  'you can also use the XUI colors
 
Upvote 0

Sergio Castellari

Active Member
Licensed User
Excellent!!!
It works perfect!
I apply it like this:

B4X:
SeteoColores("CODIGO",xui.Color_ARGB(255,138,43,226)) 'COLUMNA, Color


'****************************************************************************
'* SeteoColores() Permite Setear COLOR TEXTO especificamente a una columna. *
'*          Recibe: columnID ---> Nombre COLUMNA, String                    *
'*                  clr --------> COLOR, Int                                *
'************************************************************* 12-05-2020 ***
Sub SeteoColores(columnID As String, clr As Int )
    Dim c As B4XTableColumn = B4XTable1.GetColumn(columnID)
  Dim formatter As B4XFormatter
  formatter.Initialize
  c.Formatter = formatter
  c.Formatter.GetDefaultFormat.TextColor = clr
End Sub

Thanks @Mahares!
Also @Erel for the IDE. It really helps to generate a clean code, also as programs guide you with some programming topics. Spectacular!

Hugs!
 
Upvote 0

Sergio Castellari

Active Member
Licensed User
As a final stage, unify the "SUB" like this:

B4X:
SeteoColumna("SALDO", "CENTER", "RIGHT",2,2,-1)
SeteoColumna("CODIGO", "CENTER", "RIGHT",0,0,xui.Color_ARGB(255,138,43,226))


'******************************************************************************
'* SeteoColumna() Permite Setear ALINEACION,DECIMALES y COLOR a una columna.  *
'*          Recibe: columnID ---> Nombre COLUMNA, String                      *
'*                  alignmentV -> Alineacion Vertical, Strinng                *
'*                  alignmentH -> Alineacion Horizantal, Strinng              *
'*                  nMax -------> Maximo Decimal, Int [-1]=NO aplica          *
'*                  nMin -------> Minimo Decimal, Int                         *
'*                  clr --------> COLOR, Int          [-1]=NO aplica          *
'*************************************************************** 12-05-2020 ***
Sub SeteoColumna(columnID As String, alignmentV As String, alignmentH As String, nMax As Int, nMin As Int, clr As Int)
    Dim column As B4XTableColumn = B4XTable1.GetColumn(columnID)
    Dim c As B4XTableColumn = B4XTable1.GetColumn(columnID)
  Dim formatter As B4XFormatter
  formatter.Initialize
  c.Formatter = formatter
    'Seteo ALINEACION
    For i = 1 To column.CellsLayouts.Size - 1        'comienza en 1 debido al encabezado
    Dim pnl As B4XView = column.CellsLayouts.Get(i)
    pnl.GetView(0).SetTextAlignment(alignmentV, alignmentH)
    Next
    'Seteo DECIMALES si el parametros es distinto de -1
  If nMax <> -1 Then
    c.formatter.GetDefaultFormat.MaximumFractions = nMax
    c.formatter.GetDefaultFormat.MinimumFractions = nMin
    End If
    'Seteo COLOR si el parametros es distinto de -1
  If clr <> -1 Then
        c.Formatter.GetDefaultFormat.TextColor = clr
    End If
End Sub

The idea is to have less code and simpler when configuring the column.
I am not sure if it is correct, the way to pass parameters [-1] as null or that I don't want to modify.
This routine works correctly.

Hugs
 
Upvote 0
Top