String Resource new line /n not possible?

AndroidXmlAndroid LayoutNewlineAndroid Resources

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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>

Solution 2 - Android

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: > This is your string. > > This is the second line of your string.\n\n Third line of your 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.

Solution 3 - Android

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

Solution 4 - Android

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

Solution 5 - Android

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.

Solution 6 - Android

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>

Solution 7 - Android

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>

Solution 8 - Android

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

Solution 9 - Android

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.

Solution 10 - Android

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

Solution 11 - Android

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

Solution 12 - Android

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.

Solution 13 - Android

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;

Solution 14 - Android

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.

Solution 15 - Android

\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.

Solution 16 - Android

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

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

(to 2 break lines)

Solution 17 - Android

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

Solution 18 - Android

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.

Solution 19 - Android

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.

Solution 20 - Android

You can use this format

<string name="test">
"This is
a new line"
</string>

Solution 21 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndi JayView Question on Stackoverflow
Solution 1 - AndroidjamesView Answer on Stackoverflow
Solution 2 - AndroidDimesioView Answer on Stackoverflow
Solution 3 - AndroidMahmoud MabrokView Answer on Stackoverflow
Solution 4 - AndroidPeterView Answer on Stackoverflow
Solution 5 - AndroidTorkel BörjesonView Answer on Stackoverflow
Solution 6 - AndroidSventraView Answer on Stackoverflow
Solution 7 - AndroidBharat LalwaniView Answer on Stackoverflow
Solution 8 - AndroidflamandierView Answer on Stackoverflow
Solution 9 - AndroidSaloniGuptsView Answer on Stackoverflow
Solution 10 - AndroidHill KarimovView Answer on Stackoverflow
Solution 11 - AndroidilidiocnView Answer on Stackoverflow
Solution 12 - AndroidMortezaView Answer on Stackoverflow
Solution 13 - AndroidAli NawazView Answer on Stackoverflow
Solution 14 - AndroidAhmet B.View Answer on Stackoverflow
Solution 15 - AndroidPrajwal WaingankarView Answer on Stackoverflow
Solution 16 - AndroidAlejandro IbagueView Answer on Stackoverflow
Solution 17 - AndroidavalanchaView Answer on Stackoverflow
Solution 18 - AndroidHARIS UMAIDView Answer on Stackoverflow
Solution 19 - AndroidNasibView Answer on Stackoverflow
Solution 20 - AndroidAndreas StokidisView Answer on Stackoverflow
Solution 21 - AndroidijkView Answer on Stackoverflow