How to add a line break in an Android TextView?


I am trying to add a line break in the TextView.

I tried suggested \n but that does nothing. Here is how I set my texts.

TextView txtSubTitle = (TextView)findViewById(;

This is my String: <string name="sample_string">some test line 1 \n some test line 2</string>

It should show like so:

some test line 1
some test line 2

But it shows like so: some test line 1 some test line 2.

Am I missing something?

7/21/2011 6:54:39 AM

Accepted Answer

ok figured it out:

<string name="sample_string"><![CDATA[some test line 1 <br />some test line 2]]></string>

so wrap in CDATA is necessary and breaks added inside as html tags

2/9/2019 5:16:25 PM

Android version 1.6 does not recognize \r\n. Instead, use: System.getProperty ("line.separator")

String s = "Line 1"
           + System.getProperty ("line.separator")
           + "Line 2"
           + System.getProperty ("line.separator");

Linebreaks (\n) only work if you put your string resource value in quotes like this:

<string name="sample_string">"some test line 1 \n some test line 2"</string>

It won't do linebreaks if you put it without quotes like this:

<string name="sample_string">some test line 1 \n some test line 2</string>

yes, it's that easy.


Tried all the above, did some research of my own resulting in the following solution for rendering linefeed 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 formatted data.

Cheers :D


This worked for me

android:text="First \n Second"

There are two ways around this. If you use your string as a raw string, you need to use the newline character. If you use it as html, e.g. by parsing it with Html.fromString, the second variant is better.

1) Newline character \n

<string name="sample> This\nis a sample</string>

2) Html newline tag <br> or <br />

<string name="sample> This<br>is a sample</string>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]