NotRealityItself
NotRealityItself

Reputation: 1

How can I display only a part of a website in an Android WebView

How can I display only a part of a website in an Android WebView?

The HTML-Code of this website contains a Head-Block which should be displayed and a Body-Block which mainly exists of a table styled like this for each table row:

<table class="VBlock">
<tr class="normal">
<th class="VBlock">Class</th>
<th class="VBlock">Lesson</th>
<th class="VBlock">Subject</th>
<th class="VBlock">represented by</th>
<th class="VBlock">Subject</th>
<th class="VBlock">Room</th>
<th class="VBlock">Notes</th>
</tr>

...

</table>

Well, in the WebView only specific table rows should be displayed. This should be dependent on the content of the table column (NOTE: the words in the columns are not final, just imaginary)

 <th class="VBlock">Class</th>

by comparing to an ArrayList.

I have just written the following code which I partly found searching the web:

public void getWData(){
    final WebView webView1 = (WebView) findViewById(R.id.webView1);
    webView1.getSettings().setJavaScriptEnabled(true);
    webView1.getSettings().setBuiltInZoomControls(true);
    webView1.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            webView1.loadUrl("");
        }

    });
    webView1.loadUrl(url);

I think I have to write something in the params of the " webView1.loadUrl("") " in the onPageFinished-void but I am not sure.

Thanks in advance for your help

Upvotes: 0

Views: 350

Answers (2)

Faruk Yazici
Faruk Yazici

Reputation: 2404

Assuming you have an ArrayList that has some values in it;

List<String> list;

You have to generate a complex Javascript function and run it here. First, you have to convert your ArrayList to a Javascript list in string format as follows:

String listStr = "[";
for(int i=0; i<list.size(); i++){
    listStr += list.get(i);
    if(i!=list.size()-1)
        listStr += ",";
}
listStr += "]";

By the end of this code part, you have the list in string format ready to use in Javascript. Now you have to modify the loadUrl in onPageFinished as follows:

String func = "javascript:(function() { " +
    "var arrayList = " + listStr + ";" +
    "var tableRows = document.getElementsByTagName('th');"
    "for(var j=0; j<tableRows.length; j++){"+
    "tableRows[j].style.visibility = 'hidden';"+
    "};"+
    "for(var i=0; i<arrayList.length; i++){"+
        "for(var j=0; j<tableRows.length; j++){"+
            "if(tableRows[j].innerHTML != arrayList[i]){"+
                "tableRows[j].style.visibility = 'visible';"+
            "}"+
        "}"+
    "}"+
"})()";

Then, you can run this Javascript function as follows:

webView1.loadUrl(func);

Basically, this javascipt function hides all the table rows at first. Then iterates over your array, and makes the matching ones visible.

Upvotes: 1

Ye Min Htut
Ye Min Htut

Reputation: 2904

You might want to use Jsoup. As you are not providing full html code, I can't show you the example.

I can only show you the door. You're the one that has to walk through it. :)

Hope it helps.

Upvotes: 0

Related Questions