Showing a page in WebView is really simple. Below code will enable to do that
References
http://developer.android.com/guide/webapps/webview.html
WebView graphWebView = (WebView)findViewById(R.id.webView);
if(graphWebView != null)
{
WebSettings webSettings = graphWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
graphWebView.setWebViewClient(new AppWebViewClient());
graphWebView.loadUrl("http://google.com");
}
It is important to set the WebView client because that actually decides whether the web page to be loaded in the web view or should be outside. Little odd as we think it should have been other way.
private class AppWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.google.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;
}
}
also to note, we should have setJavaScriptEnabled(true) inorder to enable the javascript code to be working on this page.
We can also load webpages from file system that can inturn also hit the servers.
File lFile = new File(Environment.getExternalStorageDirectory() + "/" + "graph.html");
Log.v(LOG_TAG,"File object is :"+lFile);
Log.v(LOG_TAG,"File Path is :"+lFile.getAbsolutePath());
graphWebView.loadUrl("file:///" + lFile.getAbsolutePath());
Here it is very important to have the prefix file:/// otherwise, the loading wont work.
References
http://developer.android.com/guide/webapps/webview.html
No comments:
Post a Comment