9

I have an Android app, which contains a WebView, and I would like to display in it not a webpage, but only a div from that webpage. I should mention that I do not have access to that page.

1
  • Are there any answers that don't include, "use this library"? It seems such overkill to import an entire library to do one little bit of trimming. Commented Dec 1, 2015 at 18:02

2 Answers 2

14

I would recommend Jsoup. It's larger than tagsoup but provides inbuilt functionality to pull the HTML from the URL and is super easy to use. For example if you want to pull a div with id example you would do the following:

Document doc = Jsoup.connect(url).get(); Elements ele = doc.select("div#example"); 

Then to load the HTML you've extracted into your web view you would do:

String html = ele.toString(); String mime = "text/html"; String encoding = "utf-8"; webView.loadData(html, mime, encoding); 
Sign up to request clarification or add additional context in comments.

4 Comments

this works, i get the html, but i get webpage not available in the webview
I can't really give you much without more info. If you're getting the HTML then it's probably a valid URL. Are you sure you're doing loadData() and not loadUrl(), and have you tried outputting the HTML to see what it is?
I managed to get it working after using loadDataWithBaseURL. Thanks!
How you can preserve style ?
2

You'll need to load the HTML of the page yourself, extract the div contents and pass it to the WebView as a string. You may find an HTML parser library (e.g. tagsoup) useful for this.

1 Comment

Thank you! could you give some example on this approach?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.