Android Question [B4XTable] Change text colour of a cell by value

vecino

Well-Known Member
Licensed User
Longtime User
Hi, I want to show a different colour of a cell as shown in this image (Interesting Number), how can I do it?
I know there was a thread explaining this, but I can't find it.
Thank you.

SS-2019-02-07_15.33.03.png
 
Solution
When are you told to write in a different colour when the value is greater than 1?
It works for me. Look at my code my man
B4X:
Private  formatter As B4XFormatter
B4X:
CreateCustomFormat(B4XTable1.GetColumn("Student_Num"))
    formatter.GetDefaultFormat.TextColor = xui.Color_Black
B4X:
Private Sub CreateCustomFormat (c As B4XTableColumn)
    formatter.Initialize
    c.Formatter = formatter
    Dim Positive As B4XFormatData = c.Formatter.NewFormatData
    Positive.TextColor = xui.Color_Red
'    Positive.TextColor = B4XTable1.TextColor
    Positive.FormatFont = xui.CreateDefaultFont(22)
    c.Formatter.AddFormatData(Positive, 114.01, c.Formatter.MAX_VALUE, True) 'change your 114.01 to 1.01
End Sub

1643917629534.png

Xfood

Expert
Licensed User
Upvote 1

vecino

Well-Known Member
Licensed User
Longtime User
Hi, I can't seem to change the colour of the text in the cell I need. In that link they explain how to change the background of the cell, but I can't find how to change the text.
It should be easy, but so far I can't do it.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
I already found it in the initial example of Erel presenting B4XTable.
Thank you, friends.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Finally, after adapting to take advantage of here's example, I cannot get it to work.
I'm using this code, I don't know what I need to add or change.
What I want is to change the colour of the text in the column "cantidad" (col5), if the value is greater than 1, show the text in green.
Thank you.

B4X:
Sub ShowContent( llrows As List )
    Dim col0, col1, col2, col3, col4, col5, col6 As B4XTableColumn
    '
    col0 = tbVC.AddColumn("id",tbVC.COLUMN_TYPE_NUMBERS)
    col1 = tbVC.AddColumn("nombre",tbVC.COLUMN_TYPE_TEXT)
    col2 = tbVC.AddColumn("estado",tbVC.COLUMN_TYPE_TEXT)
    col3 = tbVC.AddColumn("creado",tbVC.COLUMN_TYPE_TEXT)
    col4 = tbVC.AddColumn("observaciones",tbVC.COLUMN_TYPE_TEXT)
    col5 = tbVC.AddColumn("cantidad",tbVC.COLUMN_TYPE_TEXT) 
    CreateCustomFormat(col5) 
    col6 = tbVC.AddColumn("nombresintildes",tbVC.COLUMN_TYPE_TEXT)
    col0.Width = -1
    col1.Width = tbVC.mBase.Width * 0.40
    col2.Width = tbVC.mBase.Width * 0.10
    col3.Width = tbVC.mBase.Width * 0.10
    col4.Width = tbVC.mBase.Width * 0.30
    col5.Width = tbVC.mBase.Width * 0.10
    col6.Width = -1
    ' 
    For Each tc As B4XTableColumn In Array(col0,col2,col3,col5)
        tc.Searchable = False
    Next 
    '
    tbVC.Refresh
    tbVC.SearchField.RequestFocusAndShowKeyboard
    '
    tbVC.SetData(llrows)
End Sub

Private Sub CreateCustomFormat (c As B4XTableColumn)
    Dim formatter As B4XFormatter
    formatter.Initialize
    c.Formatter = formatter
    ' >1
    Dim Mayor1 As B4XFormatData = c.Formatter.NewFormatData
    Mayor1.TextColor = tbVC.TextColor
    Mayor1.FormatFont = xui.CreateDefaultFont(16)
    c.Formatter.AddFormatData(Mayor1, 2, c.Formatter.MAX_VALUE, True)
    '    <=1 
    Dim Menor2 As B4XFormatData = c.Formatter.CopyFormatData(Mayor1)
    Menor2.TextColor = xui.Color_Green
    Menor2.FormatFont = xui.CreateDefaultBoldFont(16)
    Menor2.Prefix = "("
    Menor2.Postfix = ")"
    c.Formatter.AddFormatData(Menor2,c.Formatter.MIN_VALUE,1, False)
