B4J Question TableView row color change

Nokia

Active Member
Licensed User
Longtime User
in table view is it possible to change either the row background color or font color on each row depending on the data in the row?
 

Nokia

Active Member
Licensed User
Longtime User
You can add labels as the items instead of strings. This will allow you to change the labels background color (with CSSUtils.SetBackgroundColor).

you have any simple examples?

this is the code I am currently using..

B4X:
  'setting table colums
   tblCS.SetColumns(Array As String("Key Name", "Company", "ID", "Key Type"))
   tblCS.SetColumnWidth(0, 145)
   tblCS.SetColumnWidth(1, 145)
   tblCS.SetColumnWidth(2, 75)
   tblCS.SetColumnWidth(3, 35)

this is in a loop
B4X:
      Dim aRow(4) As Object
       Dim str As String = k.GetString(Main.strMKDirLoc,f)
       aRow(0) = k.GetKey("kn", str)
       aRow(1) = k.GetKey("cn", str)
       aRow(2) = f
       aRow(3) = k.GetKey("kt", str)
       tblCS.Items.Add(aRow)

       C = C + 1
 
Last edited:
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
There is an example here: https://www.b4x.com/android/forum/threads/tableview-cell-alignment-and-color.35364/#post-208851

Remember to use CSSUtils instead of modifying the style property.


thanks.. Erel,

but now I have a new problem.. how do I address the text of the label on row change..

here is the code for color change

B4X:
        Dim lb1, lb2, lb3, lb4 As Label
         lb1.Initialize("")
         lb2.Initialize("")
         lb3.Initialize("")
         lb4.Initialize("")
     
         lb1.Text = KeyName
         lb2.Text = k.GetKey("cn", str)
         lb3.Text = f
         lb4.Text = kt
         
         If KeyType = "SGK" Then
           lb1.TextColor = fx.Colors.LightGray
           lb2.TextColor = fx.Colors.LightGray
           lb3.TextColor = fx.Colors.LightGray
           lb4.TextColor = fx.Colors.LightGray
         Else
           lb1.TextColor = fx.Colors.Blue
           lb2.TextColor = fx.Colors.Blue
           lb3.TextColor = fx.Colors.Blue
           lb4.TextColor = fx.Colors.Blue
         End If
         
         tblCS.Items.Add(Array As Object(lb1, lb2, lb3, lb4))

row change
B4X:
  If Index <> -1 Then
     If Row(3) <> "SGK" Then
       btCSOK.Enabled = True
       imDelete.Enabled = True
     Else
       btCSOK.Enabled = False
       imDelete.Enabled = False
     End If
   End If
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
Each item in Row is a label.
B4X:
Dim lbl As Label = Row(3)
If lbl.Text = "SGK" Then

that was simple..

so I updated my code
B4X:
Dim lb3 As Label = Row(3)

   If Index <> -1 Then
     If lb3.Text <> "SGK" Then
       btCSOK.Enabled = True
       imDelete.Enabled = True
     Else
       btCSOK.Enabled = False
       imDelete.Enabled = False
     End If
   End If

and

B4X:
  Dim arow(3) As Object = tblCS.SelectedRowValues
   Dim lb1, lb2, lb3 As Label
   
   lb1 = arow(0)
   lb2 = arow(1)
   lb3 = arow(2)
   
   Dim R(3) As Object
   R(0) = lb1.Text
   R(1) = lb2.Text
   R(2) = lb3.Text
   
   CallSub2(oClassCalled,"AsignCompanySelect",R)
   cs.close

and works good.. thanks!!!! Erel
 
Upvote 0
Top