Add javascript into WebView - android

I am developing an application where in 1 part I want to add javascript into WebView.. But am not getting how to do it in an appropriate way.. Can anyone pls guide me into this?????
I am doing it like:
wb=(WebView)findViewById(R.id.webView1);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setPluginState(WebSettings.PluginState.ON);
wb.getSettings().setPluginsEnabled(true);
wb.loadUrl("javascript:<script " ></script> ");
wb.setWebViewClient(new HelloWebViewClient());
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}

It was very simple..also without using any javascriptInterface..In my code, Instead writing:
wb.loadUrl("javascript:<script> </script>");
use,
wb.loadDataWithBaseURL(null,"<script> </script>","text/html","utf-8",null);
and its working now :)

Look at these nice tutorials about how to implements javascript in webview in android..
I think its provides you all the information what you needed..
Android WebView
Android WebView, Javascript and CSS
EDIT: Further if your implemented code having any exception or not working then please post that code and exception then here we can help you..

Related

Prevent link to be opened in external browser with WebChromeClient

I've developed a small group of .html pages that are stored in a server.
My android app, using a webview with: setWebChromeClient loads these pages.
minSdkVersion 19
targetSdkVersion 24
The problem:
Everytime I need to load a new page, using a link in my .html page, the new page is opened in an external browser.
Url Overriding
I know about the shouldOverrideUrlLoading() method, when we're using the setWebViewClient().
But unfortunately I can't use the normal webview. I need to use the WebChromeClient() because of some feature that only work with this one. (Like the input file)
My doubt is...
How can I override my URL to force them to load inside of the webChromeClient?
I tried this but with no luck:
webView.setWebChromeClient(new WebChromeClient() {
// (...)
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
You can use following code to achieve this.
WebView web = (WebView)findViewById(R.id.web);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportMultipleWindows(true); // This forces ChromeClient enabled.
web.setWebChromeClient(new WebChromeClient(){
#Override
public void onReceivedTitle(WebView view, String title) {
getWindow().setTitle(title); //Set Activity tile to page title.
}
});
web.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
I was using an embedded mobile web application in an Android App making use of the WebChromeClient class and did not want to fiddle around with recompiling the APK.
While looking for the easiest header(); solution (since the mobile web application is in php) I found out that using:
header("Location: url.php", TRUE, 307);
was a quick fix solution without recompiling the Android app.
This allowed the user to re-direct within the app without calling the web browser externally from my app.
Here is the link to the answer by Arindam Nayak where I got the idea from.

WebView.loadDataWithBaseURL. Go exactly to #label

How to go EXACTLY to http://example.com/index.html#label if content to webview loading via WebView.loadDataWithBaseURL ?
By default webview go to root only. How go to label ?
WebView doesn't have such functionality from the box.
You can achieve this with the help of javascript, but this is a bit of ugly.
You should load your html page and then add javascript code to it.
After this you can load your webview and call javascript methods from current page.
Check this link for example how that can be done.
WebView jump to anchor using loadDataWithBaseUrl
Example goes here
webContents.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
String id = "895884";
webContents.loadUrl("javascript:scrollAnchor(" + id + ");");
}
});

Android WebView- PopUp background is Transparent

I have loaded a WebView in my android app:
webView = (WebView)view.findViewById(R.id.webView);
String url = UrlConstants.URL_CITY;
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
But every popUp that is of the website has a transparent background as shown:
I've tried many urls as well: One being this
Anyone help will be appreciated. Thanks
I had a similar issue when adding WebView at runtime using addView. Turns out you need to use the addView overload specifying LayoutParams with MATCH_PARENT. Failing to do so would break side menus and pop-up on some web pages. Really nasty bug if you ask me.

Keeping a webView contained in Android?

Hey everyone! Im pretty new to Android and I have a webView under 2 other widgets on the screen(Or w/e you call it). But when ever I navigate the webView, its changes to full screen and i cant see my other widgets. Ive looked all over Google but couldn't find anything, is there some way I can fix this?
Im using this code to navigate:
webView.loadUrl("http://www.example.com/");
Thank you.
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
when you do webView.loadUrl("www.example.com") or if there is a redirection when a hyperlink is clicked or something similar, the android device browser is started. I believe you are stuck with this. Try pasting the above code before you call the webView.loadUrl("www.example.com")
What we are essentially doing is asking the webview to load the url, instead of opening it in the device browser.

How to display Web site in Android WebView

I am new to Android WebView, in my application i need to display a web site conatins 3 web pages. In the first web page there will be a link to navigate to the second page and second to third page. I given the URL in the WebView, the first page is displayed perfectly, when i click the link it directly opens the browser application to display the second page. But i want to display the second page in the WebView itself. Please find my code below:
WebView forumView=(WebView)findViewById(R.id.forumView);
forumView.getSettings().setJavaScriptEnabled(true);
forumView.loadUrl("url");
As i said i am very new to WebView my code might be wrong, please help me to solve this problem.
Thanks in Advance,
Rajapandian
This piece of code will help you.
wbb = (WebView) findViewById(R.id.webView_tobe_loaded);
WebSettings wbset=wbb.getSettings();
wbset.setJavaScriptEnabled(true);
wbb.setWebViewClient(new MyWebViewClient());
String url="http://www.google.com";
System.out.println(getdeviceid());
wbb.getSettings().setJavaScriptEnabled(true);
wbb.loadUrl(url);
You'll have to intercept the clicks yourself if you don't want the default Android behavior.
You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient of your WebView using the setWebViewClient() method.
If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

Categories

Resources