Reputation: 208042
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
Reputation: 11
You can get text in next line by simply adding android:orientation="vertical"
in linear layout in activity.main.xml
Upvotes: 0
Reputation: 61
Go to values> strings inside add string
CHOOSE YOUR CATEGORY"\n"TO WATCHuse "\n" to add new line
then add the string name inside the text view
android:text="@string/category"
Upvotes: 0
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
Reputation: 21
Just try:
Textview_name.settext("something \n new line "):
In Java file.
Upvotes: 2
Reputation: 133
Try this:
android:text="Lorem Ipsum \nDolor Ait Amet \nLorem Ipsum"
Upvotes: 3
Reputation: 1137
You need to put the "\n"
in the strings.xml
file not within the page layout.
Upvotes: 5
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
Reputation: 2318
Also you can add <br>
instead of \n.
And then you can add text to TexView:
articleTextView.setText(Html.fromHtml(textForTextView));
Upvotes: 7
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
Reputation: 429
One way of doing this is using Html
tags::
txtTitlevalue.setText(Html.fromHtml("Line1"+"<br>"+"Line2" + " <br>"+"Line3"));
Upvotes: 8
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
Reputation: 6364
Side note: Capitalising text using
android:inputType="textCapCharacters"
or similar seems to stop the \n
from working.
Upvotes: 1
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
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
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
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
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
Reputation: 2085
for the new line in TextView just add \n in middle of your text it works..
Upvotes: 0
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
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"));
Using the replace method you need to filter escaped linefeeds (e.g. '\\\n'
)
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
Reputation: 1050
Need to keep
1.android:maxLines="no of lines"
2.And use \n
for getting of the next Lines
Upvotes: 4
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