I have a hybrid application where I have a WebView which is implementing the shouldOverrideUrlLoading method (both the deprecated and the newest version). This should take over control before loading any external links or certain links within my domain. Without going into specifics, the code looks roughtly like this:
private WebView mWebView;
mWebView.setWebViewClient(new myWebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.isExternal() || url.contains("#specialCase")) {
// Do actions
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
I have noticed that all external links work properly, however shouldOverrideUrlLoading is not being called at all when the link is within my domain, so there is no way for me to detect those cases where I want to take over control.
The android documentation states
Give the host application a chance to take over the control when a new
url is about to be loaded in the current WebView.
Does that new mean different domain? Is there anything I am missing or doing wrong? Any ideas on how to detect the user has clicked a link pointing to the same domain?
Thank you in advance.
Finally found the reason why shouldOverrideUrlLoading was never been called.
Apparently the method is only called when the actual loading is about to start. Our web application is a single-page application, hence even though the URL changes, no new page is loaded and shouldOverrideUrlLoading is not called.
Related
Good day!
Guys, I have a problem and want your help.
Through a WebView, how do I get when clicked by the user, do not open the options of standard browsers (eg chrome and internet). I saw an app by clicking the link to the page opens normally however, as if open in a custom browser. I found it very interesting, and I wonder how this mechanics.
If anyone know how to create an app that perform the same procedure I am grateful.
For better doubt the APP is this:
https://play.google.com/store/apps/details?id=com.tachanfil.jornaisdobrasil&hl=pt-BR
You have to set up WebViewClient like
this.mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
Then your app will display all links in your WebView instead of opening browser
Perhaps you are missing setting your client as the webview client - Refer to
http://developer.android.com/reference/android/webkit/WebView.html#setWebViewClient%28android.webkit.WebViewClient%29
I use AdMob service to display advertisement in my android app.
I need to display advertisement using WebView instead of opening external web browser for it.
Overriding onLeaveApplication() method in AdListener doesn't help, external application is opened anyways after running method code.
Any solution?
you mean when you click on your ad it opens in a separate browser window? you would like it open inside the same webview?
then you just need to add a shouldoverrideurlloading. something like this:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Otherwise allow the OS to handle it
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity( intent );
EDIT: sorry, this is only for the actual webview you are using in your app. since admob ads are displayed in their own little banner (which is essentially a webview too) it doesnt make sense to load that webpage into the banner. therefore a browser window has to pop up. may i ask why that is so important to you? I dont think it can be easily realized, meaning its not really possible.
I have an app with a previously-existing, web-based registration process that I am trying to use inside a WebView. I need to add some style tags to the html in order to hide some elements for better displaying the content inside my app. I can get it to work on initial load, but I cannot figure out how to do it from one page to the next inside the WebView. Here is what I have working:
On initial load of the site, I am getting the raw html and appending "<style>MY STYLES HERE</style>" to the string before calling
wv.loadDataWithBaseURL(url, rawHtml, null, "UTF-8", url);
This works perfectly, but if a user clicks a link on the page and it loads another page into the WebView, then this code does not get called and the style tag is lost.
I assume I need to override "shouldOverrideUrlLoading" in the WebViewClient, but I don't know how to intercept the html from here. I thought I would try something like:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String rawHtml = getRawHtml(url) + "<style>...</style>";
wv.loadDataWithBaseURL(url, rawHtml, null, "UTF-8", url);
}
But this obviously sends it into an endless loop of intercepting the load to start a new load.
I have also tried overriding onPageFinished and doing:
wv.loadUrl("javascript:(function() { ... })()");
which works, except that it waits until the entire page is loaded before executing. This causes the page to appear loaded with all of the UI elements in tact, and then all of the ones I am trying to hide suddenly disappear. My ultimate goal is to enhance the look and feel of the site on a mobile device, so this is not an option.
Is there something else I can do in "shouldOverrideUrlLoading" to inject style tags? Or if not, what else can I try?
I've run into this problem, and depending on the number of redirects, etc, we have not been able to make the injected JavaScript available all the time.
At minimum, you should use the wv.loadUrl("javascript:(function() { ... })()"); approach, but call it in both onPageStarted() and onPageFinished().
Depending on the complexity of your pages, you might need to inject the JavaScript in onLoadResource() as well.
I am working on an android project right now and have a question about how to do callbacks in different webviews. I used JSInterface for my project too. Here I have 2 webviews. One has an index page, anther is a overlay(still a html page though.) What I want to do is if any user clicks on some links on the overlay, it should fire a callback function which is written in the java file where the index page was connected to through JSInterface. It might sound confusing, but I have draw something to help make it clear!
Thanks!
You can use a custom URL scheme like myurl://function for your functionality links. Then write an event handler for the WebView's shouldOverrideUrlLoading event in which you decide how to process the URL: either instruct the webview to load it, or do some custom action.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.startsWith("myurl://"))
{
// Parse further to extract function and do custom action
}
else
{
// Load the page via the webview
view.loadUrl(url);
}
return true;
}
I used startsWith to check the URL for this quick and dirty example, but you should consider using android.net.Uri.parse for parsing URLs.
This should allow you to call the Java function foo() without having to go through the first WebView.
If you want to go through the first webview, then you can call a function on the JSInterface like this (where webView1 is the first WebView retrieved through findViewById):
webView1.loadUrl("javascript:myjsinterface.myjsfunc();")
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.