Android, format text from Json to HTML removes new lines and white spaces

118 views Asked by At

The text is received from a Json is like this:

  This is sentence 1.

  This is <b>sentence 2</b>.

And I use this code to format to HTML and pass it to a textView:

private void textChecker(TextView view, String formattedText) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        view.setText(Html.fromHtml(formattedText, Html.FROM_HTML_MODE_LEGACY));
    } else {
        view.setText(Html.fromHtml(formattedText));
    }
}

The problem is that the new text will dismiss any white spaces at the beginning or new lines along the way. And the output looks like this:

This is sentence 1. This is sentence 2.

How do I format the text to use the HTML tags and also keep the original formatting/alignment?

1

There are 1 answers

0
cat15ets On

The only solution that seems reasonable, for now, is to replace the required characters with HTML tags, in the json string in java:

jsonText.replace("\r\n", "<br/>");

But if any of you have a more elegant/simple solution, feel free to post it!