Tuesday, April 17, 2018

How to increase the spacing between paragraphs in a textview

Leave a Comment

I have some text that have more than one paragraph (using "\n") and want to put a spacing between the paragraphs, but without using "\n\n". But the text from the same paragraph I want to keep them with a lower space.

I tried using lineSpacingExtra and lineSpacingMultiplier but it sets spaces to every line (insinde the paragraph too).

I want something like this:

Multiparagraph padding

2 Answers

Answers 1

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.

Answers 2

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>")); 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment