[android] String Resource new line /n not possible?

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?

This question is related to android xml android-layout newline android-resources

The answer is


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.


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;


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

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


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.


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


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.


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


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.


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>

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.


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


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to xml

strange error in my Animation Drawable How do I POST XML data to a webservice with Postman? PHP XML Extension: Not installed How to add a Hint in spinner in XML Generating Request/Response XML from a WSDL Manifest Merger failed with multiple errors in Android Studio How to set menu to Toolbar in Android How to add colored border on cardview? Android: ScrollView vs NestedScrollView WARNING: Exception encountered during context initialization - cancelling refresh attempt

Examples related to android-layout

How to check if a key exists in Json Object and get its value How to center the elements in ConstraintLayout Android - how to make a scrollable constraintlayout? Add ripple effect to my button with button background color? This view is not constrained vertically. At runtime it will jump to the left unless you add a vertical constraint Is it possible to put a ConstraintLayout inside a ScrollView? Differences between ConstraintLayout and RelativeLayout How to remove title bar from the android activity? How to have EditText with border in Android Lollipop Android: ScrollView vs NestedScrollView

Examples related to newline

How can I insert a line break into a <Text> component in React Native? Print "\n" or newline characters as part of the output on terminal Using tr to replace newline with space How to write one new line in Bitbucket markdown? Line break in SSRS expression How to insert a new line in Linux shell script? Replace CRLF using powershell How to write new line character to a file in Java What is the newline character in the C language: \r or \n? How to print values separated by spaces instead of new lines in Python 2.7

Examples related to android-resources

Failed linking file resources Execution failed for task ':app:processDebugResources' even with latest build tools getResources().getColor() is deprecated getColor(int id) deprecated on Android 6.0 Marshmallow (API 23) Android getResources().getDrawable() deprecated API 22 Android Studio cannot resolve R in imported project? Android XXHDPI resources Android ImageView setImageResource in code format statement in a string resource file Lint: How to ignore "<key> is not translated in <language>" errors?