272

When I define a TextView in xml, how do I add a new line to it? \n seems not to work.

<TextView
   android:id="@+id/txtTitlevalue"
   android:text="Line1 -\nLine2"
   android:layout_width="54dip"
   android:layout_height="fill_parent"
   android:textSize="11px" />
Pentium10
  • 204,586
  • 122
  • 423
  • 502

32 Answers32

321

Don't trust the Visual editor. Your code does work in the emu.

Macarse
  • 91,829
  • 44
  • 175
  • 230
94

Try:

android:lines="2"

\n should work.

Christian
  • 7,062
  • 9
  • 53
  • 79
46

try System.getProperty("line.separator");

kakopappa
  • 5,023
  • 5
  • 54
  • 73
44

I think this has something to do with your HTM.fromHtml(subTitle) call: a "\n" doesn't mean bupkis to HTML. Try <br/> instead of "\n".

resnbl
  • 1,443
  • 13
  • 10
22

Tried all the above, did some research of my own resulting in the following solution for rendering line feed escape chars:

string = string.replace("\\\n", System.getProperty("line.separator"));
  1. Using the replace method you need to filter escaped linefeeds (e.g. '\\\n')

  2. Only then each instance of line feed '\n' escape chars gets rendered into the actual linefeed

For this example I used a Google Apps Scripting noSQL database (ScriptDb) with JSON formated data.

Cheers :D

Jared
  • 25,627
  • 7
  • 56
  • 61
Robert
  • 631
  • 7
  • 5
22

First, put this in your textview:

android:maxLines="10"

Then use \n in the text of your textview.

maxLines makes the TextView be at most this many lines tall. You may choose another number :)

aF.
  • 64,980
  • 43
  • 135
  • 198
11

Make sure your \n is in "\n" for it to work.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Justin
  • 800
  • 2
  • 11
  • 26
10
<TextView
   android:id="@+id/txtTitlevalue"
   android:text="Line1: \r\n-Line2\r\n-Line3"
   android:layout_width="54dip"
   android:layout_height="fill_parent"
   android:textSize="11px" />

I think this will work.

Ziem
  • 6,579
  • 8
  • 53
  • 86
user3578286
  • 147
  • 4
  • 10
9

I just solve the same problem, put below attributes in xml

android:lines="2" android:maxLines="4" android:singleLine="false"

work.Html.fromHtml("text1 <br> text2").toString() also work.

duggu
  • 37,851
  • 12
  • 116
  • 113
Ben Wong
  • 91
  • 1
  • 1
9

My 2 cents, using Android TV.

Add \n in XML strings, while reading:

public void setSubtitle(String subtitle) {
    this.subtitle = subtitle.replace("\\n", System.getProperty("line.separator"));
}
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
8

This solved my problem.

stringVar.replaceAll("\\\\n", "\\\n");
viki
  • 1,178
  • 1
  • 17
  • 22
8

One way of doing this is using Html tags::

txtTitlevalue.setText(Html.fromHtml("Line1"+"<br>"+"Line2" + " <br>"+"Line3"));
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
garry
  • 419
  • 5
  • 10
7

Also you can add &lt;br&gt; instead of \n.

And then you can add text to TexView:

articleTextView.setText(Html.fromHtml(textForTextView));
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
mobiledev Alex
  • 2,228
  • 2
  • 28
  • 30
  • Not in the XML you can't. It gives a compile error - `android:text` can't contain < or > characters. – Timmmm Oct 22 '12 at 12:12
6

System.getProperty("line.separator"); this work for me.

Ziem
  • 6,579
  • 8
  • 53
  • 86
João Victor
  • 91
  • 1
  • 6
6

If you debug, you will see that the string is actually "\ \r\ \n" or "\ \n", ie, it is escaped. So if you massage that string, to get rid of the extra \, you will have your solution. This is true especially if you are reading from a database.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Tilottama Gaat
  • 193
  • 3
  • 7
5

You need to put the "\n" in the strings.xml file not within the page layout.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
epicness42
  • 1,137
  • 11
  • 12
  • This worked for me, where Visual Editor was adding extra backslashes trying to display the backslash in \n – dp2050 Apr 07 '20 at 04:23
