Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Getting the plain text content from a webview is rather hard. Basically, the android classes don't offer it, but <em>javascript does</em>, and Android offers a way for javascript to pass the information back to your code. </p> <p>Before I go into the details, do note that if your html structure is simple, <em>you might be better off just parsing the data manually</em>.</p> <p>That said, here is what you do:</p> <ol> <li><a href="http://developer.android.com/reference/android/webkit/WebSettings.html#setJavaScriptEnabled%28boolean%29" rel="noreferrer">Enable javascript</a></li> <li><a href="http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29" rel="noreferrer">Add your own javascript interface class</a>, to allow the javascript to communicate with your Android code </li> <li>Register your own <a href="http://developer.android.com/reference/android/webkit/WebView.html#setWebViewClient%28android.webkit.WebViewClient%29" rel="noreferrer">webviewClient</a>, overriding the <a href="http://developer.android.com/reference/android/webkit/WebViewClient.html#onPageFinished%28android.webkit.WebView,%20java.lang.String%29" rel="noreferrer">onPageFinished</a> to insert a bit of javascript</li> <li>In the javascript, acquire the <a href="http://javascript.gakaa.com/element-innertext.aspx" rel="noreferrer">element.innerText</a> of the tag, and pass it to your javascript interface.</li> </ol> <p>To clarify, I'll post a working (but very rough) code example below. It displays a webview on the top, and a textview with the text-based contents on the bottom.</p> <pre><code>package test.android.webview; import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.TextView; public class WebviewTest2Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); WebView webView = (WebView) findViewById(R.id.webView); TextView contentView = (TextView) findViewById(R.id.contentView); /* An instance of this class will be registered as a JavaScript interface */ class MyJavaScriptInterface { private TextView contentView; public MyJavaScriptInterface(TextView aContentView) { contentView = aContentView; } @SuppressWarnings("unused") public void processContent(String aContent) { final String content = aContent; contentView.post(new Runnable() { public void run() { contentView.setText(content); } }); } } webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new MyJavaScriptInterface(contentView), "INTERFACE"); webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { view.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText);"); } }); webView.loadUrl("http://shinyhammer.blogspot.com"); } } </code></pre> <p>Using the following main.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="0.5" /&gt; &lt;TextView android:id="@+id/contentView" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="0.5" /&gt; &lt;/LinearLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload