Displaying a part of a webpage in webview

Please note that this question is not a duplicate of How to display some part of webpage in android webview? or Android WebView: display only some part of website as they exclude some element whereas i want to include only one.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    boolean validate=checkIfNet();
    if(!validate){
        finish();
    }
    setContentView(R.layout.activity_main);
    WebView wb = (WebView) findViewById(R.id.webview);
    wb.getSettings().setJavaScriptEnabled(true);
    wb.getSettings().setLoadWithOverviewMode(true);
    wb.getSettings().setUseWideViewPort(true);
    wb.getSettings().setJavaScriptEnabled(true);
    wb.getSettings().setBuiltInZoomControls(false);
    wb.getSettings().setPluginState(WebSettings.PluginState.ON);
    //wb.getSettings().setPluginsEnabled(true);
    //wb.setWebViewClient(new HelloWebViewClient());
    wb.loadUrl("http://www.dota2.com/leaderboards#europe");
}

private boolean checkIfNet() {
    boolean connected = false;
    ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
        //we are connected to a network
        connected = true;
    }
    else
        connected = false;
    return connected;
}

}

Now while getting http://www.dota2.com/leaderboards#europe it gets the full page. I only want the table in there. The one with tbody id="leaderboard_body". I am kind of new so pls chill on me. Thanks in advance.

1 answer

  • answered 2017-06-17 19:13 Thiago Valle

    Actually what you want to achieve technically is to remove some parts of the html.

    What happens is that your webview will get all of the content of the URL you provided to it, this is inherent on how http works. The webview class doesn't let you choose to show only a specific part of the html, so what you must do is to remove what you don't want before rendering using javascript.

    If you want to show it using a webview then the solutions in your post are what you want, if you want to parse the html and render it using Android UI classes then the jsoup solutions are what you want(however this is a bit overkill).

    If you're really new to this my tip is: use an API. Probably something like https://docs.opendota.com/. The way virtually all apps(and websites) that are not static works is by using APIs. Here is an introduction on why you would want to do so instead of parsing with jsoup.