Mirnal Mannu
Mirnal Mannu

Reputation: 93

How to show a part of the website in the WebView in Android

I am new to Android Studio, I want to show a part of the website in my android application WebView (not the complete site). I tried to do it using jsoup library but no success. Currently I am trying to achieve this using the following code, but still no success. Please help me what to do for the same.

webView1=(WebView)findViewById(R.id.webView1);
     webView1.getSettings().setJavaScriptEnabled(true);
    webView1.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url)
        {
            url="http://https://mannudestiny.wordpress.com/";
            webView1.loadUrl("javascript:(function() { " + "document.getElementsByClassName('site-content').style.display='none'; " + "})()");
            webView1.loadUrl(url);
        }
    });
    //webView1.loadUrl("www.google.com");

Upvotes: 2

Views: 2668

Answers (2)

Roga Men
Roga Men

Reputation: 520

I also tried each and every answer on stack overflow to achieve the answer but none of them worked for me.

I got the solution to add this:

view.getSettings().setJavaScriptEnabled(true);
        view.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url)
            {
                view.loadUrl("javascript:(function() { " +
                        "var head = document.getElementsByClassName('header')[0].style.display='none'; " +
                        "var head = document.getElementsByClassName('blog-sidebar')[0].style.display='none'; " +
                        "var head = document.getElementsByClassName('footer-container')[0].style.display='none'; " +
                        "})()");

            }
        });
        view.loadUrl("your url");

Adding (var head =) looks like to hide my class in webview.

I hope this will be helpful for someone.

Upvotes: 1

Mirnal Mannu
Mirnal Mannu

Reputation: 93

I tried each and every answers on stack overflow to achieve the answer for my question, but none of them worked for me, there were slight changes which needed to be done in the code to achieve this. Today I got the answer to my own question.

To show some parts of the website we have to change the code as:

webView1.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url)
        {
            webView1.loadUrl("javascript:(function() { " +
                    "document.getElementById('footer').style.display='none'; " +
                    "document.getElementsByClassName('logo')[0].style.display='none'; " +
                    "})()");

        }
    });
    webView1.loadUrl("your url");

when you are using getElementById then remove the [0] and when using getElementsByClassName then use [0] after the id or class name respectively. Also if you want to show the website part then use style.display='block' and if you want to remove the part then use style.display='none' , hope that my answer and explanation is helpful.

Upvotes: 2

Related Questions