I am using android webview and loading an url. Inside the webview, there are certain click events and on clicking , the UI inside webview is changed. I am not getting any callback in shouldOverrideUrlLoading(...). Is there a way to detect the changes inside webview in android?
The shouldOverrideUrlLoading() is called only when baseUrl changes.
The onPageFinished(WebView view, String url) method is called when every url changes and loads. The Url can be parsed to find out the change.
Related
I know the method onShouldStartLoadWithRequest exists for only ios but is there anything equivalent for android? I need to check the url before the webview loads. I know onNavigationStateChange and onLoadStart gets called initially but i need something even before this.
You can set a webViewClient tpo your webView object and override shouldOverrideUrlLoading(WebView view, WebResourceRequest request) and get the url like request.getUrl()
I am loading a web view in android, initially it loads a URL it shows for 3-4 seconds and then automatically redirected to another URL, I just want to skip the first URL displaying by extending the web view loading...
I have used method
shouldOverrideUrlLoading(WebView view, String url);
I also tried the handler postdealy() method but of no use...
When is shouldOverrideUrlLoading method called?
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
}
});
Is it called during initial loading of url? e.g. webView.loadUrl( "file:///android_asset/html/index.html");
Is it called everytime URL of webview changes?
Any reference? I didn't find one. Thanks
It does however, get called when the WebView to load a different URL from the one the user had requested.
Calling loadUrl() will also trigger the shouldOverrideUrlLoading() method. (Only when a new url is about to be loaded.)
Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If
WebViewClient is not provided, by default WebView will ask Activity
Manager to choose the proper handler for the url. If WebViewClient is
provided, return true means the host application handles the url,
while return false means the current WebView handles the url.
Ref : public boolean shouldOverrideUrlLoading (WebView view, String url)
Below is the answer for your both the questions:
As per the document, it will manage every time new URL is about to load in current WebView.
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();")