B4J Question Inserting CSS into page

Discussion in 'B4J Questions' started by TomDuncan, Jul 7, 2015.

  1. TomDuncan

    TomDuncan Active Member Licensed 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..
    Code:
    <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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Dim s As String = $"
    <style>#main {
     ${
    write any code you like 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>"$
     
  3. TomDuncan

    TomDuncan Active Member Licensed 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
     
  4. sonicmayne

    sonicmayne Member Licensed User

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

    Code:
    Sub ExecuteScript(WebView As WebView, Script As StringAs 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
     
  5. FrenchDeveloper

    FrenchDeveloper Member Licensed User

    I get an error "java.lang.RuntimeException: Method: getEngine not found in: android.webkit.WebView" at this line :
    Code:
    Dim engine As JavaObject = jo.RunMethodJO("getEngine",Null)
    Thank you in advance for any help
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    This is B4J code, not B4A.
     
Loading...