Back in Android’s WebView

When a user navigates a website through a webview, pressing the back button will finish the current activity, instead of going back in the browsing history.

You can change this behavior like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
		mWebView.goBack();
		return true;
	}
	return super.onKeyDown(keyCode, event);
}
Share

How to play/embed YouTube videos in WebView

How to play/embed YouTube videos in WebView

mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.getSettings().setJavaScriptEnabled(true); 

and make sure you have the “embed code” in your html code.
(You will need to change the size of the video though, the default size is huge for a mobile screen!)

Share

WebView: Change background and font color

In order to change a WebView’s background color there is a standard way:

mWebView.setBackgroundColor(Color.Black);

In order to change a WebView’s text font color there is no standard way:
Either you change the font through the html code, or you do this:

htmlData="<font color='black'>" + htmlData + "</font>";
mWebView.loadDataWithBaseURL(null, htmlData, "text/html", "UTF-8", null);
Share

Transform HTML entities to normal text

If you are parsing HTML encoded data like RSS, you’ll probably face HTML entities representing special characters like quotes, the & symbol and more.

It’s fine if you use the WebView to present them, but if you want to display the data somewhere else you have to transform them to “normal text”.

mTextView.setText(Html.fromHtml(htmlData).toString());
Share