Android Question Change the color of a B4X Table column according to the content of another

Sergio Castellari

Member
Licensed User
Hello,

I need to change the color of the content of one column, according to the content of another.
In the example I test, they change the color, if the content has a positive or negative value. In my case, I want the color to change, but if another column contains an "S" or "".
This code is the current example and it works:

B4X:
Private Sub CreateCustomFormat (c As B4XTableColumn)
   Dim formatter As B4XFormatter
   formatter.Initialize
   c.Formatter = formatter
   Dim Positive As B4XFormatData = c.Formatter.NewFormatData
   Positive.TextColor = B4XTable1.TextColor
   Positive.FormatFont = xui.CreateDefaultBoldFont(16)
   c.Formatter.AddFormatData(Positive, 0, c.Formatter.MAX_VALUE, True) 'Inclusive (zero included)
   Dim Negative As B4XFormatData = c.Formatter.CopyFormatData(Positive)
   Negative.TextColor = xui.Color_Red
   Negative.FormatFont = xui.CreateDefaultBoldFont(14)
   Negative.Prefix = "("
   Negative.Postfix = ")"
   c.Formatter.AddFormatData(Negative,c.Formatter.MIN_VALUE, 0, False)
End Sub
But I need to change the color according to the content of another column, something like this:

Col_ENE_si = B4XTable1.AddColumn("ENE_si" , B4XTable1.COLUMN_TYPE_TEXT)
...
Private Sub ENE_Si (c As B4XTableColumn)
Dim formatter As B4XFormatter
formatter.Initialize
c.Formatter = formatter
Dim Positive As B4XFormatData = c.Formatter.NewFormatData
Positive.FormatFont = xui.CreateDefaultBoldFont(16)C
If Col_ENE_si = "S" Then --->>>> HOW DO I OBTAIN THE CONTENT OF THE COLUMN "Col_ENE_si" ???
Positive.TextColor = B4XTable1.TextColor
Else
Positive.TextColor = xui.Color_Gray
End If
c.Formatter.AddFormatData(Positive, 0, c.Formatter.MAX_VALUE, True) 'Inclusive (zero included)
End Sub

Here he tells me that the TYPES do not MATCH and of course it does not work ...

Regards,
Sergio
 

Sergio Castellari

Member
Licensed User
Thanks @jimmyF,

I was looking at the Post, but it refers to selected rows.
What I want is to change the value or content of the cell / column by one color or another depending on the value of another column.
I want to apply it to columns with numeric content.
I think it should be applied through FORMATTER, since the format is applied as the rows are displayed, that is, it is applied in real time, even before loading the data B4XTable1.SetData (data)

Hopefully someone can enlighten us!

Regards,
Sergio
 

Erel

Administrator
Staff member
Licensed User
B4XFormatter will not help here.

You need to handle the DataUpdated event. Example:
B4X:
Sub B4XTable1_DataUpdated
   For i = 0 To B4XTable1.VisibleRowIds.Size - 1
       Dim RowId As Long = B4XTable1.VisibleRowIds.Get(i)
       If RowId > 0 Then
           Dim pnl As B4XView = NumberColumn.CellsLayouts.Get(i + 1) '+1 because the first cell is the header
           Dim row As Map = B4XTable1.GetRow(RowId)
           Dim clr As Int
           Dim OtherColumnValue As String = row.Get(StateColumn.Id)
           If OtherColumnValue.StartsWith("A") Then clr = xui.Color_Green Else clr = xui.Color_Black
           pnl.GetView(0).TextColor = clr
       End If
   Next
End Sub
 

Sergio Castellari

Member
Licensed User
Spectacular !, thank you very much @Erel

I achieved the goal, I even decided to apply more effects (necessary to warn the user)

B4X:
Sub B4XTable1_DataUpdated
   btnNext.Enabled = B4XTable1.lblNext.Tag
   btnPrev.Enabled = B4XTable1.lblBack.Tag

  'Recorro todas las FILAS visibles...
   For i = 0 To B4XTable1.VisibleRowIds.Size - 1
       Dim RowId As Long = B4XTable1.VisibleRowIds.Get(i)
       If RowId > 0 Then
           Dim pnl  As B4XView = Col_ENE_act.CellsLayouts.Get(i + 1) '+1 la primera celda es el Header
           Dim pnl2 As B4XView = Col_AGU_act.CellsLayouts.Get(i + 1) '+1 la primera celda es el Header
           Dim pnl3 As B4XView = Col_Nombre.CellsLayouts.Get(i + 1) '+1 la primera celda es el Header
           Dim row  As Map = B4XTable1.GetRow(RowId)
           Dim clr  As Int
           Dim clr2 As Int
           Dim clr3 As Int

           'Coloreo el servicio ENERGIA
           Dim cEne As String = row.Get(Col_ENE_si.Id) 'Obtengo el contenido de la Columna/Fila
           If cEne.StartsWith("S") Then
               clr = xui.Color_Black
               pnl.GetView(0).TextColor = clr
               pnl.GetView(0).TextSize = 18
           Else
               clr = xui.Color_Gray
               pnl.GetView(0).TextColor = clr
               pnl.GetView(0).TextSize = 14
           End If

      'Coloreo el servicio AGUA
           Dim cAgu As String = row.Get(Col_AGU_si.Id) 'Obtengo el contenido de la Columna/Fila
           If cAgu.StartsWith("S") Then
               clr2 = xui.Color_Black
               pnl2.GetView(0).TextColor = clr2
               pnl2.GetView(0).TextSize = 18
           Else
               clr2 = xui.Color_Gray
               pnl2.GetView(0).TextColor = clr2
               pnl2.GetView(0).TextSize = 14
           End If
           
           'Coloreo NOMBRE de la Cuenta si posee NOTAS...
           Dim cNotas As String = row.Get(Col_Notas.Id) 'Obtengo el contenido de la Columna/Fila
         If cNotas.Length <> 0 Then clr3 = xui.Color_Red Else clr3 = xui.Color_Black
         pnl3.GetView(0).TextColor = clr3
       End If
   Next
End Sub
Regards,
Sergio
 
Top