Code example for WebView: Displaying web pages

Methods: getSettingsloadUrlsetLayoutParamssetScrollBarStylesetWebChromeClientsetWebViewClient

0
		getWindow().requestFeature(Window.FEATURE_PROGRESS);
 
		// Makes Progress bar Visible 
		getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
 
		WebView webView = new WebView(this);
		webView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT, 1f));
		webView.loadUrl(getIntent().getDataString());
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setSupportZoom(true);
		webView.getSettings().setBuiltInZoomControls(true);
//		webView.getSettings().setLoadWithOverviewMode(true); // not supported by older versions of Android 
		webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		webView.getSettings().setUseWideViewPort(true);
 
		setContentView(webView);
		 
		webView.setWebViewClient(new WebViewClient() {
			@Override 
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().startsWith("https://")){
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
					startActivity(intent);
					finish();
					return true; 
				} 
				view.loadUrl(url);
				setTitle(url);
				return false; 
			} 
 
			@Override 
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
			} 
 
			@Override 
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
			} 
		}); 
 
		webView.setWebChromeClient(new WebChromeClient() {
			@Override 
			public void onProgressChanged(WebView view, int newProgress) {
				setProgress(newProgress * 100); // Make the bar disappear after
												// URL is loaded 
			}