When loading a page in a webview, I can't reference images on another server - if the page the webview loads is example.com, then
img src="http://anotherexample.com/image.jpg" will not load.
Is there a work-around for this?
its amazing, but for me problem solved by adding shouldOverrideUrlLoading:
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
Related
I am trying to display a website in a WebView.
The problem is that it contains the following HTML-tag:
<meta http-equiv="refresh" content="4; URL=subst_001.htm">
This causes the website do disappear (and the subst_001.htm, which contains an error message, appears)
Is there an easy way to solve this problem?
Clarification: I don't have (editing) access to the website I want to open.
I found a solution:
Using a "custom" WebView does the trick.
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new CustomWebViewClient());
public class CustomWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#TargetApi(24)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return true;
}
}
I trying to load "view-source:http://goo.gl/lEYQQf" link in webview. If I load this link in google chrome from PC, the link changes into this "view-source:https://www.facebook.com/connect/login_success.html#access_token=CAAAACZAVC6ygBAKYZBIixFdEDGOHMUhTMxoBcN4pCaPu137s02iHtD0kgKUlW59cU7qAuLgsIXZBOvJ8OLevA3zArPx8v2TkUxbG493Bq7hMiHKHYZC66cbEvQsDBzhAbZCVIx8jOGUc4k3ynW4f65cUZB6m5IZA5tWrnQhnBcGUHMjKtmLZBpsmBzxOZA9ZCwmFgZD&expires_in=0"
I need this access token. I have tried this code:
wv = (WebView) findViewById(R.id.webView);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
Log.e("testing", url);
}
});
wv.loadUrl("view-source:http://goo.gl/lEYQQf");
This is not loading the url. When I remove the "view-source:" part from the given url, in output it is giving me an another link which does not contains the access token. I have tried two more ways but I am not able to get that token. How can I get it?
I want to prevent WebView from loading images except certain image that starts with specific URL.
How to?
Thank you.
This is what you need to do -
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(url.contains("dummyURL"){ // put a string that is common in urls that you want to load
}else {
view.stopLoading();
}
return false;
}
});
Sometimes, when I load my webview with loadUrl, the website is not showing up until I touch the screen or scroll.
It's like I have a webview drawing problem.
Context context = ctx;
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.website);
WebView webView = (WebView) dialog.findViewById(R.id.weburl);
webView.setScrollbarFadingEnabled(false);
//Disable the horizontal scroll bar
webView.setHorizontalScrollBarEnabled(false);
//Enable JavaScript
webView.getSettings().setJavaScriptEnabled(true);
//Set the user agent
webView.getSettings().setUserAgentString("AndroidWebView");
//Clear the cache
webView.clearCache(true);
webView.loadUrl("http://" + WebUrl);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
view.setVisibility(View.VISIBLE);
return false; // then it is not handled by default action
}
#Override
public void onPageFinished(WebView view, String url) {
view.refreshDrawableState();
Log.v("FINISH","FINISH");
}
});
Do anybody have an idea why I have this kind of problem.
What version of Android are you testing on? Pre-4.1 versions of Android seem to have this sort problem with WebViews sometimes.
Add this to the manifest for that Activity to fix the problem:
android:hardwareAccelerated="false"
Make WebView invisible in your layout:
<WebView
...
android:visibility="invisible"
.../>
Now, show it back when onPageFinished occurs for the fist time:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (webView.getVisibility() != View.VISIBLE) {
webView.setVisibility(View.VISIBLE);
}
}
});
I'am not sure, I don't have the whole code, but I think is related to the webViewClient implemented in this function:
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
view.setVisibility(View.VISIBLE);
return false; // then it is not handled by default action
}
here is the officiel definition:
public boolean shouldOverrideUrlLoading (WebView view, String url)
Try to test with your code without implementing shouldOverrideUrlLoading, or make it return true.
I have loaded an external URL in my WebView. Now what I need is that when the user clicks on the links on the page loaded, it has to work like a normal browser and open the link in the same WebView. But it's opening the default browser and loading the page there?
I have enabled JavaScript. But still it's not working. Have I forgotten something?
If you're using a WebView you'll have to intercept the clicks yourself if you don't want the default Android behaviour.
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;
}
}
in some cases you might need an override of onLoadResource if you get a redirect which doesn't trigger the url loading method. in this case i tried the following:
#Override
public void onLoadResource(WebView view, String url)
{
if (url.equals("http://redirectexample.com"))
{
//do your own thing here
}
else
{
super.onLoadResource(view, url);
}
}
Official documentation says, click on a link in a WebView will launch application that handles URLs. You need to override this default behavior
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
or if there is no conditional logic in the method simply do this
myWebView.setWebViewClient(new WebViewClient());
Add this 2 lines in your code -
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
The method boolean shouldOverrideUrlLoading(WebView view, String url) was deprecated in API 24. If you are supporting new devices you should use boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request).
You can use both by doing something like this:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
newsItem.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
});
} else {
newsItem.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
Arulx Z's answer was exactly what I was looking for.
I'm writing an app with Navigation Drawer with recyclerview and webviews, for keeping the web browsing inside the app regardless of hyperlinks clicked (thus not launching the external web browser). For that it will suffice to put the following 2 lines of code:
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
exactly under your WebView statement.
Here's a example of my implemented WebView code:
public class WebView1 extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.wv1); //webview statement
wv.setWebViewClient(new WebViewClient()); //the lines of code added
wv.setWebChromeClient(new WebChromeClient()); //same as above
wv.loadUrl("http://www.google.com");
}}
this way, every link clicked in the website will load inside your WebView.
(Using Android Studio 1.2.2 with all SDK's updated)