Here is what I use when I do not have access to the source string, for example. for loaded HTML:
// replace newlines with <br> public static String replaceNewlinesWithBreaks(String source) { return source != null ? source.replaceAll("(?:\n|\r\n)","<br>") : ""; }
For XML, you should probably change this instead of <br/> instead.
An example of its use in a function (additional calls removed for clarity):
// remove HTML tags but preserve supported HTML text styling (if there is any) public static CharSequence getStyledTextFromHtml(String source) { return android.text.Html.fromHtml(replaceNewlinesWithBreaks(source)); }
... and another example:
textView.setText(getStyledTextFromHtml(someString));
Lorne Laliberte Apr 17 2018-12-12T00: 00Z
source share