Pentium10
Pentium10

Reputation: 208042

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

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

Upvotes: 273

Views: 433290

Answers (30)

S_R
S_R

Reputation: 11

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

Upvotes: 0

Deepak Pradhan
Deepak Pradhan

Reputation: 61

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"

Upvotes: 0

Trigve Hagen
Trigve Hagen

Reputation: 1

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

Upvotes: 0

LKS
LKS

Reputation: 11

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

Upvotes: 0

user11705013
user11705013

Reputation: 21

Just try:

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

In Java file.

Upvotes: 2

Muss Mesmari
Muss Mesmari

Reputation: 133

Try this:

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

Upvotes: 3

Dror
Dror

Reputation: 11

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

Upvotes: 1

epicness42
epicness42

Reputation: 1137

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

Upvotes: 5

simmash
simmash

Reputation: 31

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

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

Upvotes: 3

mobiledev Alex
mobiledev Alex

Reputation: 2318

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

And then you can add text to TexView:

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

Upvotes: 7

Tilottama Gaat
Tilottama Gaat

Reputation: 193

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.

Upvotes: 6

garry
garry

Reputation: 429

One way of doing this is using Html tags::

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

Upvotes: 8

Justin
Justin

Reputation: 800

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

Upvotes: 11

Viral Parmar
Viral Parmar

Reputation: 112

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

This will work.

Upvotes: 3

Rob
Rob

Reputation: 41

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!

Upvotes: 4

Voy
Voy

Reputation: 6364

Side note: Capitalising text using

android:inputType="textCapCharacters"

or similar seems to stop the \n from working.

Upvotes: 1

Pavani
Pavani

Reputation: 1

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.

Upvotes: 0

Palani kumar
Palani kumar

Reputation: 119

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

Upvotes: 2

Jo&#227;o Victor
Jo&#227;o Victor

Reputation: 91

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

Upvotes: 6

user3578286
user3578286

Reputation: 147

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

Upvotes: 10

Ben Wong
Ben Wong

Reputation: 91

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.

Upvotes: 9

ATOzTOA
ATOzTOA

Reputation: 36000

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

Upvotes: 9

Nikhil Borad
Nikhil Borad

Reputation: 2085

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

Upvotes: 0

Christian
Christian

Reputation: 7320

Try:

android:lines="2"

\n should work.

Upvotes: 96

Sebastian Corradi
Sebastian Corradi

Reputation: 1421

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.

Upvotes: 1

viki
viki

Reputation: 1178

This solved my problem.

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

Upvotes: 8

Robert
Robert

Reputation: 651

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

Upvotes: 22

Danny
Danny

Reputation: 1050

Need to keep

1.android:maxLines="no of lines"

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

Upvotes: 4

kakopappa
kakopappa

Reputation: 5095

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

Upvotes: 46

aF.
aF.

Reputation: 66747

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

Upvotes: 21

Related Questions