String Resource new line /n not possible?
Asked Answered
L

21

220

It doesn't seem like it's possible to add a new line /n to an XML resource string. Is there another way of doing this?

Litotes answered 28/3, 2011 at 14:13 Comment(4)
you mean \n? do you have an example?Financial
It is supposed to be \n. Works for meLavonda
@anandtripathi, I rolled back your edit because it made the answers meaningless. The OP had wrongly use \n instead of /n, which is the essence of the question. We should not correct OP's code, but only the question body, for the obvious raisonsImagery
Rebuilding project may help.Allegorize
J
438

use a blackslash not a forwardslash. \n

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Hello\nWorld!</string>
</resources>

Also, if you plan on using the string as HTML, you can use &lt;br /&gt; for a line break(<br />)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Hello&lt;br /&gt;World!</string>
</resources>
Joycelynjoye answered 28/3, 2011 at 14:16 Comment(6)
In strings.xml only HTML format it accepts, so &lt;br /&gt; is the appropriate solution providedLavonda
I want to mention something for people like me reaching this page because of Android Studio. In AS when you extract a string using the IDE's tool, it automatically strips newline characters. You can just manually add them back in and it'll work fine. Not sure why it does this.Aureliaaurelian
\n doest work for me , neither change the line neither string has /n . Do you know why ? neither hit enter works , that said in other answersMullein
It seems that at least in the Translations Editor you need to use forward slash, otherwise it it will simply interpret it as a literal \n. Talk about consistent design....Karaganda
just a more recent update on this: neither \n or /n work in strings.xml files. neither does &lt;br /&gt;. however just using <br /> works perfectly for this! I tested them just to make sure, and <br /> is the only one that worked as expected!Coreycorf
\n works at run time but not in the layout inspector.Adornment
C
53

I know this is pretty old question but it topped the list when I searched. So I wanted to update with another method.

In the strings.xml file you can do the \n or you can simply press enter:

<string name="Your string name" > This is your string.

   This is the second line of your string.\n\n Third line of your string.</string>

This will result in the following on your TextView:

This is your string.

This is the second line of your string.

Third line of your string.

This is because there were two returns between the beginning declaration of the string and the new line. I also added the \n to it for clarity, as either can be used. I like to use the carriage returns in the xml to be able to see a list or whatever multiline string I have. My two cents.

Carloscarlota answered 25/6, 2013 at 3:30 Comment(2)
Oh, that's interesting. thanks for sharing. Also, I feel like an idiot for asking this question, but it looks like it has helped a lot of people. LOL!!Litotes
Two successive linebreaks are treated by (at least current versions of) Android as just a single space. github.com/paour/StringResourceTestDerogative
B
45

After I tried next solution

  • add \n
  • add \n without spaces after it
  • use "" and press Enter inside text
  • text with press Enter
  • use lines=2

What solves me is br tag

<string name="successfullyfeatured">successfully<br/> You are now a member</string>

Update

the most reliable solution is to use Translation editor and edit text and it will handle new lines for you

Bisexual answered 8/11, 2020 at 8:0 Comment(3)
Just behaves differently on different platforms for me.Fatuitous
Why <br/> instead of /n?Darcie
i tried new line character but did not work then what worked with me was br tag @DarcieBisexual
A
38

This is an old question, but I found that when you create a string like this:

<string name="newline_test">My
New line test</string>

The output in your app will be like this (no newline)

My New line test

When you put the string in quotation marks

<string name="newline_test">"My
New line test"</string>

the newline will appear:

My 
New line test
Ammamaria answered 25/8, 2017 at 10:14 Comment(3)
Works for me but just in case, remember when you're defining resources like above, you should avoid using Html.fromHtml. ThanksSnath
Wrapping the multiline string in quote marks worked perfectly. Much better than putting \n everywhere (which can quickly become unreadable and confusing). Needless to say, but just in case: if you want to have a literal quote mark in your text, you must escape it: \"Drew
note that the lines in the resource should not be indented, else the output will display with leading spacesWeep
D
9

When using the translations editor in Android Studio just click the icon to the right (or use Shift-Enter on Windows, Alt/Option-Enter on MacOS) and then add line breaks using return.

This will insert \n correctly in the localized strings.xml.

Dombrowski answered 21/1, 2018 at 12:32 Comment(4)
Thanks a lot for this answer.Chanellechaney
There is no icon to the right and Shift-Enter jumps to the next text above, at least on ubuntu.Ichthyo
Updated this with info on where to find the button.Semitic
In Android Studio Translation editor, one has to select the text item, then come down to the bottom of the Translation editor window, where Key, Default value, Translation are displayed, then click on the value (used to edit the text / content), press the "Shift + Enter" here, this opens a text editing window. Here you can create paragraphs as it has to appear and save it. This will be used and displayed as such as paragraphs.Rheology
B
7

In the latest version of Android studio, "\n" is going to be printed like it was meant to be there unless the whole string it's in apostrophes

For Example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">"Hello\nWorld!"</string>
</resources>
Bernat answered 6/4, 2019 at 2:5 Comment(1)
How can I make two break lines?Enclave
S
5

I have tried two options, both are efficient and easy.

  1. I used <br/> HTML tag
<string name="name_detail">
        Hey user. <br/>
        Enjoy a new shopping deal.!!
</string>
  1. Add an encoding line at the top of strings.xml
<?xml version="1.0" encoding="utf-8"?>

and use "\n" to break line Example

<string name="name_detail">
        Hey user.\n
        Enjoy a new shopping deal.!!
