user7129054 user7129054 - 1 year ago 107
Android Question

How to display a part of the webpage on the webview android

I am trying to extract small portion from webpage and load into webview
I have tried following solution given in the link,But it did not work

Display a part of the webpage on the webview android

#Extracting data using getElementsByClass("darewod");

htmlDocument = Jsoup.connect(htmlPageUrl).get();
element = htmlDocument.getElementsByClass("darewod");

String html = element.toString();
String mime = "text/html";
String encoding = "utf-8";

I have tried the following two methods to load to webview but it seems not working,Its just printing HTML on UI

wv1.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);

wv1.loadData(html, "text/html", null);

Can you please tell me if i am missing anything here?

Answer Source

Your are loading your html code without the proper structure (so all definitions in head are lost, like CSS references) and without the initial document (or loading with base url) all relative paths are broken.

<div class="darewod"> <a title="Workout of the Day" href="/workouts/lower-abs-workout.html" rel="alternate"><img src="/images/grid/wod/2016/wod_nov8.jpg" alt="Workout of the Day"></a> </div>

What you could do: replace the body of your document with your selected element, therby preserving the structure and information regarding base:

Example Code

WebView wv;
Handler uiHandler = new Handler();

protected void onCreate(Bundle savedInstanceState) {

    wv = (WebView)findViewById(;
    wv.setWebViewClient(new MyWebViewClient());

    new BackgroundWorker().execute();


// load links in WebView instead of default browser
private class MyWebViewClient extends WebViewClient {

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;

    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return false;

private class BackgroundWorker extends AsyncTask<Void, Void, Void> {

    protected Void doInBackground(Void... arg0) {
        return null;

    public void getDarewod(){

        try {
            Document htmlDocument = Jsoup.connect("").get();
            Element element ="#gkHeaderMod > div.darewod").first();

            // replace body with selected element
            final String html = htmlDocument.toString();

   Runnable() {
                public void run() {
                    wv.loadData(html, "text/html", "UTF-8");
        } catch (IOException e) {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download