Split HTML generation into separate functions
This commit is contained in:
parent
9554e2992e
commit
eaebd08661
1 changed files with 32 additions and 14 deletions
|
@ -12,21 +12,39 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
val mainWebView: WebView = findViewById(R.id.mainWebView)
|
val mainWebView: WebView = findViewById(R.id.mainWebView)
|
||||||
|
|
||||||
val html = """
|
val contentHtml = """
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8"/>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Hello, World!</h1>
|
<h1>Hello, World!</h1>
|
||||||
<p>This text is rendered with WebView.</p>
|
<p>This text is rendered with WebView.</p>
|
||||||
</body>
|
"""
|
||||||
|
|
||||||
|
val fullHtml = fullHtml(contentHtml)
|
||||||
|
val encodedFullHtml = Base64.encodeToString(fullHtml.toByteArray(), Base64.NO_PADDING)
|
||||||
|
|
||||||
|
mainWebView.loadData(encodedFullHtml, "text/html", "base64")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun fullHtml(contentHtml: String): String {
|
||||||
|
return """
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>${headInnerHtml()}</head>
|
||||||
|
<body>$contentHtml</body>
|
||||||
</html>
|
</html>
|
||||||
""".trimIndent()
|
"""
|
||||||
|
}
|
||||||
|
|
||||||
val encodedHtml = Base64.encodeToString(html.toByteArray(), Base64.NO_PADDING)
|
private fun headInnerHtml(): String {
|
||||||
|
return """
|
||||||
|
<meta charset="utf-8"/>
|
||||||
|
<style>${fullCss()}</style>
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
|
||||||
mainWebView.loadData(encodedHtml, "text/html", "base64")
|
private fun fullCss(): String {
|
||||||
|
return """
|
||||||
|
body {
|
||||||
|
background-color: #FFFF00;
|
||||||
|
}
|
||||||
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue