How to increase the spacing between paragraphs in text form

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

I tried using lineSpacingExtra and lineSpacingMultiplier , but it sets spaces for each line (including the paragraph).

I need something like this:

Multiple filling

+34
android textview
source share
5 answers

You can use Spannable for 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 above code replaces all line breaks with two line breaks. After that, it sets the absolute size for each break of the second line.

+20
source share

you can use

 Html.fromHtml(String); 

This will help you write a line in the form of html, where you can use html tags such as <p> , <h1> , etc.

For example:

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

I ran into a similar problem. Finally, I fixed the mutable text for the newline character. Here is the code coming.

  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); 
+2
source share

I'm afraid this is not configurable in TextView. Here is a document with all the attributes of a TextView: https://developer.android.com/reference/android/R.styleable#TextView I think the best way to do this is to use one TextView for each paragraph, and then define a field between the TextView.

I think this is a similar design concept, like the p tag in HTML. There is an entire paragraph inside the tag, then determine the location of the p tag.

0
source share

There is no need to manipulate the spacing between paragraphs.

FROM_HTML_MODE_LEGACY: Separate block-level elements with blank lines (two newline characters) between them. This is a legacy before N.

 textView.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY) } else { Html.fromHtml(html) } 
0
source share

All Articles