How to make a new line or tab in <string> XML (eclipse/android)?

AndroidXmlEclipseTabsLine

Android Problem Overview


So, in my strings.xml I have a very long text which I want to format somehow. How can I put a tab before the first sentence of the text? Also, what is the code for new line? Thanks

Android Solutions


Solution 1 - Android

Add \t for tab and \n for new line.

Solution 2 - Android

Use \n for a line break and \t if you want to insert a tab.

You can also use some XML tags for basic formatting: <b> for bold text, <i> for italics, and <u> for underlined text

More info:

https://developer.android.com/guide/topics/resources/string-resource.html

Solution 3 - Android

\n didn't work for me. So I used <br/> HTML tag

<string name="message_register_success">
    Sign up is complete. <br/>
    Enjoy a new shopping life at fatherofapps.com!!
</string>

Solution 4 - Android

Use \t to add tab and \n for new line, here is a simple example below.

<string name="list_with_tab_tag">\tbanana\torange\tblueberry\tmango</string>
<string name="sentence_with_new_line_tag">This is the first sentence\nThis is the second scentence\nThis is the third sentence</string>

Solution 5 - Android

add this line at the top of string.xml

<?xml version="1.0" encoding="utf-8"?>

and use

'\n'

from where you want to break your line.

ex. <string> Hello world. \n its awesome. <string>

Output:

Hello world.
its awesome.

Solution 6 - Android

You can use \n for new line and \t for tabs. Also, extra spaces/tabs are just copied the way you write them in Strings.xml so just give a couple of spaces where ever you want them.

A better way to reach this would probably be using padding/margin in your view xml and splitting up your long text in different strings in your string.xml

Solution 7 - Android

\n doesn't seem to work for tools:text

You can use <br/> to get a preview including line breaks, but it won't show up on the device unless you format the text using something like Html.fromHtml()

Solution 8 - Android

for space use \t and for a new line use \n in your XML string, like

<string name="name">\tFirst Sentence\nSecond Sentence</string>

the output will be

    First Sentence
Second Sentence

Solution 9 - Android

Add '\t' for tab

<string name="tab">\u0009</string>

Solution 10 - Android

  • Include this line in your layout xmlns:tools="http://schemas.android.com/tools"

  • Now , use \n for new line and \t for space like tab.

  • Example :

    for \n : android:text="Welcome back ! \nPlease login to your account agilanbu"

    for \t : android:text="Welcome back ! \tPlease login to your account agilanbu"

Solution 11 - Android

Really all of the above did not work for me in Android Studio 4. What worked was:

<br>first line\n</br>second line

Note: Both \n and br-tags was needed.

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
QuestionFlorin-Constantin CiubotariuView Question on Stackoverflow
Solution 1 - AndroidBoris StrandjevView Answer on Stackoverflow
Solution 2 - AndroidGridtestmailView Answer on Stackoverflow
Solution 3 - AndroidNhatVMView Answer on Stackoverflow
Solution 4 - AndroidZelalemView Answer on Stackoverflow
Solution 5 - AndroidAnkit SainiView Answer on Stackoverflow
Solution 6 - AndroidStefan de BruijnView Answer on Stackoverflow
Solution 7 - AndroidMerthan ErdemView Answer on Stackoverflow
Solution 8 - AndroidMuhammad AnasView Answer on Stackoverflow
Solution 9 - AndroidAlireza GhanbariniaView Answer on Stackoverflow
Solution 10 - Androiduser10828639View Answer on Stackoverflow
Solution 11 - AndroidKetobombView Answer on Stackoverflow