Skip to content Skip to sidebar Skip to footer

Get Html Element From My Own Web Page From A Webview?

I have a webview displaying a page from my own server. Is it possible to add a javascript method that would allow my android app to read out a page element from the webview? Someth

Solution 1:

Ok this is how i did it:

<html><head><scripttype="text/javascript">functiononBodyLoad() {
        var element = document.getElementById("abc");
        window.javascriptAccessor.getYerData(element.innerHTML); 
      }

    </script></head><bodyonload="onBodyLoad()"><divid="abc">data</p></body></html>

Then my activity:

WebView webview = (WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(newJavascriptAccessor(), "javascriptAccessor");
webview.setWebViewClient(newWebViewClient() {}); // wouldn't work without this!
webview.loadUrl(url);

privateclassJavascriptAccessor {
    @SuppressWarnings("unused")
    publicvoidgetYerData(String data) {
        Log.v(TAG, data);
    }
}

Yeah!

Post a Comment for "Get Html Element From My Own Web Page From A Webview?"