Android Question [SOLVED]How to change the color of a B4XTable row according to content and then disable it?

Daniel44

Active Member
Licensed User
Hi everyone!

Well.. I'm working on a B4Xtable and I'd like to change X Row according to content.. Ex: I have the column "state" below it shows "Exists" and "Not Exists" and I need to change the row's color if it's "Not Exists" I've seen this post: B4XTable How do I change the color of a row ? and it paints the 5th Table Row . I need something like that but painting the whole row if it's "Not Exists" and then disable that row, I mean can see it but not able to edit it when I cell_click.

Thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change the color based on the content:
B4X:
Sub B4XTable1_DataUpdated
    For i = 0 To B4XTable1.VisibleRowIds.Size - 1
        Dim RowId As Long = B4XTable1.VisibleRowIds.Get(i)
        If RowId > 0 Then
            'criteria
            Dim Value As Double = B4XTable1.GetRow(RowId).Get(numberColumn.Id)
            If Value > 10 Then
                SetRowColor(i, xui.Color_Red)
                Continue
            End If
        End If
        If i Mod 2 = 0 Then
            SetRowColor(i, B4XTable1.EvenRowColor)
        Else
            SetRowColor(i, B4XTable1.OddRowColor)
        End If
    Next
End Sub

Sub SetRowColor (RowIndex As Int, Clr As Int)
    For Each c As B4XTableColumn In B4XTable1.VisibleColumns
        Dim pnl As B4XView = c.CellsLayouts.Get(RowIndex + 1) '+1 because of the header
        pnl.Color = Clr
    Next
End Sub

I'm not sure what you mean with "disable that row" however you can store the RowId value in a B4XSet and later use it to determine whether a row is disabled or not.
 
Upvote 0

Daniel44

Active Member
Licensed User
Change the color based on the content:
B4X:
Sub B4XTable1_DataUpdated
    For i = 0 To B4XTable1.VisibleRowIds.Size - 1
        Dim RowId As Long = B4XTable1.VisibleRowIds.Get(i)
        If RowId > 0 Then
            'criteria
            Dim Value As Double = B4XTable1.GetRow(RowId).Get(numberColumn.Id)
            If Value > 10 Then
                SetRowColor(i, xui.Color_Red)
                Continue
            End If
        End If
        If i Mod 2 = 0 Then
            SetRowColor(i, B4XTable1.EvenRowColor)
        Else
            SetRowColor(i, B4XTable1.OddRowColor)
        End If
    Next
End Sub

Sub SetRowColor (RowIndex As Int, Clr As Int)
    For Each c As B4XTableColumn In B4XTable1.VisibleColumns
        Dim pnl As B4XView = c.CellsLayouts.Get(RowIndex + 1) '+1 because of the header
        pnl.Color = Clr
    Next
End Sub

I'm not sure what you mean with "disable that row" however you can store the RowId value in a B4XSet and later use it to determine whether a row is disabled or not.
Hey Erel! thank you for help ..! I'll try this code... well when I say "disable row" I mean that row does nothing on cell_click event or long_cell_click event .. In my B4XTable I have a Cell_click event for editing and updating data.
Thank you So Much
 
Upvote 0

Daniel44

Active Member
Licensed User
Change the color based on the content:
B4X:
Sub B4XTable1_DataUpdated
    For i = 0 To B4XTable1.VisibleRowIds.Size - 1
        Dim RowId As Long = B4XTable1.VisibleRowIds.Get(i)
        If RowId > 0 Then
            'criteria
            Dim Value As Double = B4XTable1.GetRow(RowId).Get(numberColumn.Id)
            If Value > 10 Then
                SetRowColor(i, xui.Color_Red)
                Continue
            End If
        End If
        If i Mod 2 = 0 Then
            SetRowColor(i, B4XTable1.EvenRowColor)
        Else
            SetRowColor(i, B4XTable1.OddRowColor)
        End If
    Next
End Sub

Sub SetRowColor (RowIndex As Int, Clr As Int)
    For Each c As B4XTableColumn In B4XTable1.VisibleColumns
        Dim pnl As B4XView = c.CellsLayouts.Get(RowIndex + 1) '+1 because of the header
        pnl.Color = Clr
    Next
End Sub

I'm not sure what you mean with "disable that row" however you can store the RowId value in a B4XSet and later use it to determine whether a row is disabled or not.
Hey Erel I've been trying to implement your code without good result. I've changed on line 6 ' the value type to string because my column 'state' it's a string type but I don't understand 'numnercolum.Id' . Can you help with that please? Thank you
 
Upvote 0

Daniel44

Active Member
Licensed User
Hey Erel I've been trying to implement your code without good result. I've changed on line 6 ' the value type to string because my column 'state' it's a string type but I don't understand 'numnercolum.Id' . Can you help with that please? Thank you
I changed
numberColumn is a B4XTableColumn global variable. It was assigned with:
B4X:
numberColumn = B4XTable1.AddColumn("Interesting Number", B4XTable1.COLUMN_TYPE_NUMBERS)
Well I didnt that I just changed it and
numberColumn is a B4XTableColumn global variable. It was assigned with:
B4X:
numberColumn = B4XTable1.AddColumn("Interesting Number", B4XTable1.COLUMN_TYPE_NUMBERS)


Hey Erel! well I just changed that whole line and now it is:
B4X:
Dim Value As String = BX4Table1.GetRow(RowId).GetValueAt(3)
and it works fine!

thank you a lot Erel!!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
No, this is a bad solution. Never use GetValueAt.

B4XTable.GetRow returns a Map. You need to find the relevant column id. The best way to do it is like I did with numberColumn. You can however also find by logging the map:
B4X:
Log(B4XTable1.GetRow(RowId))
The solution will then be something like:
B4X:
Dim Value As String = BX4Table1.GetRow(RowId).Get("c2")
 
Upvote 0

Daniel44

Active Member
Licensed User
No, this is a bad solution. Never use GetValueAt.

B4XTable.GetRow returns a Map. You need to find the relevant column id. The best way to do it is like I did with numberColumn. You can however also find by logging the map:
B4X:
Log(B4XTable1.GetRow(RowId))
The solution will then be something like:
B4X:
Dim Value As String = BX4Table1.GetRow(RowId).Get("c2")

Great Erel! I did as you said and it returns my relevant column name. Thank so much!
 
Upvote 0
Top