Why Android WebChromeClient's onCreateWindow method never got called? - android

I'm building my app on webview service and trying to open new url in another window (neither in default system browser nor in same webview). I set up my own WebViewClient and WebChromeClient like below,
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
view.loadUrl(url);
return true;
}
}
public class MyWebChromeClient extends WebChromeClient {
#Override
public boolean onCreateWindow (WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
WebView childView = new WebView(view.getContext());
final WebSettings settings = childView.getSettings();
settings.setJavaScriptEnabled(true);
childView.setWebChromeClient(this);
childView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(childView);
resultMsg.sendToTarget();
Log.d(LOG_TAG, "onCreateWindow"); // never log
return true;
}
}
I also definitely had my webview setSupportMultipleWindows to true.
However, onCreateWindow never got called. Is there anything I missed?
Thanks in advance.

Did you tell the "parent" webview that multiple windows are allowed?
WebView parentWebview = (WebView) findViewById(R.id.parent_webview);
// ...
parentWebview.getSettings().setSupportMultipleWindows(true);
That's the most common mistake I've seen.

add this line to code to invoke onCreateWindow()
webView.getSettings().setSupportMultipleWindows(true);

Without the full code its hard to say why OnCreateWindow() isn't being called. In your activity OnCreate() method do you have code similar to the following?
// Get the main web viewer
mWebView = (WebView) findViewById(R.id.webViewer);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Set WebViewClient and WebChromClient
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.setWebChromeClient(new MyWebChromeClient());
Also, what code are you using in which you think the OnCreateWindow() should be called?

Related

How to remove or disable WebView header?

I'm trying to remove the webview header, I've tried a number of possible solutions but to no avail.
One of my attempts:
android:theme="#android:style/Theme.NoTitleBar"
I'm loading the webview using this code:
setContentView(R.layout.activity_websitenew1);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.loadUrl("http://www.domain.com");
myWebView .setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
What is happening in your case is that the URL is being handled by Android which is choosing the handler for you. This handler is the system's browser application. What you want is for control to stay within your app.
You can do this in code as follows:
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.domain.com");
See http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView,%20java.lang.String) for details.

Android webview navigate in app not open browser [duplicate]

I have a webview in my android app, but when someone navigates around the site, it opens in a new window, i want it to stay inside the webview.. is there a way to do this easily? Here is the code in my activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.shop);
WebView webview;
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.co.uk");
You'll have to create a WebViewClient:
public class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
And then set it to your WebView like this:
webview.setWebViewClient(new myWebViewClient());
Or you can just do that, without creating a new class:
myWebView.setWebViewClient(new WebViewClient());
It works for me.
webview.setWebViewClient(new WebViewClient());
If you want to customize then you should override shouldOverrideUrlLoading (WebView view, String url). but it's deprecated in API 24. You can use public boolean shouldOverrideUrlLoading (WebView view,WebResourceRequest request). actually both of them needs to return false.
Android WebView Example

surfing all website and all links opens in same webview

when I create webview application to view any website on same webview not browser it works fine but when I click on any link in that website it opens in browser ? how to load all internal links/pages/sections when user click on them at same webview ?
Java version:
You only need to set a WebViewClient for your WebView and then all links will be opened it the same WebView:
webView.setWebViewClient(new WebViewClient());
private class myCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//here load the url in your webview
webview.loadUrl(url);
return true;
}
And this for set in your webview
webView.setWebViewClient(new myCustomWebViewClient());
Kotlin version.
webView.webViewClient = WebViewClient()
You should add a webViewClient of your own.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
in the webViewClient you can override the shouldOverrideUrlLoading() method to be like:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
I hope you find it helpful.

webChromeClient opens link in browser

I have searched and read a lot of posts but can not figure out how to do it in my code.
I want to use geolocation in my app and need to view in webChromeClient in stead of webViewClient which I use for the html files now and the links does stay in the same view.
When I change this to webChromeClient, the html links, like <a href="http://url/file.php?q=123", are suddenly opening in the browser!
How can I prevent this?
myWebView = new WebView(this);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setGeolocationEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, android.webkit.GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false); }
});
myWebView.loadUrl("file:///android_asset/HTML/index.html");
setContentView(myWebView);
WebChromeClient doesn't contain the shouldOverrideUrlLoading method, the WebViewClient does. Remember the "WebView" can and does use both WebViewClient and WebChromeClient at the same time if specified. The WebViewClient adds methods not available to you with no client assigned (keeping navigation in the webview). The same with the WebChromeClient has specific methods it can use (get page title on load for example).
So you can build your code like this:
WebView web = (WebView)findViewById(R.id.web);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setGeolocationEnabled(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 able to get around this by setting a dummy WebViewClient in addition to the WebChromeClient. Not sure why, but when I take out this line the web page starts opening in the browser again.
mBrowser.setWebViewClient(new WebViewClient());
To open links in the browser you can use an intent in the shouldOverrideUrlLoading method to launch the URL in a browser versus using your webview to handle the link:
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("http://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
}
If you want to load in the webview use:
WebViewClient yourWebClient = new WebViewClient()
{
// Override page so it's load on my view only
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// This line we let me load only pages with an anchor tag
if ( url.contains("url") == true )
//Load new URL Don't override URL Link
return false;
// Return true to override url loading (In this case do nothing).
return true;
}
};

WebView clicks opens mobile browser

There is a WebView which loads mobile-optimized URL (webpage). But when I click on a link, it does not load inside of the WebView (inside of the app), but mobile browser opens.
How to prevent this?
I tried overloading URLs via shouldOverrideUrlLoading(), but it did not help.
This is a code.
webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setPluginsEnabled(true);
if (Build.VERSION.SDK_INT > 7) {
webSettings.setPluginState(WebSettings.PluginState.ON);
}
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(url)) {
view.loadUrl(url);
return true;
}
return false;
}
#Override
public void onLoadResource(WebView view, String url) {
}
});
webView.loadUrl("http://some-url.com");
EDIT
Does GET or POST posting methods have anything with links' clicks open mobile web browser???
Return true instead of false in shouldOverrideUrlLoading.
From the documentation:
shouldOverrideUrlLoading returns True if the host application wants to
leave the current WebView and handle the url itself, otherwise return
false.

Categories

Resources