I am trying to display a webpage using webView. I use loadDataWithBaseUrl to display the webpage.
webView = (WebView) findViewById(R.id.webview);
WebSettings settings=webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
(activity).setTitle("Loading...");
(activity).setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
}
});
String s= "file://"+f.getAbsolutePath()+"/";
webView.loadDataWithBaseURL(BASE_URL,HTMLData, "text/html", "utf-8","");
The problem I am facing is that the javascript on the page which should have been called on onLoad is not being run. The browser displays the webpage correctly. Also, the loadUrl method gives the correct results. Please suggest a possible solution.
Edit: I got it sorted out. The javascript file had an error. Please ignore.
Related
When creating an application i noticed it is not recognized as the variable navigator.standalone is not set. I first thought i would fix this bij setting it mannually when injecting javascript as below:
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
webView.evaluateJavascript("navigator.standalone = true;", new ValueCallback<String>() {
#Override
public void onReceiveValue(String s) {
Log.e(TAG + "jsinject", s);
}
});
}
});
webView.addJavascriptInterface(new WebAppInterface(this, webView), "Android");
However this injects to late and some javascript functions wont fire because it is not recognized as a standalone application. Is there a possibility to set the var before the page finished loading?
I replaces the function onPageFinished(WebView view, String url) with onPageStarted(WebView view, String url, Bitmap favicon) this injects the navigator var before the page is fully loaded
I'm trying to remove this part of a webpage in side my WebView
<header class="page-header" data-type="fixed" role="banner"
This is what I tried so far
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceStatus){
View view = inflater.inflate(R.layout.fwebview, container, false);
webView = (WebView) view.findViewById(R.id.webView);
String url = getArguments().getString("link");
final String js = "javascript:"
+ "function () {"
+ " var element = document.getElementsByClassName('page-header');"
+ " element[0].parentNode.remove(element[0]);"
+ "} ();";
// Enable Javascript
webView.getSettings().setJavaScriptEnabled(true);
//set the WebViewClient before calling loadUrl
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
webView.loadUrl(js);
}
});
webView.loadUrl(url);
return view;
}
Somehow this doesn't work. I tried various methods to achieve what I want but somehow it won't remove the header. Can anyone help me fix this problem? It's really anoying
you need also WebChromeClient set for your WebView
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient()); //after setJavaScriptEnabled
also enabling DOM storage may be helpful
webView.getSettings().setDomStorageEnabled(true);
I'd like to set top padding in a html source using JS function.The point is to not reload the page when this function will take effect, that's why I supposed to use innerHTML property.
My actual source is:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView web, String url) {
web.loadUrl("javascript:(function(){document.body.innerHTML = document.body.innerHTML.style.paddingTop = 100px");
}
});
this solution is not working, giving me warning:
I/chromium: [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
You need to write
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView web, String url) {
web.loadUrl("javascript:(function(){ document.body.style.paddingTop = '100px'})();");
}
});
I have a WebView in which i display web content which i have no control over. The content displays fine, but have links which spawn a popup window. The javascript function that does that looks like this:
function getEntry(id) {
var win = window.open('', 'Booking',
'resizable=yes,scrollbars=yes,status=no,width=500,height=400');
win.document.location = '/some/url/1-' + id ;
}
I can't easily change this, and if the people responsible for the page i download would change it, i guess my app would fail miserably...
My WebView setup in the activity looks like this:
_webview = new WebView(this);
setContentView(_webview);
final Activity activity = this;
_chromeClient = new MyChromeClient();
_webview.setWebChromeClient(_chromeClient);
//I experimented with changing user agent, in case that would have any effect, it didn't...
_webview.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
_webview.setWebViewClient(new MyWebViewClient());
_webview.getSettings().setJavaScriptEnabled(true);
_webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
_webview.getSettings().setSupportZoom(true);
_webview.getSettings().setBuiltInZoomControls(true);
_webview.getSettings().setDomStorageEnabled(true);
//Cache settings...
_webview.getSettings().setAppCacheMaxSize(1024*1024*8);
_webview.getSettings().setAppCachePath("/data/data/com.your.package.appname/cache");
_webview.getSettings().setAllowFileAccess(true);
_webview.getSettings().setAppCacheEnabled(true);
MyWebClient:
private class MyWebViewClient extends WebViewClient {
#Override
public void onLoadResource(WebView view, String url) {
Log.d("MyWebViewClient",url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
showProgressDiag();
Log.d("MyWebViewClient","shouldOverride... : " + url);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url){
hideProgressDiag();
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if(failingUrl.equals("file:///android_asset/html/error.html")){
hideProgressDiag();
Toast.makeText(_context, "Error! Check internet connection, or try again later...", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(_context, failingUrl, Toast.LENGTH_SHORT).show();
view.loadUrl("file:///android_asset/html/error.html");
}
}
}
MyChromeClient:
private class MyChromeClient extends WebChromeClient{
#Override
public void onProgressChanged(WebView view, int progress) {
Pdiag.setProgress(progress * 100);
}
}
When clicking one of the links that points to the javascript function all that happens is that the WebView turns grey, without going through shouldOverrideUrlLoading(). When i hit the back key the app exits, meaning that nothing was placed in the nav history of the WebView. Sometimes nothing happens, but then the shouldOverrideUrlLoading() do run and from a Log.d() i can see that the correct URL for the popup has been given to the WebView.
The thing is, on very rare occasions it shows up fine, but i have no clue how to reproduce it, and wether it actually shows in a popup.
I'm lost... And quite frustrated... Thinking of watching a bad sitcom instead :(
EDIT:
Actually, maybe the URL wasn't all that correct after all... In Firefox the URL ends with "X<<<<" but in my Log.d() output it ends with "X%3C%3C%3C%3C"... I'll investigate if i could change that...
EDIT 2:
Nope, didn't do anything... The URL is identical to the one in Firefox...
First of all, you need to set the following settings on your WebView:
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
Then you need to attach a WebChromeClient that overrides onCreateWindow. Your implementation of this method can create a new web view, and display it inside a dialog:
webView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
WebView newWebView = new WebView(MyActivity.this);
WebSettings webSettings = newWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Other configuration comes here, such as setting the WebViewClient
final Dialog dialog = new Dialog(MyActivity.this);
dialog.setContentView(newWebView);
dialog.show();
newWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onCloseWindow(WebView window) {
dialog.dismiss();
}
});
((WebView.WebViewTransport)resultMsg.obj).setWebView(newWebView);
resultMsg.sendToTarget();
return true;
}
});
Don't forget to set the new web view to the resultMsg, send it to its target and return true, as mentioned in the API documentation.
Please check with adding this -
getSettings().setSupportMultipleWindows(true);
I would like to know if there is a way to detect if the page is loaded completely in a webview - and if not loaded completely, automatically reload it again until the page is loaded entirely.
Is there a way to detect it ?
Thank you.
there is no definitive way to tell if the page will not finish loading. Still you can achieve similar kind of functionality by implementing WebViewClient.Timer t= new Timer("pageloadtimer", true);
webView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url) {
t.cancel();
}
}
public void loadURL(String url, long timeout){
t.schedule(new TimerTask(){
public void run(){
webview.reload();
}
}, timeout);
webview.load(url);
}
prateek i think this will work for u.......
in your webview set webview client as follows....
wv.setWebViewClient(new WebViewClient(){
});
and use webViewclients different callback methods to detect page loade or not or did u recieve some error while loading etc......
some of its methods are
public void onPageStarted(WebView view, String url,Bitmap favicon) {}
public void onPageFinished(WebView view, String url) {}
public void onLoadResource(WebView view, String url) {}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {};
look for these methods.....
Thanks.