Home » Android » How to add a line break in an Android TextView?

How to add a line break in an Android TextView?

Posted by: admin March 10, 2020 Leave a comment


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(r.id.txtSubTitle);

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?

How to&Answers:

\n works for me, like this:

<TextView android:text="First line\nNext line"


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


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 😀


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>


This worked for me

android:text="First \n Second"


This worked for me, maybe someone will find out this helpful:

TextView textField = (TextView) findViewById(R.id.textview1);
textField.setText("First line of text" + System.getProperty("line.separator") + "Linija 2");


If you’re using XML to declare your TextView use android:singleLine = "false" or in Java, use txtSubTitle.setSingleLine(false);


Used Android Studio 0.8.9. The only way worked for me is using \n.
Neither wrapping with CDATA nor <br> or <br /> worked.


I use the following:

YOUR_TEXTVIEW.setText("Got some text \n another line");


very easy : use “\n”

    String aString1 = "abcd";
    String aString2 = "1234";
    mSomeTextView.setText(aString1 + "\n" + aString2);

\n corresponds to ASCII char 0xA, which is ‘LF’ or line feed

\r corresponds to ASCII char 0xD, which is ‘CR’ or carriage return

this dates back from the very first typewriters, where you could choose to do only a line feed (and type just a line lower), or a line feed + carriage return (which also moves to the beginning of a line)

on Android / java the \n corresponds to a carriage return + line feed, as you would otherwise just ‘overwrite’ the same line


Try to double-check your localizations.
Possible, you trying to edit one file (localization), but actually program using another, just like in my case. The default system language is russian, while I trying to edit english localization.

In my case, working solution is to use “\n” as line separator:

    <string name="string_one">line one.
    \nline two;
    \nline three.</string>


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

It’s HTML escaped code for <br/>

And then you can add text to TexView:



You could also use the String-Editor of Android Studio, it automatically generates line brakes and stuff like that…


As Html.fromHtml deprecated I simply I used this code to get String2 in next line.

textView.setText(fromHtml("String1 <br/> String2"));


    public static Spanned fromHtml(String html){
        Spanned result;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            result = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
        } else {
            result = Html.fromHtml(html);
        return result;


The most easy way to do it is to go to values/strings (in your resource folder)

Declare a string there:

    <string name="example_string">Line 1\Line2\Line n</string>

And in your specific xml file just call the string like

    android:text="@string/example_string" />


I found another method:
Is necessary to add the “android:maxWidth=”40dp”” attribute.
Of course, it may not work perfectly, but it gives a line break.


\n was not working for me. I was able to fix the issue by changing the xml to text and building the textview text property like below.

android:text="Line 1
Line 2
Line 3


Hopefully This helps those who have said that \n did not work for them.


I’m reading my text from a file, so I took a slightly different approach, since adding \n to the file resulted in \n appearing in the text.

    final TextView textView = (TextView) findViewById(R.id.warm_up_view);
    StringBuilder sb = new StringBuilder();
    Scanner scanner = new Scanner(getResources().openRawResource(R.raw.warm_up_file));
    while (scanner.hasNextLine()) {



Maybe you are able to put the lf into the text, but it doesn’t display? Make sure you have enough height for the control. For example:



May be wrong:



In my case, I solved this problem by adding the following:



try this:

         TextView calloutContent = new TextView(getApplicationContext());
         calloutContent.setText(" line 1" + System.getProperty ("line.separator")+"  line2" );