As the question indicates, I am working on a TextView
which will show formatted text using SpannableStringBuilder
. It has multiple paragraphs and I would like to know what would be the easiest (or at least the least complicated) way to set spacing between paragraphs using some inbuilt span. Is this possible? Or will I be required to build a custom span class for this?
Asked
Active
Viewed 2,515 times
7

Rameez Hussain
- 6,414
- 10
- 56
- 85
1 Answers
8
Implement the LineHeightSpan
and override chooseHeight
method as follows
@Override
public void chooseHeight(CharSequence text, int start, int end,
int spanstartv, int v, FontMetricsInt fm) {
Spanned spanned = (Spanned) text;
int st = spanned.getSpanStart(this);
int en = spanned.getSpanEnd(this);
if (start == st) {
fm.ascent -= TOP_SPACING;
fm.top -= TOP_SPACING;
}
if (end == en) {
fm.descent += BOTTOM_SPACING;
fm.bottom += BOTTOM_SPACING;
}
}
Don't forget to add \n
at the end of your each paragraph text.

Durgadass S
- 1,098
- 6
- 13
-
If you could provide the implementation of `ILineHeightSpan` for Xamarin, I would fall in love. – Jul 12 '16 at 23:21
-
2Note: this will also affect a cursor height if you are using this span inside EditText. – Sver Jan 27 '17 at 08:09
-
Are TOP_SPACING and BOTTOM_SPACING density independent or not? – fikr4n Nov 06 '17 at 16:48