title: Overriding window.print() in webview
body: <p>Since, webview does not support javascript function <code>window.print()</code>, I am trying to override it as is mentioned <a href="https://bugs.chromium.org/p/chromium/issues/detail?id=322303" rel="nofollow noreferrer">here</a>.
  In short, solution mentioned there is:
  "Inject a JavaScript to override window.print() and call JavaScriptInterface method"</p>
  
  <p>Therefore, I did following:
  <br/>
         1) Added following method call to the place where I create and initialize the webview:</p>
  
  <pre><code>tab.getWebView().loadUrl("javascript:window.print = function() {console.log('Gonna do some special "
                      + "stuff!!');"
                      + "alert(\"Printing\"); customPrint.print();}");</code></pre>
  
  <p>2) Created a web app interface </p>
  
  <pre><code>public class WebAppInterface
          {
              @JavascriptInterface
              public void print()
              {
                  Toast.makeText(activityInstance, "print called", Toast.LENGTH_SHORT).show(); 
              }
          }
  </code></pre>
  
  <p><br/></p>
  
  <p>3) Added above created interface to the code where webview is initialized:</p>
  
  <pre><code>mWebView.addJavascriptInterface(new WebAppInterface(), "customPrint");</code></pre>
  
  <p>This works fine when I have a webpage which does not have window.print() in same window ie. some html whose onClick() is like this:</p>
  
  <pre><code> function myFunction() { window.print(); }</code></pre>
  
  <p>But fails to work when myFunction() is replaced with something like this: </p>
  
  <pre><code>var sUrl="www.google.com";
      var ctrlString="Hello world";
      var wind = window.open(sUrl,"PrintWindow",ctrlString);
      wind.document.write("&lt;html&gt;&lt;head&gt;&lt;meta name=\"viewport\" 
      content=\"width=device-width\" /&gt;&lt;style&gt;html, body, p {font-size: 20px; 
      width: 100%; display: block; margin: 2px; padding: 1px; }&lt;/style&gt; . 
      &lt;/head&gt;&lt;body&gt;&lt;p&gt;Hello world.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;");
      var tempTitle = document.title;
      wind.document.title = sUrl;     
      wind.print();
      wind.document.title = tempTitle;
      wind.close();
  </code></pre>
  
  <p>This is happening because we are opening new window when print is clicked on webpage.In my case, we open a new webview when new window is opened which is initialized again. Although I think adding web interface when new webview is created should work, it is not working in my case. Need to figure out what could be wrong in this case?</p>