End Sub
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Nor does it :(
I checked and it has to be NUMBERS and not set up as TEXT when you prepare the columns. It has to be after all the columns are defined, not between col5 and col6
To make sure you got things right try this. It works for me. Use your table name, mine is B4XTable1 It does not solve your problem, just to make sure you are on the right track:
B4X:
CreateColorFormat(B4XTable1.GetColumn("candidad"), Colors.Magenta) 'line after setting data type. the column MUST be of type NUMBERS

B4X:
Sub CreateColorFormat (c As B4XTableColumn, clr As Int )
Dim formatter As B4XFormatter
formatter.Initialize
c.Formatter = formatter
c.Formatter.GetDefaultFormat.TextColor =clr
End Sub
 
Last edited:
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Just in case, I've simplified it and it doesn't work either.

B4X:
Sub ShowVerContent( llrows As List )
    Dim col0, col1, col2, col3, col4, col5, col6 As B4XTableColumn
    
    col0 = tbVC.AddColumn("id",tbVC.COLUMN_TYPE_NUMBERS)
    col1 = tbVC.AddColumn("nombre",tbVC.COLUMN_TYPE_TEXT)
    col2 = tbVC.AddColumn("estado",tbVC.COLUMN_TYPE_TEXT)
    col3 = tbVC.AddColumn("creado",tbVC.COLUMN_TYPE_TEXT)
    col4 = tbVC.AddColumn("observaciones",tbVC.COLUMN_TYPE_TEXT)
    col5 = tbVC.AddColumn("cantidad",tbVC.COLUMN_TYPE_NUMBERS)   
    col6 = tbVC.AddColumn("nombresintildes",tbVC.COLUMN_TYPE_TEXT)

'    CreateCustomFormat(col5)
    CreateColorFormat(tbVC.GetColumn("candidad"), Colors.Magenta) 'line after setting data type. the column MUST be of type NUMBERS
    
    tbVC.Refresh
    tbVC.SetData(llrows)
End Sub

Sub CreateColorFormat (c As B4XTableColumn, clr As Int )
    Dim formatter As B4XFormatter
    formatter.Initialize
    c.Formatter = formatter
    c.Formatter.GetDefaultFormat.TextColor =clr
End Sub
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Now that works!!!!!
I had just "copied and pasted" it.

GetColumn("candidad")
It is "cantidad".

tampoco.png
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Now that works!!!!!
You are definitely in the right track. Sometimes it is hard when you are not seeing the project in full in front of you. You can wait, hopefully someone will spot it or post a little project using your code for the formatting.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Thank you, and now how can I make it change colour only when the value is greater than 1?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
how can I make it change colour only when the value is greater than 1
B4X:
CreateCustomFormat(B4XTable1.GetColumn("cantidad"))
 formatter.GetDefaultFormat.TextColor = xui.Color_Black   'you need this line too
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 = xui.Color_Red
'    Positive.TextColor = B4XTable1.TextColor
    Positive.FormatFont = xui.CreateDefaultFont(22)
    c.Formatter.AddFormatData(Positive, 2, c.Formatter.MAX_VALUE, True)   'this is for 2 and bigger. Change it to your minimum
End Sub
 
Upvote 1

vecino

Well-Known Member
Licensed User
Longtime User
formatter.GetDefaultFormat.TextColor = xui.Color_Black 'you need this line too

¿formatter? But that gives error, what is formatter there?
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
It has no effect :(
When are you told to write in a different colour when the value is greater than 1?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
When are you told to write in a different colour when the value is greater than 1?
It works for me. Look at my code my man
B4X:
Private  formatter As B4XFormatter
B4X:
CreateCustomFormat(B4XTable1.GetColumn("Student_Num"))
    formatter.GetDefaultFormat.TextColor = xui.Color_Black
B4X:
Private Sub CreateCustomFormat (c As B4XTableColumn)
    formatter.Initialize
    c.Formatter = formatter
    Dim Positive As B4XFormatData = c.Formatter.NewFormatData
    Positive.TextColor = xui.Color_Red
'    Positive.TextColor = B4XTable1.TextColor
    Positive.FormatFont = xui.CreateDefaultFont(22)
    c.Formatter.AddFormatData(Positive, 114.01, c.Formatter.MAX_VALUE, True) 'change your 114.01 to 1.01
End Sub

1643917629534.png
 
Upvote 2
Solution
Top