B4J Code Snippet Change single view's element style (CSS subclass) at runtime

Here's how it can be done:
B4X:
Sub StyleCSSsubclass (targetView As B4XView, subclass As String, style As String)
    Dim subTarget As Node = targetView.As(JavaObject).RunMethodJO("lookup", Array(subclass))
    If subTarget <> Null Then
        Dim jo As JavaObject = subTarget
        jo.RunMethod("setStyle", Array(style))
    End If
End Sub

For example, let's change color of small arrow on right side of ComboBox (declared as ComboBox):
B4X:
StyleCSSsubclass(ComboBox1.As(B4XView), ".arrow-button .arrow", "-fx-background-color: #FF0000;")

If you don't know what subclass and style you need to use for certain view, ask ChatGPT. Better use tuned GPT - click on Explore GPTs button in ChatGPT interface and search for CSS keyword. I use HTML & CSS Expert one. Most answers are correct, or at least pointing to the right direction. This code snippet uses part of such answer.

UPD: According to Erel's advice, it should look like this:
B4X:
Sub StyleCSSsubclass (targetView As B4XView, subclass As String, property As String, value As String)
    Dim subTarget As Node = targetView.As(JavaObject).RunMethodJO("lookup", Array(subclass))
    If subTarget <> Null Then CSSUtils.SetStyleProperty(subTarget, property, value)
End Sub
And usage example:
B4X:
StyleCSSsubclass(ComboBox1.As(B4XView), ".arrow-button .arrow", "-fx-background-color", "#FF0000")
Depends on CSSUtils internal library.
 
Last edited:

Gandalf

Member
Licensed User
Longtime User
For info, the full list of CSS subclass are listed in the Modena.css
I know. Just asking ChatGPT is faster than searching through this file, especially when you are not sure about what exactly to search for.
 
Top