</string>
Swear answered 8/6, 2021 at 5:44 Comment(0)
B
4

If you put "\n" in a string in the xml file, it's taken as "\\n"

So , I did :

text = text.Replace("\\\n", "\n");   ( text is taken from resX file) 

And then I get a line jump on the screen

Bespoke answered 15/4, 2016 at 9:37 Comment(3)
Only happens when you add the string with the wizard. If you edit the xml it worksCommoner
I made this mistake too. Thanks for your advice. But I just corrected all appearances of \n myself.Trotskyite
Not a good solution. You should get the text from the resources and use it directly. What if you have to handle more special characters? You gonna write more code for them too? What if you have edge cases?Cloyd
P
3

don't put space after \n, otherwise, it won't work. I don't know the reason, but this trick worked for me pretty well.

Pentagram answered 25/6, 2020 at 14:59 Comment(0)
T
3

You can use this format

<string name="test">
"This is
a new line"
</string>
Tichonn answered 6/12, 2021 at 17:44 Comment(2)
This is the most simple solution. I was working with "\n" for new line in String resources. Finally, using this "quoted data" I done my job easily.Seel
Is it mandatory to use double quotes?Overlarge
M
2

I just faced this issue. didn't work on TextView with constraint parameters. Adding android:lines="2" seems to fix this.

Meenen answered 7/6, 2020 at 19:22 Comment(0)
B
2

I have tried 1-Top word \n Bottom word 2-Top word ENTER Bottom word

Didn't work And finally <br/> worked for me in android studio

In fact the correct way to use it is \n although it is possible to see it done after Build the application. The
only has an effect on the design and not on the application

<string name="available_balance">Available\nBalance</string> //Look good on the app and Available\nBalance in android studio
<string name="available_balance">Available<br/>Balance</string> //Look good in android studio and Available Balance on the App
Buffybuford answered 4/8, 2021 at 7:51 Comment(2)
Please share some code sample of how you tried and how you solved it.Aldehyde
In fact the correct way to use it is \n although it is possible to see it done after Build the application. The <br/> only has an effect on the design and not on the application <string name="available_balance">Available\nBalance</string>Buffybuford
D
2

finally, I found the solution. the <br/> is working on design only and it's not working in running app.
the \n is working on running app only and not working on design
so the solution is to use both if you want to have both results.
something like this

\n<br/>

Example:

Hello \n<br/>World.

Result in design mode:
Hello\n
World.

Result in running app:
Hello
World.

Dehumanize answered 27/9, 2021 at 19:45 Comment(1)
You say you "finally" found the solution, but a solution was provided over a decade ago, and has been widely validated by the community. What does your solution add that isn't already addressed in the accepted answer—or the other seventeen answers, for that matter?Corinacorine
A
1

Very simple you have to just put
\n where ever you want to break line in your string resource.

For example

String s = my string resource have \n line break here;

Allayne answered 26/7, 2017 at 10:54 Comment(2)
How can I make two break lines?Enclave
@Enclave \n\n for two. \n\n\n for three. and so on...Simson
P
1

I want to expand on this answer. What they meant is this icon:

Editor window button

It opens a "real editor window" instead of the limited-feature text box in the big overview. In that editor window, special chars, linebreaks etc. are allowed and converted to the correct xml "code" when saved

Percept answered 20/8, 2019 at 11:53 Comment(0)
S
1

Just use "\n" in your strings.xml file as below

<string name="relaxing_sounds">RELAXING\nSOUNDS</string>

Even if it doesn't looks 2 lines on layout actually it is 2 lines. Firstly you can check it on Translation Editor

enter image description here

Click the down button and you will see this image

enter image description here

Moreover if you run the app you will see that it is written in two lines.

Somersomers answered 4/2, 2021 at 21:30 Comment(0)
B
1
\n tag worked in the strings.xml file

Eg:

 <string name="hello">Hello World &amp;\nWelcome</string>

Output:

Hello World &
Welcome

Note:

  • Make sure that you dont add space before and after of \n"
  • Even if it doesnt looks on new line in the preview window of UI when you run the app it will be displayed in the next line.
Bidget answered 4/6, 2021 at 9:40 Comment(0)
H
1

To see in the design tab the line breaks and in the device i use:

<br/><br/>\n\n

(to 2 break lines)

Humpback answered 19/7, 2021 at 23:32 Comment(0)
S
0

Although the actual problem was with the forward slash still for those using backslash but still saw \n in their layout.

Well i was also puzzled at first by why \n is not working in the layout but it seems that when you actually see that layout in an actual device it creates a line break so no need to worry what it shows on layout display screen your line breaks would be working perfectly fine on devices.

Szombathely answered 8/2, 2021 at 18:27 Comment(0)
B
0

Late answer but i think might help someone.

You will some times see \n in the textview in android studio's layout preview. (But it lies then). Please refresh layout.
Or deploy your app to a real Android Device to see the \n actually working in android string resource.

Betancourt answered 2/12, 2021 at 15:13 Comment(0)
B
-1

Have you tried turning it off and on again ?! This is a very old question but no other answer seem to suggest this.
It could be that the layout preview on android studio is not properly displaying the textView. So you could try exiting android studio and launching it again.
I guess this can happen if android studio was open while the computer was in sleep/hibernate etc.
Anyway, worth a shot.

Brothers answered 15/3, 2021 at 10:59 Comment(1)
My layout preview really did display 'SPEED\nDEPTH" on a button until after a complete rebuild. After that my buttons had the correct two line caption.Towers

© 2022 - 2024 — McMap. All rights reserved.