4

Need to keep

1.android:maxLines="no of lines"

2.And use \n for getting of the next Lines

Danny
  • 1,050
  • 3
  • 11
  • 26
4

For me the solution was to add the following line to the layout:

<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    ...
    >

And \n shows up as a new line in the visual editor. Hope it helps!

Rob
  • 41
  • 4
  • 1
    I am not sure if it's related to the question :) – Suleyman May 20 '18 at 22:33
  • I had the same problem that instead of a new line the string "\n" just showed up itself. When i added the line above this wans't the case anymore and the new line showed up – Rob May 20 '18 at 22:44
  • 1
    Maybe it's because you were using `tools:text` and not `android:text` like the OP, but anyways sorry for disturbing :) – Suleyman May 20 '18 at 22:50
  • I think i should have clarified it better, thanks to your comment i now did :) I use `android:text` I think it's just a bug of the visual editor, like the acceptet answer suggests but it is more comfortable to be able to see it :) – Rob May 20 '18 at 22:55
  • Yeah, I understand now, you are right it is more comfortable :) thanks – Suleyman May 20 '18 at 23:07
  • This should be the chosen answer, as it provides practical help instead of just reassurance. – Matan Koby Feb 12 '19 at 10:47
3

You need to put \n in the file string.xml

<string name="strtextparkcar">press Park my Car to store location \n</string>
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
simmash
  • 31
  • 1
3
 android:text="Previous Line &#10; Next Line" 

This will work.

Viral Parmar
  • 112
  • 1
  • 3
3

Try this:

android:text="Lorem Ipsum \nDolor Ait Amet \nLorem Ipsum"
4b0
  • 21,981
  • 30
  • 95
  • 142
Muss Mesmari
  • 123
  • 10
  • OP tried almost exactly this but had a leading "-" in his example. If the space in your suggestion makes a difference I would elaborate on that in the answer. – Omnibyte Jun 13 '22 at 09:11
2

This works fine. Check it for your app.

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 1\nText 2\nText 3"/>
Palani kumar
  • 119
  • 1
  • 10
2

Just try:

Textview_name.settext("something \n  new line "):

In Java file.

4b0
  • 21,981
  • 30
  • 95
  • 142
1

Make sure you are using your_package.R and not android.R

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Dror
  • 11
  • 1
1

RuDrA05's answer is good, When I edit the XML on eclipse it does not work, but when I edit the XML with notepad++ it DOES work.

The same thing is happening if I read a txt file saved with eclipse or notepad++

Maybe is related to the encoding.

Sebastian Corradi
  • 1,353
  • 2
  • 14
  • 25
1

Side note: Capitalising text using

android:inputType="textCapCharacters"

or similar seems to stop the \n from working.

Voy
  • 5,286
  • 1
  • 49
  • 59
0

for the new line in TextView just add \n in middle of your text it works..

nifCody
  • 2,394
  • 3
  • 34
  • 54
Nikhil Borad
  • 2,065
  • 16
  • 20
0

If the TextView is in any Layout (LinearLayout for example), try to change the orientation attribute to vertical as android:orientation="vertical" for the layout or change the TextView attribute android:singleLine="true" to create a new line after it.

Alex
  • 5,240
  • 1
  • 31
  • 38
Pavani
  • 1
0

Programatically: myTV.setText("My Text" + "\n" + myString);

LKS
  • 11
  • 2
0

Create a string in your stings.xml

<resources>
    ...
    <string name="new_line">\u000A</string>
</resources>

Then in you code reference the string

val linkString = "This is in the first line.${getString(R.string.new_line)}This is on the second line."
Trigve Hagen
  • 1
  • 1
  • 4
0

Go to values> strings inside add string

CHOOSE YOUR CATEGORY"\n"TO WATCH

use "\n" to add new line

then add the string name inside the text view

android:text="@string/category"

0

You can get text in next line by simply adding android:orientation="vertical" in linear layout in activity.main.xml

S_R
  • 1
  • 3