How to increase the spacing between paragraphs in a textview

You can use Spannable's to achieve this:

String formattedText = text.replaceAll("\n", "\n\n");
SpannableString spannableString = new SpannableString(formattedText);

Matcher matcher = Pattern.compile("\n\n").matcher(formattedText);
while (matcher.find()) {
    spannableString.setSpan(new AbsoluteSizeSpan(25, true), matcher.start() + 1, matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

The code above replaces all line breaks with two line breaks. After that it sets absolute size for each second line break.


You can use

Html.fromHtml(String);

This will help you to write string in form of html where you can use the html tags like <p>, <h1> etc

Eg:

myTextView.setText(Html.fromHtml("<p>This is it first<br>paragraph.</p><p>This is the second<br>paragraph.</p>"));

I have faced a similar problem. Finally, I fixed with spanable text for newline character. Here is the code goes.

    String text ="Lorem ipsum dolor sit amet, End of first line.\nBegining of the second line, Aenean justo mi, faucibus eu suscipit id, finibus gravida quam. End of Second Line.\nBegining of the third line, Aliquam ac diam tempus, pharetra nibh at, imperdiet diam.  End of Third Line.\n Begining of the fourth line, Fusce placerat erat dolor, id tristique lacus gravida ac. End of Fourth Line.\nBegining of the Fifth line, Sed id molestie est, sed elementum lectus.  End of Fifth Line.\n";

    text = text.replace("\n","\n\0");
    Spannable spannable = new SpannableString(text);
    for (int i = 0; i < text.length()-1; i++) {
        if (text.charAt(i) == '\n') {
            spannable.setSpan(new RelativeSizeSpan(1.5f), i+1, i+2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    mTextMessage.setText(spannable, TextView.BufferType.SPANNABLE);