How to add line spacing after every "\n" in a string in android?
Asked Answered
C

2

3

As the question is clearly stated, I want to add a small line spacing after every \n defined in a string in a resource file in Android.

Let's say we have a string defined in xml like this:

<string name="line_test">This is the 1st line. This is still the 1st line.\nThis is the 2nd line.\nThis is the 3rd line. This is still the 3rd line.\nThis is the 4th line.</string>

The normal output when setting it to a TextView will be like the left side of this image. What I want to achieve is the right side of this image.

Image 1

There are some attributes in xml like:

android:lineSpacingMultiplier="2"

and

android:lineSpacingExtra="10dp"

but these attributes will only lead to something like this:

Image 2

So I don't want to add line spacing after every new line because the width of the line is consumed, but only after every defined \n.

Any way to achieve what I want through code or html? By saying html, I mean something like this, but with some modified stuff after adding <br/>:

textView.setText(Html.fromHtml("This is the 1st line. This is still the 1st line.<br/>This is the 2nd line.<br/>This is the 3rd line. This is still the 3rd line.<br/>This is the 4th line."));

It is more like I want to create many paragraphs in the TextView with small line spacing after each paragraph all in one string. I thought that this would be possible to be done through HTML, so that's why I added html tag.


Edit:

I also don't want to add two line breaks because I only want a small line spacing, and putting two line breaks together will have a large line spacing, which I don't want due to some design issues in my app.

Contraband answered 3/2, 2016 at 13:40 Comment(1)
Why don't you give two line breaks? \n\nCowbane
T
3

You can try using this:

public String addNewLine(String string) {
    int count = string.split("\n", -1).length - 1;
    StringBuilder sb = new StringBuilder(count);
    String[] splitString = string.split("\n");
    for (int i = 0; i < splitString.length; i++) {
        sb.append(splitString[i]);
        if (i != splitString.length - 1) sb.append("\n\n");
    }
    return sb.toString();
}
Telecommunication answered 3/2, 2016 at 13:56 Comment(7)
Sorry, it didn't work. There is no even multiple lines. It only transformed the whole string into a single line.Contraband
try with "\n" and tell meTelecommunication
Nothing changed. The whole string is still in one line.Contraband
Ok now i have to leave later ill try another strategieTelecommunication
Ok, no problem. Thanks for your help though.Contraband
I have modifed your code a little bit. I think I will have to add two line breaks rather than one line break because this is the simple solution. I will have to fix my layout though and suite it to the needs. I accepted your answer because it helped me to reach what I wanted. ;)Contraband
A pleasure to help such a young and talented programmer :)Telecommunication
C
5

What about giving two line breaks like this

<string name="line_test">This is the 1st line. This is still the 1st line.\n\nThis is the 2nd line.\n\nThis is the 3rd line. This is still the 3rd line.\n\nThis is the 4th line.</string>

In case of HTML use two <br/> tags

textView.setText(Html.fromHtml("This is the 1st line. This is still the 1st line.<br/><br/>This is the 2nd line.<br/><br/>This is the 3rd line. This is still the 3rd line.<br/><br/>This is the 4th line."));
Cowbane answered 3/2, 2016 at 13:43 Comment(1)
Oh yes, I forgot to mention that I don't want to add two line breaks because I only want a small line spacing, and putting two line breaks together will have a large line spacing, which I don't want due to some design issues in my app. I edited the question now. However, I up-voted your answer because it is still definitely correct. Thanks for your answer.Contraband
T
3

You can try using this:

public String addNewLine(String string) {
    int count = string.split("\n", -1).length - 1;
    StringBuilder sb = new StringBuilder(count);
    String[] splitString = string.split("\n");
    for (int i = 0; i < splitString.length; i++) {
        sb.append(splitString[i]);
        if (i != splitString.length - 1) sb.append("\n\n");
    }
    return sb.toString();
}
Telecommunication answered 3/2, 2016 at 13:56 Comment(7)
Sorry, it didn't work. There is no even multiple lines. It only transformed the whole string into a single line.Contraband
try with "\n" and tell meTelecommunication
Nothing changed. The whole string is still in one line.Contraband
Ok now i have to leave later ill try another strategieTelecommunication
Ok, no problem. Thanks for your help though.Contraband
I have modifed your code a little bit. I think I will have to add two line breaks rather than one line break because this is the simple solution. I will have to fix my layout though and suite it to the needs. I accepted your answer because it helped me to reach what I wanted. ;)Contraband
A pleasure to help such a young and talented programmer :)Telecommunication

© 2022 - 2024 — McMap. All rights reserved.