143

In my strings.xml file I have a very long text which I want to format.

How can I put a tab before the first sentence of the text? Also, what is the code for new line?

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
  • See my answer here: http://stackoverflow.com/questions/18938403/how-to-preserve-line-breaks-in-xml-string-resources-in-android/31683548#31683548 – Jin Jul 28 '15 at 17:46

11 Answers11

228

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

aleksandrbel
  • 1,422
  • 3
  • 20
  • 38
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
39

\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>
NhatVM
  • 1,964
  • 17
  • 25
  • 2
    In the layout, you can see the \n to be visible but while running \n converts into a new line on the actual device and the
    looks a new line in layout but in actual device it is visible as
    – Simran Sharma Jul 06 '21 at 09:30
38

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Gridtestmail
  • 1,459
  • 9
  • 10
25

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>
Zelalem
  • 392
  • 5
  • 7
17

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.
Ankit Saini
  • 342
  • 3
  • 11
13

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

aleksandrbel
  • 1,422
  • 3
  • 20
  • 38
Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
8

\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()

Merthan Erdem
  • 5,598
  • 2
  • 22
  • 29
  • 2
    In the layout, you can see the \n to be visible but while running \n converts into a new line on the actual device and the
    looks a new line in layout but in actual device it is visible as
    – Simran Sharma Jul 06 '21 at 09:31
  • You are right, edited. Is \n seriously something that tools:text can't handle? – Merthan Erdem Jul 06 '21 at 18:49
  • Forget about `tools:text`, if you used \n for `android:text`, it would still be visible as \n in the layout preview on android studio. But when you run it on an actual device, the \n converts to a new line. – Simran Sharma Jul 08 '21 at 03:52
6

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
helvete
  • 2,455
  • 13
  • 33
  • 37
Muhammad Anas
  • 71
  • 1
  • 2
3

Add '\t' for tab

<string name="tab">\u0009</string>
2
  • 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"

2

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.

Ketobomb
  • 2,108
  • 1
  • 17
  • 27