Code example for WebView: Displaying web pages

Methods: getSettingsloadUrlsetScrollBarStylesetWebChromeClientsetWebViewClient

0
 
		// Get the WebView. 
		viewer = (WebView) findViewById(R.id.webview);
 
		// Set WebView options. 
		viewer.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR); // Set default zoom level.
		viewer.getSettings().setJavaScriptEnabled(true); // Turn on JavaScript.
		viewer.getSettings().setUseWideViewPort(true); // Allow double-tap to zoom.
		viewer.getSettings().setBuiltInZoomControls(true); // Allow pinch-to-zoom.
		viewer.setWebViewClient(new JJGWebViewClient()); // Use our overridden webview client.
		viewer.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); // Don't let scrollbars add whitespace.
 
		// Progress bar. 
		final Activity activity = this;
		viewer.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				// Add space before loading text and title to put it inside progress bar. 
				activity.setTitle("\u0020\u0020" + getText(R.string.webview_loading));
				activity.setProgress(progress * 100);
				if (progress == 100) {
					activity.setTitle("\u0020\u0020" + viewer.getTitle());
				} 
			} 
		}); 
 
		// Load the URL. 
		viewer.loadUrl(content);
	} 
 
	// Allow back button to go back. 
	public boolean onKeyDown(int keycode, KeyEvent event) {
		// If we have a history, go back one page.