How do I add a newline to a TextView in Android?

AndroidNewlineTextview

Android Problem Overview


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" />

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

Try:

android:lines="2"

\n should work.

Solution 3 - Android

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

Solution 4 - Android

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

Solution 5 - Android

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 :)

Solution 6 - Android

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

Solution 7 - Android

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

Solution 8 - Android

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

Solution 9 - Android

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.

Solution 10 - Android

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"));
}

Solution 11 - Android

One way of doing this is using Html tags::

txtTitlevalue.setText(Html.fromHtml("Line1"+"<br>"+"Line2" + " <br>"+"Line3"));

Solution 12 - Android

This solved my problem.

stringVar.replaceAll("\\\\n", "\\\n");

Solution 13 - Android

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

Solution 14 - Android

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

And then you can add text to TexView:

articleTextView.setText(Html.fromHtml(textForTextView));

Solution 15 - Android

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.

Solution 16 - Android

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

Solution 17 - Android

Need to keep

1.android:maxLines="no of lines"

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

Solution 18 - Android

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!

Solution 19 - Android

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

<string name="strtextparkcar">press Park my Car to store location \n</string>

Solution 20 - Android

 android:text="Previous Line &#10; Next Line" 

This will work.

Solution 21 - Android

Try this:

android:text="Lorem Ipsum \nDolor Ait Amet \nLorem Ipsum"

Solution 22 - Android

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"/>

Solution 23 - Android

Just try:

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

In Java file.

Solution 24 - Android

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

Solution 25 - Android

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.

Solution 26 - Android

Side note: Capitalising text using

android:inputType="textCapCharacters"

or similar seems to stop the \n from working.

Solution 27 - Android

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

Solution 28 - Android

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.

Solution 29 - Android

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

Solution 30 - Android

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

Solution 31 - Android

Go to values> strings inside add string

<string name="category">CHOOSE YOUR CATEGORY"\n"TO WATCH</string>

use "\n" to add new line

then add the string name inside the text view

android:text="@string/category"

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
QuestionPentium10View Question on Stackoverflow
Solution 1 - AndroidMacarseView Answer on Stackoverflow
Solution 2 - AndroidChristianView Answer on Stackoverflow
Solution 3 - AndroidkakopappaView Answer on Stackoverflow
Solution 4 - AndroidresnblView Answer on Stackoverflow
Solution 5 - AndroidaF.View Answer on Stackoverflow
Solution 6 - AndroidRobertView Answer on Stackoverflow
Solution 7 - AndroidJustinView Answer on Stackoverflow
Solution 8 - Androiduser3578286View Answer on Stackoverflow
Solution 9 - AndroidBen WongView Answer on Stackoverflow
Solution 10 - AndroidATOzTOAView Answer on Stackoverflow
Solution 11 - AndroidgarryView Answer on Stackoverflow
Solution 12 - AndroidvikiView Answer on Stackoverflow
Solution 13 - AndroidJoão VictorView Answer on Stackoverflow
Solution 14 - Androidmobiledev AlexView Answer on Stackoverflow
Solution 15 - AndroidTilottama GaatView Answer on Stackoverflow
Solution 16 - Androidepicness42View Answer on Stackoverflow
Solution 17 - AndroidDannyView Answer on Stackoverflow
Solution 18 - AndroidRobView Answer on Stackoverflow
Solution 19 - AndroidsimmashView Answer on Stackoverflow
Solution 20 - AndroidViral ParmarView Answer on Stackoverflow
Solution 21 - AndroidMuss MesmariView Answer on Stackoverflow
Solution 22 - AndroidPalani kumarView Answer on Stackoverflow
Solution 23 - Androiduser11705013View Answer on Stackoverflow
Solution 24 - AndroidDrorView Answer on Stackoverflow
Solution 25 - AndroidSebastian CorradiView Answer on Stackoverflow
Solution 26 - AndroidVoyView Answer on Stackoverflow
Solution 27 - AndroidNikhil BoradView Answer on Stackoverflow
Solution 28 - AndroidPavaniView Answer on Stackoverflow
Solution 29 - AndroidLKSView Answer on Stackoverflow
Solution 30 - AndroidTrigve HagenView Answer on Stackoverflow
Solution 31 - AndroidDeepak PradhanView Answer on Stackoverflow