Last active
July 28, 2020 15:55
-
-
Save emedinaa/052589e92c14d8773e407f0506ed30ec to your computer and use it in GitHub Desktop.
Html.fromHtml API < 24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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