ShawnWang
ShawnWang

Reputation: 68

How to get the webview's content which was clicked on the webview

I want to add the bookmark function to my app, when I clicked on the webview which display the HTML files (The file's main part is string), I want to capture the first line of the content which display on the screen. Anyone knows the answer?

Thanks very much in advance.

-Shawn

Upvotes: 0

Views: 714

Answers (2)

Deepak Goel
Deepak Goel

Reputation: 5684

you can identify the some content of the page as It contains img tag or not Use this API of webView click here

 WebView.HitTestResult hr = ((WebView)v).getHitTestResult();
 int i=hr.getType() ;

and use the int values of this class for the content

Hope it help

Upvotes: 1

ol_v_er
ol_v_er

Reputation: 27294

If there is a link into the WebView and you want to do a specific action when the user click on this link you must catch the link click using the following code:

Somewhere in your activity code (commonly in the onCreate method):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState, R.layout.news_details);
    ...
    mWebView.setWebViewClient(new MyWebViewClient());
    ...
}

And the WebViewClient class:

class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (DEBUG) {
            Log.d(TAG, "shouldOverrideUrlLoading url= " + url);
        }
        if ( the url is like you want) {
            // TODO: add the code to do what you need to do with the url
            // the webview should not do anything with this link.
            return true;
        } else {
            // let the webview normally handle the link
            return false;
        }
    }
}

If what you want to do is get the actual displayed content of the WebView, there is no API to do that.

Have a look on those post:

Is it possible to get the HTML code from WebView

Retrieve webview content

Both redirect on this website:

http://lexandera.com/2009/01/extracting-html-from-a-webview/

Upvotes: 0

Related Questions