I did one sample application using WebView, in that web view the URL comes from web services. It\'s working fine, but if I click any link within that WebView, its automatica
This Will open any particular link only in your app ...
WebView web;
web = (WebView) findViewById(R.id.web);
web.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("http://www.xplorerstrick.net")) {
view.loadUrl(url);
progDailog.show();
return true;
}
else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
You need to call wvBikeSite.setWebViewClient, e.g:
MyWebViewClient wvc = new MyWebViewClient();
wvBikeSite.setWebViewClient(wvc);
Where MyWebViewClient overrides shouldOverrideUrlLoading, viz:
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Use this code:
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
You have to set up a webViewClient for your webView.
Sample:
this.mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
You can always open url within webview by using this:
@Override
public void onCreate(Bundle savedInstanceState) {
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onLoadResource(WebView view, String url){
}
}
Try this one, that method is deprecated.
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}