First, I am going to say this is only an issue on Android 2 and older (4 seems to be unaffected and I didn't test 3).
I have a WebView
that loads html from a string. The HTML looks like this:
<html>
<head>
<link rel='stylesheet' type='text/css' href='http://www.robotsidekick.com/test.css?rev=0' />
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
You can see that css file looks like this:
body {
font-family: "Lucida Console", "Lucida Sans Typewriter", Monaco, "Bitstream Vera Sans Mono", monospace;
}
h1 {
border-bottom: 3px solid #ccc;
}
The WebView
code looks like this:
final WebView webview = new WebView(this);
setContentView(webview);
final String result = "<html><head><link rel='stylesheet' type='text/css' href='http://www.robotsidekick.com/test.css?rev=0' /></head><body><h1>Hello World</h1></body></html>";
webview.loadData(result, "text/html", Encoding.UTF_8.toString());
What Happens
I see the html code in the WebView
as if I had set the mime type to plain text.
What I'd Expect (and what happens in Android 4.x
I see the html in the WebView
There are a few things that have to be true to show the symptoms I am seeing:
- Android 2.x (I was using 2.3.7 and 2.2)
- The css has to be accessed via a url
href="http://www.robotsidekick.com/test.css?rev=0"
as opposed tohref="test.css"
(however it doesn't matter if it's a real css filehref="http://www.thisisnotarealurl.fake/test.css?rev=0
causes the same problem) - The css must have a get parameter
?rev=0
, but it doesn't matter what it is
Also wanted to note that the following did not make a difference:
- Using the appropriate
"
instead of'
in the HTML - Having the
link
tag close itself or not - The contents of the css
- Adding a doctype
<!DOCTYPE html>
to the HTML