The basic idea is to alter every url being loaded in a webview (for example adding/removing get parameters).
I have a custom WebViewClient, in which I have the following method :
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String modifiedUrl = Util.someMethod(url);
super.shouldOverrideUrlLoading(view, modifiedUrl);
}
Will it work or should I put this logic in another method, for example onPagestarted ?
You rather should do something like :
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(conditionForModifyingUrl){
String modifiedUrl = Util.someMethod(url);
view.loadUrl(modifiedUrl);
return true;
}
return false;
}
Calling super.shouldOverrideUrlLoading(view, modifiedUrl) won't work because, as it is its name, this method only checks if the url should be overridden, and does not load the url at all.
Related
After I've filtered out the extension inside my webview, ".png". How can I then use the entire link to open a new activity?
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains(".png")) {
Toast.makeText(getActivity(), "png clicked", Toast.LENGTH_SHORT).show();
Instead of having my toast, I want to load the URL that was clicked. I'm new to programming in general so I'm not sure the approach.
I would really like to know the use case, but assuming that what you want is just to load that PNG in the webview, you could do this:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains(".png")) {
view.loadUrl(url);
}
return true;
}
Kind regards
I have some logic I want my WebView to apply whenever I try to change its url. For links within loaded pages it's fine, I can just do it by using shouldOverrideUrlLoading method on my custom WebViewClient.
But when I explicitly call loadUrl on my WebView, shouldOverrideUrlLoading is not fired (obviously, or it would produce an endless loop when calling loadUrl from within this method).
Is there a way to "preload" a url manually so the shouldOverrideUrlLoading method is called?
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("myapp://")
{
//Do something
}
else {
view.loadUrl(url);
}
return true;
}
This works fine when I click a link from within the rendered page. But if I call:
mWebView.loadUrl("myapp://whatever");
shouldOverrideUrlLoading() is not called. What I am asking is whether there's a way that I can pass a url to my webView so it gets passed through to shouldOVerrideUrlLoading() before actually loading it. (i.e. shouldOVerrideUrlLoading() is called whenever a new url is about to be loaded. That's the event I want to fire)
REF: Android's official WebView documentation
if your app version greater then 'N' you should use this function
shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
check you function and version
If all you're trying to do is re-use your logic, just extract it into your WebViewClient and use that to load the url and then just delegate to this in shouldOverrideUrlLoading():
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return loadUrl(view, url);
}
public boolean loadUrl(WebView view, String url) {
if(url.startsWith("myapp://")
{
//Do something
}
else {
view.loadUrl(url);
}
return true;
}
Then instead of
mWebView.loadUrl("myapp://whatever");
You do:
mWebViewClient.loadUrl(mWebView, "myapp://whatever");
I have a WebView in which I load a page with a custom link (like app://action). I registered the url schemes in the manifest file and when I click on the link, the onResume() method of my Activity is called with the correct data and it works OK.
My problem is that the WebView still try to load the link and my WebView ends up to show a "Web page unavailable" message. I don't want that.
How can I prevent the WebView to load the url?
Here's my code :
WebView banner = ...
banner.setWebViewClient(new WebViewClient() {
#Override
public void onLoadResource(WebView view, String url) {
if (url.startsWith("app://")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url), getContext(), Main.class);
//startActivity(i);
}
}
}
banner.loadUrl("url_to_the_banner");
Use WebViewClient.shouldOverrideUrlLoading instead.
public boolean shouldOverrideUrlLoading(WebView view, String url){
// handle by yourself
return true;
}
WebViewClient Reference
Updates: Method shouldOverrideUrlLoading(WebView, String) is deprecated in API level 24. Use shouldOverrideUrlLoading(WebView, WebResourceRequest) instead.
But it must return false otherwise, so:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(url.startsWith(myString){
// handle by yourself
return true;
}
// ...
return false;
}
I'm using webview in android
and when I call loadURL it does a call to the browser activity
I need to show the webcontent inside my own activity as I have other stuff to be viewed in it
buttons, text, ,.. etc
Use the WebView.setWebViewClient(WebViewClient) method ( http://developer.android.com/reference/android/webkit/WebView.html) and override the shouldOverrideUrlLoading(WebView, String) method like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if ( url.equals( "http://www.example.com" ) ) {
// Do something
return true;
}
// If we couldn't match the URL, the following line
// lets the webview use default behaviour (i.e. open browser)
return super.shouldOverrideUrlLoading(view, url);
}
});
I am developing an android application , in which i have certain Twits with a specific hash tag like #nokia
i have this link https://mobile.twitter.com/search/%23nokia should be open in webview without hiding my Tabs at the bottom.How can i do this?
i have already use the method
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// This line we let me load only pages inside Firstdroid Webpage
if ( url.contains("firstdroid") == true )
/* Load new URL Don't override URL Link */
return false;
// Return true to override url loading (In this case do nothing).
return true;
}
any help
When you return true in this method, you should call:
view.loadUrl(url);
Yes, Olsavage is right. If you want to "override" the URL Loading, you need to return true + call view.loadUrl(url) before you actually return true
Your code should then be:
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// This line we let me load only pages inside Firstdroid Webpage
if ( url.contains("firstdroid") == true )
/* Load new URL Don't override URL Link */
return false;
// Return true to override url loading (In this case do nothing).
view.loadUrl(url);
return true;
}
[edit]:
How about the %23 in your URL ? Is that what you want to achieve? Open the actual #hash in the web browser?
in the webview client, instead of the code.
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url); // open link in the same web view.
return true;
}
});