Format long paragraphs in android TextView
have a lot of text (over 10000 words) that I want to display on my Android app. Text contains bullets, paragraphs, and line breaks. I would like to know how to display on my app without manual editing. It is not possible to convert every newline character to \
n, and every tab character to
\t. I’ve also tried parsing data with HTML and then using
Html.fromHtml(getString(R.string.text)). However, the display I get looks completely unformatted.
This is how I want the text to look in the app :
HTML markup is a simple solution to simple problems, such as bold, italicized, or even displaying bullet points. To style text that contains HTML tags, call the Html.fromHtml method. Behind the scenes, the HTML format is converted to span. Note that the HTML class does not support all HTML markup and CSS styles, such as setting a bullet point to another color.
val text = "My text <ul><li>bullet one</li><li>bullet two</li></ul>" txtview.text = Html.fromHtml(text)
Span allows you to achieve multi-style text with more fine-grained customization. For example, you can define paragraphs of text to have bullet points by applying BulletSpan. You can customize the gap between text margins and bullets, as well as the color of bullets. Starting with Android P, you can even set the radius of the dot. You can also create custom implementations for spans. Review the “Creating a Custom SPAN” section below to learn how.
val spannable = SpannableString("My text \nbullet one\nbullet two") spannable.setSpan( BulletPointSpan(gapWidthPx, accentColor),/* start index */ 9, /* end index */ 18,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spannable.setSpan(BulletPointSpan(gapWidthPx, accentColor),/* start index */ 20, /* end index */ spannable.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) txtview.text = spannable