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
Related
I have been able to successfully remove a part of a webpage using the below code, but the only problem is that the webview first display the complete webpage and then it removes the element 'header-text-nav-container' although I am calling it onPageFinished(). I tried many different ways but all in vain. How to display the webpage in the webview only after successfully removing the element 'some-part' in my case 'header-text-nav-container'. Please help
public class myWebClient extends WebViewClient
{
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:(function() { " +
"document.getElementById('header-text-nav-container').style.display='none'; " +
"})()");
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
}
I finally solved the problem by showing a progress dialog and then dismissing the progress dialog when the 'header' is successfully removed. Thus I solved the problem partially.
I'm trying to access my Java method helloWorld(); using JavaScriptInterface in Android, from the page I'm viewing using WebView
I'm a little new to this, and I don't know why I'm not getting the value to my html page
This is the Android code I'm using:
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.webview01);
JavaScriptInterface jsInterface = new JavaScriptInterface(this);
web.getSettings().setJavaScriptEnabled(true);
web.addJavascriptInterface(jsInterface, "JSInterface");
web.getSettings().setUseWideViewPort(true);
web.getSettings().setLoadWithOverviewMode(true);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://exampleweb:10110/exWeb/deposits.jsp");
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
public class JavaScriptInterface {
private Activity activity;
public JavaScriptInterface(Activity activity) {
this.activity = activity;
}
#JavascriptInterface
public String helloWorld(){
return("hello world");
}
}
}
This is the JavaScript code I've been using (on the page I'm viewing with WebView)
HTML button
<td> <button type="button" class="btn btn-xs btn-primary connectToAndroid">Test android</button></td>
JavaScript
$(".connectToAndroid").on('click', function () {
var val = window.JSInterface.helloWorld();
alert(val);
});
Does anyone know what I'm doing wrong here? I just want to alert an "Hello World" from android in my HTML page.
Any pointers are greatly appreciated!
Try by adding this line of code before loading the url and make sure you reference your jQuery library in the html.
web.setWebChromeClient(new WebChromeClient(this));
This is very weird, I tried using this a couple of hours ago:
$(".connectToAndroid").on('click', function () {
var val = Android.helloWorld();
alert(val);
});
Then I changed it, it didn't say Android anymore,
yet I kept getting an error saying:
Uncaught ReferenceError: Android is not defined
And after a while I figured, Android.helloWorld(); must be cached somehow in my webview app or something.
So, I changed back to this:
$(".connectToAndroid").on('click', function () {
var val = Android.helloWorld();
alert(val);
});
And added this:
web.addJavascriptInterface(jsInterface, "Android");
And now it works like a charm, as it should have from the beginning if it wasn't for some cache somewhere,
Can anyone explain why this happens? Does android webview actually cache javascript code? And can I prevent this behaviour in the future somehow? Cache bothers a lot when coding/testing new stuff.
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 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.
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.