Home » Java » Android Webview: Display only the content of the website

Android Webview: Display only the content of the website

Posted by: admin September 26, 2018 Leave a comment

Questions:

I want to hide header and footer of the website but its not working Please Help

I wanted to know if it was possible to display only certain parts of a website in a WebView.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new myWebClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("https://myopenhab.org/account");

    public class myWebClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {

            view.loadUrl("javascript:(function() { " +
                    "var head = document.getElementId('mainHeader').style.display='none'; " +
                    "})()");
        }
    }


    @Override
    // This method is used to detect back button
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            // Let the system handle the back button
            super.onBackPressed();
        }
    }
}
Answers: