Can anyone give me a hand with Android's WebView?
I'm trying to hide the top banner of a webpage in an Android WebView, but it's doing my head in! I've been at it for days and still can't get it to work.
I have used the code below, to try to disable the displaying of a certain element, the page loads (including the element I don't want) but then disappears completely and shows the word "none".
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
webView.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView webView, String url) {
webView.loadUrl("javascript:document.getElementsByClassName('welcome')[0].style.display='none';");
}
}
This is the onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView mAdView;
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
this.webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
// To keep browser in width of phone
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
// Sets default zoom
webView.getSettings().setTextZoom(250);
//webSettings.setTextZoom(125);
MyWebViewClient webViewClient = new MyWebViewClient();
webView.setWebViewClient(webViewClient);
webView.loadUrl("http://www.example.com");
}
Any class I choose (not just welcome) hides the entire website and displays the word 'none'. And if I change it to try to change height to 0px, it displays '0px' to the screen.
It's not a problem with the specific website I'm using, it does the same for my personal simple HTML / CSS website. Which makes me think either my JavaScript is wrong or there's something in WebView stopping me.
Join the Discussion