android - get Text out of webview
Getting the plain text content from a webview is rather hard. Basically, the android classes don't offer it, but javascript does, and Android offers a way for javascript to pass the information back to your code.
Before I go into the details, do note that if your html structure is simple, you might be better off just parsing the data manually.
That said, here is what you do:
- Enable javascript
- Add your own javascript interface class, to allow the javascript to communicate with your Android code
- Register your own webviewClient, overriding the onPageFinished to insert a bit of javascript
- In the javascript, acquire the element.innerText of the tag, and pass it to your javascript interface.
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.
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");
}
}
Using the following main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.5" />
<TextView
android:id="@+id/contentView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.5" />
</LinearLayout>
Extract text from a Webview
Got it to work. Two small problems:
You're making a UI call in the function called by JavaScript. That's not allowed.
ReplaceMainActivity.count.setText(occ);
withrunOnUiThread(new Runnable() {
@Override
public void run() {
count.setText(String.valueOf(occ));
}
});- This code already fixes the 2nd issue: calling
setText(int)
expects a resource ID, thus you need to convert to a String first.
(You need to also remove static
from your declaration of count
)
Get visible text from webview in android
You should get innerText instead of innerHtml:
document.getElementsByTagName('html')[0].innerText
how to get text from a webview
Here is an example of sending text from your webpage to your android device. It creates a 'toast' popup message on the android.
Add this to your webview:
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
Add this class to your project:
public class JavaScriptInterface {
Context mContext;
JavaScriptInterface(Context c) {
mContext = c;
}
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
Send information from your website to your android with javascript like so:
<script type="text/javascript">
Android.showToast("This is a message");
</script>
Related Topics
How to Set a Particular Font for a Button Text in Android
Sqlite Android Database Cursor Window Allocation of 2048 Kb Failed
Download Files and Store Them Locally with Phonegap/Jquery Mobile Android and iOS Apps
Opening Android Settings Programmatically
How to Customize Item Background and Item Text Color Inside Navigationview
Programmatically Getting the Gateway and Subnet Mask Details
Programmatically Relaunch/Recreate an Activity
How to Simulate Touch from Background Service with Sendevent or Other Way
Using Resultreceiver in Android
Redraw a Single Row in a Listview
Show Icon in Actionbar/Toolbar with Appcompat-V7 21
How to Re-Sign an .Apk with a Different Certificate Than What It Came With
What Is the Class R in Android
How to Determine If One of My Activities Is in the Foreground
Toolbar Navigation Icon Never Set
Android Save View to Jpg or Png
How to Disable Displaying "Suggestions" on the Soft Keyboard