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

2

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

2

Try:

android:lines="2"

n should work.

1

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

0

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

1

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 😀

2

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 🙂

0

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

<TextView
   android:id="@+id/txtTitlevalue"
   android:text="Line1: rn-Line2rn-Line3"
   android:layout_width="54dip"
   android:layout_height="fill_parent"
   android:textSize="11px" />

I think this will work.

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

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.

2

This solved my problem.

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

0

One way of doing this is using Html tags::

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

Also you can add <br> instead of n.

And then you can add text to TexView:

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

1

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

1

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.

0

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

1

Need to keep

1.android:maxLines="no of lines"

2.And use n for getting of the next Lines

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!

6

 android:text="Previous Line 
 Next Line" 

This will work.

2

You need to put n in the file string.xml

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

Try this:

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

1

This works fine. Check it for your app.

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 1nText 2nText 3"/>

Just try:

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

In Java file.

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.

Side note: Capitalising text using

android:inputType="textCapCharacters"

or similar seems to stop the n from working.

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

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

3

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.

Programatically: myTV.setText(“My Text” + “n” + myString);

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

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật