I'm developing an Android app for reading epub books and I use WebView to display the chapter of the book. Inside the book there are internal links with an anchor. (file://some_path/some_file.html#some_hash). When the user clicks on this internal link the WebView goes to the required anchor on all phones except all Samsung devices.
Have you tried to provide some custom webviewclient, try this, it may help:
myWebView.setWebViewClient(new MyCustomWebViewClient());
...
...
public class MyCusomWebViewClient extends WebViewClient {
public HelloWebViewClient() {}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
Related
Edit:
I found out that the web site is a single page website and that's probably the reason I didn't catch the change of the url so now my question is: Can I know the url changed on a single page website?
I have a webView which load a web site.
shouldOverrideUrlLoading called only once, on the first load of the page but not when it redirected inside the web.
This is the code of the webClient I use:
private class WebViewClientServiceGuide extends MyWebViewClient {
private WebView wv;
public WebViewClientServiceGuide(WebView _wv) {
wv = _wv;
Log.d(TAG, "WebViewClientServiceGuide.ctor");
}
public void onLoadResource(WebView view, String url) {
Log.d(TAG, "WebViewClientServiceGuide - onLoadResource: " + url);
super.onLoadResource(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
//Add history to history manager if needed
Log.d(TAG, "WebViewClientServiceGuide - onPageFinished");
super.onPageFinished(view,url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Log.d(TAG, "WebViewClientServiceGuide - shouldOverrideUrlLoading(WebView view, WebResourceRequest request)");
return super.shouldOverrideUrlLoading(view, request);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(TAG, "WebViewClientServiceGuide - shouldOverrideUrlLoading");
return false;
}
}
Lets say I visited these pages: page1, page2, page3 but catches only page1 on shouldOverrideUrlLoading. But when I examined the history of the webView (copyBackForwardList) I saw all the list (page1, page2, page3).
Why does it happens? (I tried to use other website and catches there all the pages - I know the web site developer should fix it but I want to know why the history stack is different than the urls I catch on shouldOverrideUrlLoading )
Thank you
Found the solution:
I overrode a method of WebViewClient called doUpdateVisitedHistory which gets a url, even on single page website
I have a webview where I perfectly load a google form. In the google form there is a link that redirect to a web page, something like "https://www.google.com/url?q=http://www.mywebsite.com/support/" but no matter what I do I cannot open this link. Everything else work fine.
WebSettings webSettings = supportWebPageWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
supportWebPageWebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//do stuff
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
//do stuff
}
});
The only way to make it work is open the link in a browser.
Anyone had the same problem?
I haven't noticed that clicking on the link it opens a new window, the answer is than here Android - Open target _blank links in WebView with external browser
In webView we can detect user opend which link like following code:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
);
but in custom chrome tab i can't find a way to detect which link opend?
is it possible have control on internal link like webView in CustomTab?
thanks.
It's not possible to track the user navigation that's happening inside a Custom Tab from the host application.
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 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)