B4J Question How to use (load) style sheets with custom views?

Cableguy

Expert
Licensed User
Longtime User
Hi guys,

If I understand correctly, stylesheets are only possible to add at form level...
This poses me a problem, as I am designing a new CV that is higly customised and I would prefer to use a stylesheet rather than inline css.
Is it possible?
 

stevel05

Expert
Licensed User
Longtime User
If you add it to the main form, it should be available throughout your app. If you want to make different classes behave differently you can add styleclasses specific to that class.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
My problem is, since its a custom view, it may be added to a pane, or any other Parenting capable node!
And only Form seem to be able to load style sheets.
Can it be done with javaobject?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Doesn't it pick up the css if you add the file to the main form. It shouldn't matter where in the app you load the customview.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
it does, but my objective was not to need extra files to do it...( outside the cv itself, after it gets compiled to a lib)
I guess I'm bound to use the designers ability to do inline css to each node
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Try this. No guarantees though.

B4X:
Dim PJO As JavaObject = mBase
  
    Do While Not(PJO Is AnchorPane)
        PJO = PJO.RunMethod("getParent",Null)
        Log(PJO)
    Loop
    Log(PJO.RunMethodJO("getScene",Null))
    PJO.RunMethodJO("getScene",Null).RunMethodJO("getStylesheets",Null).RunMethod("add",Array(File.GetUri(File.DirAssets,"cmenu.css")))

You should also probably check that it's not already added.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
This is a better implementation :
B4X:
Dim PJO As JavaObject = mBase

    Dim StyleSheetName As String = "cmenu.css"

    Do While Not(PJO Is AnchorPane)
        PJO = PJO.RunMethod("getParent",Null)
     Loop

    Dim StyleSheets As JavaObject = PJO.RunMethod("getStylesheets",Null)
  
    Dim Iterator As JavaObject = StyleSheets.RunMethod("iterator",Null)
    Dim S As String
    Dim Found As Boolean
    Do While Iterator.RunMethod("hasNext",Null)
        S = Iterator.RunMethod("next",Null)
        If S.Contains(StyleSheetName) Then
            Found = True
            Exit
        End If
    Loop
     If Not(Found) Then PJO.RunMethodJO("getStylesheets",Null).RunMethod("add",Array(File.GetUri(File.DirAssets,StyleSheetName)))
  
    Log(PJO.RunMethod("getStylesheets",Null))
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
NICE!!!
 
Upvote 0
Top