Android Question WebView calls windows.print()

qey

Member
In my application, we open a webview page to display document. Inside the webview have a print button which will call javascript the windows.print() funtion. Currently its only working in browser but not in a webview. Any advice?
 

MicroDrie

Well-Known Member
Licensed User
You need to add the WebviewExtras library to make the conncetion between Weview content and B4A
WebView call javascript:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim webview As WebView
    Dim wex As WebViewExtras
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    webview.Initialize("wv")
    
    wex.addJavascriptInterface(webview, "B4A")
    wex.addWebChromeClient(webview, "wex")
    
    
    Activity.AddView(webview, 0%x,0%y,100%x,100%y)
    webview.LoadHtml(File.ReadString(File.DirAssets,"color.html"))
    
    Sleep(10000)   ' to give you time to look at your device
'    wex.executeJavascript( webview, "document.getElementById('omo').style.color='green';")
    wex.executeJavascript( webview, "document.getElementById('colorbutton').click();")
End Sub
HTML page:
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
   function changecolor() {
      var e = document.getElementById('omo');
      if (e.style.color === 'red')
         e.style.color = 'blue';
     else
         e.style.color = 'red';
   }
</script>
</head>
<body>
</body>
<div id='omo' style='font-Size:xx-large; color:red; text-align:center;width:100%;height:100px;'>OMO</div>
<input type='button' id='colorbutton' value=' change color ' onclick='changecolor();'>
</html>
 
Upvote 0
Top