I am using below code to open a link in web view -
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setVisibility(View.VISIBLE);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl(link);
But it opens the link in browser.
I want that the url should only open in my web view. Is there any mistake in the code. Please suggest.
Thanks in advance.
You need to override WebViewClient of your webview and set it. Like it is mentioned in one of the comments on your question.
mWebView.setWebViewClient(new WebViewClient());
And google first, you may not need to post the question.
You should init WebViewClient: mWebView.setWebViewClient(new WebViewClient())
This might help:
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//set URL Here
return false;
}
}
Related
I need in-app browser to hit a URL and authenticate a user,that website pop up a window (on all other browser), but it is not showing pop up on the WebView.
This is my code
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
webView.loadUrl(url);
return true;
}
});
As stated in this issue at Google Code, you should use WebChromeClient within your webview.
WebView wv=new WebView(this);
wv.setWebChromeClient(new WebChromeClient());
I'm trying to remove the webview header, I've tried a number of possible solutions but to no avail.
One of my attempts:
android:theme="#android:style/Theme.NoTitleBar"
I'm loading the webview using this code:
setContentView(R.layout.activity_websitenew1);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.loadUrl("http://www.domain.com");
myWebView .setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
What is happening in your case is that the URL is being handled by Android which is choosing the handler for you. This handler is the system's browser application. What you want is for control to stay within your app.
You can do this in code as follows:
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.domain.com");
See http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView,%20java.lang.String) for details.
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 I create webview application to view any website on same webview not browser it works fine but when I click on any link in that website it opens in browser ? how to load all internal links/pages/sections when user click on them at same webview ?
Java version:
You only need to set a WebViewClient for your WebView and then all links will be opened it the same WebView:
webView.setWebViewClient(new WebViewClient());
private class myCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//here load the url in your webview
webview.loadUrl(url);
return true;
}
And this for set in your webview
webView.setWebViewClient(new myCustomWebViewClient());
Kotlin version.
webView.webViewClient = WebViewClient()
You should add a webViewClient of your own.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
in the webViewClient you can override the shouldOverrideUrlLoading() method to be like:
#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;
}
I hope you find it helpful.
I´m actually using a WebView in my Application and it works pretty good.
Now I´d like to be able to change the actual URL, just like that Addressbar in the Android Stock browser, where you can see the URL and where you simply can change it.
How can I enable this bar? Or do I have to implement it myself?!
Thanks!
My Code looks like:
private void setupWebView() {
webview = new WebView(this);
webview.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = webview.getSettings();
webSettings.setUserAgentString("foo");
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(false);
webSettings.setBuiltInZoomControls(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
MyActivity.this.setProgress(progress * 100);
}
});
}
Impossible:
The WebView class is an extension of Android's View class that allows
you to display web pages as a part of your activity layout. It does
not include any features of a fully developed web browser, such as
navigation controls or an address bar. All that WebView does, by
default, is show a web page.
http://developer.android.com/guide/webapps/webview.html
If you want to change URL by code:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");