Skip to content

Instantly share code, notes, and snippets.

@emedinaa
Last active July 28, 2020 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emedinaa/052589e92c14d8773e407f0506ed30ec to your computer and use it in GitHub Desktop.
Save emedinaa/052589e92c14d8773e407f0506ed30ec to your computer and use it in GitHub Desktop.
Html.fromHtml API < 24
class MainActivity : AppCompatActivity() {
private val html = "<html><ul>Message".plus(
"<li> Prueba 1</li>"
).plus(
"<li> Prueba 2</li>"
).plus(
"<li> Prueba 3</li>"
).plus(
"</ul></html>"
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//textView.text = fromHtmlSupport(html)
//textView.text = withHtmlCompact(html)
textView.text = fromHtmlTagSupport(html)
}
private fun withHtmlCompact(source:String):Spanned{
return HtmlCompat.fromHtml(source,HtmlCompat.FROM_HTML_MODE_LEGACY)
}
@SuppressWarnings("deprecation")
private fun fromHtmlTagSupport(source:String): Spanned {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
Html.fromHtml(source,Html.FROM_HTML_MODE_LEGACY)
}else {
val nSource = source
.replace("<ul>","")
.replace("</ul>","")
.replace("<li>","<p>. ")
.replace("</li>","</p>")
Html.fromHtml(nSource)
}
}
@SuppressWarnings("deprecation")
private fun fromHtmlSupport(source:String): Spanned {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
Html.fromHtml(source,Html.FROM_HTML_MODE_LEGACY)
}else {
Html.fromHtml(source)
}
}
}
//extensions
@SuppressWarnings("deprecation")
fun String.toHtmlSupport():Spanned{
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
Html.fromHtml(this,Html.FROM_HTML_MODE_LEGACY)
}else {
Html.fromHtml(this)
}
}
fun String.toHtmlCompactSupport():Spanned{
return HtmlCompat.fromHtml(this,HtmlCompat.FROM_HTML_MODE_LEGACY)
}
@SuppressWarnings("deprecation")
fun String.toHtmlTagSupport():Spanned{
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
Html.fromHtml(this,Html.FROM_HTML_MODE_LEGACY)
}else {
val nSource = this
.replace("<ul>","")
.replace("</ul>","")
.replace("<li>","<p>. ")
.replace("</li>","</p>")
Html.fromHtml(nSource)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment