B4J Question Inserting CSS into page

TomDuncan

Active Member
Licensed User
Longtime User
Hi all,
Just adding some dynamic html into my app and I need to add some,
generated css scripts into my code.

Like this..
B4X:
<style>
#main {
    [put this data here]
    background-image: url(images/fresh_snow.png);
    background-position: left top;
    background-repeat: repeat;
    padding: 15px;
}
</style>
</head>
<body  id=main >
<div>
    <div id="maindiv">
       
     </div>
</div>

What I would like to do is place the code below [put data here],
when the user accesses the page.

Tom
 

TomDuncan

Active Member
Licensed User
Longtime User
Can I in code add the background into the index.html page though.
Much like you did with the dynamic demo, putting the buttons in.
Tom
 
Upvote 0

sonicmayne

Member
Licensed User
Longtime User
If you want to inject CSS into a web page then this should do the trick (requires JavaObject):

B4X:
Sub ExecuteScript(WebView As WebView, Script As String) As String
    Dim jo As JavaObject
    jo = WebView
    Dim engine As JavaObject = jo.RunMethodJO("getEngine",Null)
    Return engine.RunMethod("executeScript",Array As String(Script))
End Sub

Sub InjectCSS(WebView As WebView, CSSText As String)
        CSSText = CSSText.Replace("'",QUOTE)
        Dim lines() As String = Regex.Split(CRLF,CSSText)
        For i = 0 To lines.Length - 1
            lines(i) = lines(i).Trim & "\"
            Log(lines(i))
        Next
        CSSText = ""
        For i = 0 To lines.length - 1
            CSSText = CSSText & lines(i)
            If i < lines.Length - 1 Then CSSText = CSSText & CRLF
        Next
        CSSText = CSSText.SubString2(0,CSSText.LastIndexOf("\"))
        Log("var cssText = '" & CSSText & "'; var css = document.createElement('style'); css.type = 'text/css'; css.innerText = cssText; document.body.appendChild(css);")
        ExecuteScript(WebView,"var cssText = '" & CSSText & "'; var css = document.createElement('style'); css.type = 'text/css'; css.innerText = cssText; document.body.appendChild(css);")           
End Sub
 
Upvote 0

FrenchDeveloper

Member
Licensed User
Longtime User
I get an error "java.lang.RuntimeException: Method: getEngine not found in: android.webkit.WebView" at this line :
B4X:
    Dim engine As JavaObject = jo.RunMethodJO("getEngine",Null)

Thank you in advance for any help
 
Upvote 0
Top