Is there any way to restore a default WebViewClient in WebView?
There is a WebView.setWebViewClient(WebViewClient client) method, but no get companion.
I would like to to load URL in WebView and avoid being sent to native browser in case of redirections but then go back to default behavior - links clicked opening in native browser.
To achieve that I would like set my own WebViewClient temporarily and then restore the default one.
WebView webView = new WebView(this);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://example.com");
// TODO: restore default WebViewClient
to go back to the default behavior try:
webview.setWebViewClient(null);
however you can't do that immediately after you call loadUrl, you need to at least wait for WebViewClient.onPageFinished.
Finally the code:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
webView.setWebViewClient(null);
}
});
webView.loadUrl(url);
Related
How to add notifications to WebView App when there is any changes in the web url. i.e. if user creates a new task in webview then I need to notify user.
Implement WebViewClient and extend onPageFinished() , so you can even see url:
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});
trying to load a simple web page in WebView as below
alway leads to the fact that the browser is being launched with it.
The following code I wrote in my main activity besinde defining
"uses-permission android:name="android.permission.INTERNET" "
in the manifest. There is no Exception thrown which I verified with the catch block.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
WebView myWebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.google.com");
} catch (Exception e) {
Log.e("MyApp", e.getMessage());
}
}
I cannot imagine what coult be simpler than my scenario. I guess as I do not do things like clicking a link
it is not necessary to do things like
// By default, redirects cause jump from WebView to default
// system browser. Overriding url loading allows the WebView
// to load the redirect into this screen.
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Do you have any Idea what could go wrong here?
In my very first example I do not use setWebViewClient(... at all but also in that case the browser opens instead of loading the side into the web view.
Thanks and Regards
Dieter
By returning fase in shouldOverrideUrlLoading(), you're instructing the WebView to ignore loading the URL and open the device's default browser instead.
You'll want to replace the contents of that function with:
// By default, redirects cause jump from WebView to default
// system browser. Overriding url loading allows the WebView
// to load the redirect into this screen.
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
I have a webview in my android app, but when someone navigates around the site, it opens in a new window, i want it to stay inside the webview.. is there a way to do this easily? Here is the code in my activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.shop);
WebView webview;
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.co.uk");
You'll have to create a WebViewClient:
public class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
And then set it to your WebView like this:
webview.setWebViewClient(new myWebViewClient());
Or you can just do that, without creating a new class:
myWebView.setWebViewClient(new WebViewClient());
It works for me.
webview.setWebViewClient(new WebViewClient());
If you want to customize then you should override shouldOverrideUrlLoading (WebView view, String url). but it's deprecated in API 24. You can use public boolean shouldOverrideUrlLoading (WebView view,WebResourceRequest request). actually both of them needs to return false.
Android WebView Example
When a user clicks on a link in the webview. I want to show a dialog asking whether the user wants to view it in default browser, if he opts YES then he should be taken to default browser otherwise it shouldn't load the link at all.
But, the issue I am facing is, I could able to show the dialog inside run() of WebViewClient's overridden method shouldOverrideUrlLoading(). When the user opts YES I am doing startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(url))); to show it in default browser. But, irrespective of the user selects for YES/NO it is showing the link in webview which I want to avoid. Any suggestions appreciated...TIA
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
http://developer.android.com/guide/webapps/webview.html
I resolved the issue, by just reloading the same URL inside shouldOverrideUrlLoading() method. Thanks for your suggestions
I am using webview in my android app. It loads a webpage successfully.When I click on a link, it opens the new page in the webview and not in browser. Now my problem is that on tilting the screen the page gets refreshed and again the homepage is loaded instead of the page where we were earlier. How can I overcome this problem? Thanks in advance..my code is given below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.xyz.com");
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new MyWebViewClient());
}
my webviewclient class:
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}}
Well, that's because when the orientation changes, the activity is finished and restarted. So the onCreate() is called again and the first URL is loaded. You will have to save the state of the webview and restore it on orientation change.
I'm late in the conversation, but this idea is really interesting too:
http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/
Basically:
1 - You make use of savedInstanceState
2 - You create your webView programmatically based on your savedInstanceState so that there is no reloading, just the exact copy of the original webView when tilting.