Android Question Is it possible to change the color of individual items in a B4XComboBox?

mscientist33

Active Member
Licensed User
I have a combo box that is populated by an sql statement. If a field is 1 I want the color of the item added to be Green, if it is 0 I want the color of the item to be white. I would prefer the background of each item to change colors but I will settle for just the text color of each item. Is this possible?
 
Solution
With the code below, the text is still black
Should be something like this:
B4X:
Dim lstItems As List
    lstItems.Initialize    
    Dim rs As ResultSet
    Do While rs.NextRow
        If rs.GetString("n").Contains("1") Then
             lstItems.Add(cs.Initialize.Color(Colors.Green).Append(rs.GetString("n")).PopAll)
        Else
            lstItems.Add(cs.Initialize.Color(Colors.Red).Append(rs.GetString("n")).PopAll)            
        End If
    Loop
    rs.close
    B4XComboBox1.SetItems(lstItems)

Mahares

Expert
Licensed User
Longtime User
f a field is 1 I want the color of the item added to be Green, if it is 0 I want the color of the item to be white.
Yes. You can use CSBuilder when you build your list items. See example:
lstItems.Initialize
B4X:
Dim cs As CSBuilder
    lstItems.AddAll(Array As Object(cs.Initialize.Color(xui.Color_Red).Append("Dog").PopAll, _
    cs.Initialize.Color(xui.Color_Magenta).Append("Cat").PopAll, "Bird", "Camel", "Tiger", "Elephant"))
Of course you replace the cats and dogs with rs.getstring("col")
 
Upvote 0

mscientist33

Active Member
Licensed User
This is what I have. a B4XCombobox named Books. With the code below, the text is still black. I have cs defined in global As CSBuilder.

B4X:
    Dim mbook As String= cs.Initialize.Color(Colors.Green).Append(rs.GetString("n")).PopAll
    Books.cmbBox.Add(mbook)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
With the code below, the text is still black
Should be something like this:
B4X:
Dim lstItems As List
    lstItems.Initialize    
    Dim rs As ResultSet
    Do While rs.NextRow
        If rs.GetString("n").Contains("1") Then
             lstItems.Add(cs.Initialize.Color(Colors.Green).Append(rs.GetString("n")).PopAll)
        Else
            lstItems.Add(cs.Initialize.Color(Colors.Red).Append(rs.GetString("n")).PopAll)            
        End If
    Loop
    rs.close
    B4XComboBox1.SetItems(lstItems)
 
Upvote 1
Solution
Top