Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paragraph spacings using SpannableStringBuilder in TextView

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?

like image 560
Rameez Hussain Avatar asked Sep 10 '14 22:09

Rameez Hussain


1 Answers

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.

like image 200
Durgadass S Avatar answered Oct 23 '22 21:10

Durgadass S