I am trying to open a link using Webview in Android. There are some links embedded into WebView. My problem is webview is not opening any link that does not starts with www. For ex, www.google.com is working but maps.google.com is not.I have also tried to override WebViewClient but it didn't work.
One thing I noticed is by putting Toast to see what url is being called in WebViewClient. It showed perfect for www.google.com but returned nothing for other links. I thing WebViewClient is not getting overriden in that case. What could be the reason. Do I have to call any ethod or some property of webview.
Any help will be appreciated.
menuView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
Toast.makeText(getApplicationContext(), "url:--" + url, Toast.LENGTH_LONG).show();
view.loadUrl(url);
return false;
}
});
Properties that I have already set are :
menuView.setVerticalScrollBarEnabled(false);
menuView.setHorizontalScrollBarEnabled(false);
final WebSettings webSettings = menuView.getSettings();
menuView.getSettings().setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
webSettings.setPluginState(PluginState.ON);
webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
menuView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
menuView.getSettings().setBuiltInZoomControls(true);
// Below required for geolocation
menuView.getSettings().setJavaScriptEnabled(true);
menuView.getSettings().setGeolocationEnabled(true);
webSettings.setGeolocationEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
// in oncreate
webview.setWebChromeClient(new wecrome());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadsImagesAutomatically(true);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webview.setWebViewClient(new MyBrowser());
webview.getSettings().setPluginState(PluginState.ON);
webview.loadUrl("http://www.example.net/locations/");
//inner class
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
view.addJavascriptInterface(new Object() {
#JavascriptInterface
public void performClick() throws Exception {
Log.d("LOGIN::", "Clicked");
Toast.makeText(googleplus.this, "Login clicked",
Toast.LENGTH_LONG).show();
}
}, "login");
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
System.out.println("started");
pd.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
pd.dismiss();
System.out.println("ends");
super.onPageFinished(view, url);
}
}
Related
I am loading a pdf in WebView.
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.setWebViewClient(new WebViewClient());
String url = "http://drive.google.com/viewerng/viewer?embedded=true&url=http://" + getIntent().getStringExtra("url");
webView.loadUrl(url);
setContentView(webView);
Whenever the activity is started, WebView is shown blank. But I want to show a progress dialog till the WebView starts rendering pdf file. How can I add a listener to the WebView indicating that the files has been started visible to the user.
public class FGWebViewClient extends WebViewClient {
private WebViewClient wvc;
private static final String TAG=FGWebViewClient.class.getCanonicalName();
ProgressDialog mProgressDialog;
// when you add the webviewclient then show the progressDialog.
public FGWebViewClient(Context context) {
Log.i(TAG, "FGWebViewClient Called...");
this.wvc=wvc;
mProgressDialog=new ProgressDialog(context);
mProgressDialog.setMessage("URL Loading...");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCanceledOnTouchOutside(false);
if(mProgressDialog!=null){
mProgressDialog.show();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG,"shouldOverrideUrlLoading");
return wvc.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i(TAG,"onPageStarted");
wvc.onPageStarted(view, url, favicon);
}
// OnpageFinished just dismiss the progressDialog.
#Override
public void onPageFinished(WebView view, String url) {
if(mProgressDialog!=null&&mProgressDialog.isShowing()){
mProgressDialog.dismiss();
mProgressDialog=null;
}
Log.i(TAG,"onPageFinished");
//To clear cache of webview
//view.clearCache(true);
//clearEveryThing(view);
wvc.onPageFinished(view,url);
}
and call like this
WebView webView=(WebView)v.findViewById(R.id.webView1);
FGWebViewClient webViewClient= new FGWebViewClient(context);
webView.setWebViewClient(webViewClient);
webView.setWebChromeClient(new WebChromeClient());
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadUrl((webUrl).toString());
You can use setWebViewClient and display showProgress as below code
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setPluginState(WebSettings.PluginState.ON);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
showProgress();
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
hideProgress();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
hideProgress();
}
});
String url = "http://drive.google.com/viewerng/viewer?embedded=true&url=http://" + getIntent().getStringExtra("url");
webView.loadUrl(url);
You could create custom class and extend it with WebViewClient. Then override method onPageStarted where you could consider that it started rendering. For e.g :
private class CustomWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
showProgress();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
dismissDialog();
}
}
here my code...am already implemented WebViewClient concept.it works well in web view..In my App have content saring via fb,twitter,g+,,,fb also login and open in webview...but when am click share to fb icon in my app after that all process will work on browser..but i want all process in webview only...
oncreate
webView = (WebView) findViewById(R.id.webView1);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebClient());
if (Build.VERSION.SDK_INT < 8) {
...
} else {
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
}
webView.loadUrl("www.example.com.");
WebViewClient
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
public void onLoadResource (WebView view, String url) {
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/myerrorpage.html");
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
progress.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progress.setVisibility(View.GONE);
}
}
You must override your shouldOverrideUrlLoading() method in WebViewClient class and following code
#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;
}
Go through the documentation for more information.
Use a if clause in your shouldOverrideUrlLoading method that returns false;
Example from Building web apps in web views
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;
}
i'm new to android programing.my problem is that in my webview when i click on any link its opening to a new web browser i dont need this i want it to be opened in my app itself.can someone help me??
this is my code
title = extras.getString("title");
url = extras.getString("url");
TextView text=(TextView) findViewById(R.id.textView1);
text.setText(title);
WebView myWebView =(WebView)findViewById(R.id.WebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
}
i tried this but my webview is not loading its showing a blank screen
web = (WebView) findViewById(R.id.webview01);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.google.com");
public class myWebClient extends 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) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
pls help me to display custom Web Page Not Available error page...in webview..i tried many code and when i run the app it shows app has been stopped
You can override onReceivedError callback of WebViewClient to achieve the desired functionality.
Either you can display some dialog to denot your custom message or load the webview with any custom html available locally in your app.
USE this it working fine on my side. May be it will help you
private WebView webviewSites;
String URL="http://www.google.com";
webviewSites = (WebView) findViewById(R.id.webview_sites);
WebSettings webSettings = webviewSites.getSettings();
webSettings.setSupportMultipleWindows(true);
webSettings.setJavaScriptEnabled(true);
webviewSites.setWebViewClient(new myWebClient());
webviewSites.loadUrl(URL);
public class myWebClient extends 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) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(final WebView view, final String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
// mProgressBar.setVisibility(View.GONE);
Log.i("LOAD_URL", "" + url);
}
I tried to load an url in a webview but this url is not completely loaded :
Whereas, in Chrome app, the url is successfully loaded :
Here is the code that i used :
mWebView.setWebViewClient(new myWebClient());
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setSupportMultipleWindows(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setSavePassword(true);
mWebView.loadUrl(mUrl);
And my myWebClient class:
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
progressB.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
progressB.setVisibility(View.VISIBLE);
return true;
}
public void onProgressChanged(WebView view, int progress) {
progressB.setProgress(progress);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressB.setVisibility(View.GONE);
}
}
seems like you are missing the setting:
mWebView.getSettings().setJavaScriptEnabled(true);
without this call javascript won't work