1
0
Fork 0

Split HTML generation into separate functions

This commit is contained in:
Alex Kotov 2021-08-14 07:21:33 +05:00
parent 9554e2992e
commit eaebd08661
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08

View file

@ -12,21 +12,39 @@ class MainActivity : AppCompatActivity() {
val mainWebView: WebView = findViewById(R.id.mainWebView)
val html = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
val contentHtml = """
<h1>Hello, World!</h1>
<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>
""".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;
}
"""
}
}