dropsOfJupiter dropsOfJupiter - 6 months ago 62
Android Question

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(r.id.txtSubTitle);
txtSubTitle.setText(Html.fromHtml(getResources().getString(R.string.sample_string)));


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?

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

Thank you all for your help!

Comments