Umair Ahmed
Umair Ahmed

Reputation: 89

How to add a line break in an Android TextView? \n not working

I am getting data from xml for line break in following format; line1 \n line2.but on setting this string in textview its displaying line1 \n line2 intead of; line1 line2. if i hardcode the the value in textview its giving correct result but getting data from xml is not showing properly...

Upvotes: 1

Views: 5121

Answers (3)

Stefan Oswald
Stefan Oswald

Reputation: 19

Both previous answers were almost there. The BR tag doesn't do so well with Android. However, \n works if you wrap it in a CDATA element as shown below.

In your strings.xml values file add this:

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

Upvotes: 0

Shailendra Madda
Shailendra Madda

Reputation: 21561

If you plan as using the string as HTML, you can use &lt;br /&gt; for a line break(<br />)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Hello&lt;br /&gt;World!</string>
</resources>

(or)

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="title">Hello\nWorld!</string>
    </resources>

Upvotes: 2

Mitesh Shah
Mitesh Shah

Reputation: 1739

<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

Upvotes: 4

Related Questions