I have a webview in my program.
I loaded a string into this webview that contains javascript function. When i loaded it in the emulator it's not working
i.e if I write a simple alert the webView won't display the alert.
I have enabled the javascript. But then also its not working.
what may be the reason?
Please help
Thankyou
Alerts will not normally work in a WebView. You will need to write the code to do that yourself. You can easily do that by implementing your own version of WebChromeClient. In otherwords:
class ChromeClient extends WebChromeClient {
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();
}
}
...
mWebView.setWebChromeClient(new ChromeClient());
Another thing I would recommend is implementing the onConsoleMessage as well. This way you can just use "console.log" in your JavaScript and have it directed to Toast or the Android Log.
Related
i have a hybrid app. I used web-view to make a android hybrid app. I also have a native menubar above the webview. My question is, How can i disable the native menu from my web application? Is it possible? If yes please share your thoughts on this
Check the answer of this question, as they provide snippet code to call Java method from javascript in Android, this will help you if you create a Java method that will disable your ImageButton from a Javascript
Call Java function from JavaScript over Android WebView
You will need to find the user-agent click here to see how to check useragent then u can write a simple javascript function on page load to disable your header
Try to set webviewclient to your webview and override onPageFinished like this. In my project I tried to hide google and facebook buttons on fitbit page.
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (!TextUtils.isEmpty(url) && url.contains("fitbit")) {
view.loadUrl("javascript: setTimeout(function () { $('.external-choices,.or').hide();} , 1000) ");//JS to hide the fitbit login from G+ and FB
}
}
}
I know you can take an instance of a webview and call addJavascriptInterface(new WebAppInterface(context), "nameofinterface") to allow javascript to send data to Android Java code. But how do I go the other way? I want to tell javascript to play a video at a specific point in time when I hit the back button on my android device. Any ideas?
webView.loadUrl("javascript:function()");
You can put it here:
#Override
public void onBackPressed() {
// ...
}
In my application , I have a WebView to load a web app.The login is done using a alert box which takes username and password. when i try to load the URL in android browser the alert box appears like this. But when I try to load the same URL in my WebView it does not show any alert box .
After adding the following code
mwebView.setWebChromeClient(new WebChromeClient(){
#Override
public boolean onJsAlert(WebView view, String url, String message, android.webkit.JsResult result) {
Log.d("JSALERT", message);
new AlertDialog.Builder(view.getContext()).setMessage(message).setCancelable(true).show();
result.confirm();
return true;
}
});
WebView shows simple alert box messages , but it does not show that custom alert box(the method is not getting fired).
In my app WebView , I wanted to show the login alert (which includes login fields and Buttons) .if any one knows the solution pls share with me
Thanks in advance
Well, what you see in Browser isn't actually an "alert box" -- it's an HTTP authentication dialog. In order to handle it, you need to override WebViewClient.onReceivedHttpAuthRequest, see docs.
You can check the relevant code in Android Browser here and here.
I have the following code in my activity:
browser = (WebView)findViewById(R.id.webkit);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setPluginsEnabled(true);
browser.getSettings().setDomStorageEnabled(true);
/*if (Build.VERSION.SDK_INT >= 16) {
browser.getSettings().setAllowFileAccessFromFileURLs(true);
browser.getSettings().setAllowUniversalAccessFromFileURLs(true);
}*/
// Loads html-string into webview
browser.loadUrl("file:///android_asset/index.html");
THe page loads fine, the css loads fine, the images all load fine. However I also have a local js file. WHich currently only contains an alert message alert("JSEnabled"); and yet the alert never appears. How can I go about diagnosing this?
I'm currently using the emulator to develop the project and have to support back to Gingerbread (2.3.3).
Incidently the same html / js works fine when I use the Browser app on the emulator (Pointing at a remotely served version of the same HTML / js)
I should also point out - all the files are in the assets folder in the project, and I've tried referencing the js as file:///android_assets/main.js and as main.js. I've even tried referencing the remote js file. Nothing works...
To work your javascript alert please add this to your webview
webview.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
I'm using the method shouldOverrideUrlLoading for an app that is personalised for multiple clients. Each client has it's own webpage that wants to be loaded into the webview. My problem is this: the app works perfect for client A and his webpage, but doesn't for client's B webpage (which isn't related with client A in any way). The difference, as I've tested, is that in client's B situation for the links that are accessed within the loaded content into the webview, shoulOverrideUrlLoading isn't working (NEVER gets called) and for client's A webpage works perfectly. Another thing is that client's B webpage doesn't work only on some Android versions, like 2.1 or 2.3.6 but it works fine on 2.3.3, 2.3.5, 4.0.2 or 4.0.3.
So this is kinda odd. If you happen to know anything, please help! Thanks!:)
EDIT: I noticed that shouldOverrideLoading isn't called when the webpage does NOT load the requested link through javascript and it works when javascript isn't used!!! but when I set webview.setJavaScriptEnabled(false) it works!!! I really need javascript to be enabled for my app cause the webpages usually use javascript for other things besides loading so I cannot disable it just because shouldOverrideUrlLoading doesn't get called!
EDIT 2: To be more exact:
This one works and shouldOverrideUrlLoading gets called:
<p onclick="location.href='linkHere'">
NewLink
<span class="icon-arrow"></span>
</p>
This one does NOT work and shouldOverrideUrlLoading does NOT get called:
<a class="link-inherit" href="linkHere">
NewLink
<span class="icon-arrow"></span>
</a>
Solution for shouldOverrideUrlLoading not called
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (url.contains("success")) {
Intent intent = new Intent(WebviewActivity.this, OrderConfirmActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
super.onPageStarted(view, url, favicon);
}
}