Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView hide div

I want to partially view a webpage on webview android and remove some div element from the webpage. I have a webpage like this

    <!DOCTYPE html>
<head></head>
<body>
<div id="container">
<div id="header"></div>
<div id="main"></div>
</div>
</body></html>

i tried like but all i see on page is title "none" or page loaded but show only white background Do anyone have any ideas?

view.loadUrl("javascript:document.getElementById('header').style.display = 'none';")

it's my java,

    package com.example.client6.tetrapolis;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class lichkab extends Activity {

public class HelloWebViewClient 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:var con = document.getElementById('header');" +"con.style.display = 'none'; ");
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lichkab);
//
mWebView = (WebView) findViewById(R.id.webview);
//
mWebView.getSettings().setJavaScriptEnabled(true);
//
mWebView.loadUrl("#");
mWebView.getSettings().setLoadsImagesAutomatically(false);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.setWebViewClient(new HelloWebViewClient());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.lichkab, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
WebView mWebView;
}
like image 691
Bank Tetrapolis Avatar asked Feb 12 '23 17:02

Bank Tetrapolis


1 Answers

I've got the same issue, so I changed JavaScript to look like this:

view.loadUrl("javascript:document.getElementById(\"header\").setAttribute(\"style\",\"display:none;\");");

Hope it helps. Bye!

like image 194
user4313674 Avatar answered Feb 15 '23 12:02

user4313674