Code example for WebView: Displaying web pages

Methods: addJavascriptInterfaceclearViewgetSettingsloadUrlsetBackgroundColorsetWebChromeClientsetWebViewClient

0
		this.useTransparentBackground = useTransparentBackground;
	} 
 
	@Override 
	public View getView(final Context context) {
		WebView w = new WebView(context);
		w.getSettings().setBuiltInZoomControls(useDefaultZoomControls);
		if (useTransparentBackground)
			w.setBackgroundColor(0x00000000);
		w.getSettings().setJavaScriptEnabled(true);
 
		w.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				onPageLoadProgress(progress * 100);
			} 
		}); 
 
		w.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				if (url != null && url.startsWith("market://")) {
					try { 
						Intent marketIntent = new Intent(Intent.ACTION_VIEW,
								Uri.parse(url));
						marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY
								| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
						context.startActivity(marketIntent);
						return true; 
					} catch (Exception e) {
					} 
				} 
				view.loadUrl(url);
				return dontLoadUrlInWebview(url); // then it is not handled by
													// default action 
			} 
 
			@Override 
			public void onPageFinished(WebView view, String url) {
				CookieSyncManager.getInstance().sync();
				view.loadUrl("javascript:window.HTMLOUT.processHTML("
						+ "document.getElementsByTagName(" 
						+ "'body')[0].innerHTML);"); 
			} 
		}); 
 
		w.addJavascriptInterface(new Object() {
			@SuppressWarnings("unused") 
			public void processHTML(String html) {
				onPageLoaded(html);
			} 
		}, "HTMLOUT"); 
		w.clearView();
		w.loadUrl(getUrlToDisplay());
		return w;
	} 
 
	protected abstract void onPageLoaded(String html);