How to Add a Line Break in an Android Textview

How to add a line break in an Android TextView?

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

How do I add a newline to a TextView in Android?

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

How do I insert a line break in a String to show on an Android TextView widget?

Ah so now since you say it's from the SQLite database, that's the issue there.

You can refer to the answer by Blundell here.

The issue is that SQLite will escape your new line characters so when you retrieve it again, it will come out as \\\n or equivalent in which you will need to unescape to display properly (so remove the extra slash so it is just \n.

So depending on what new line characters are being saved into your database, you will need to handle accordingly.

So using the linked answer, you could do something like this (will depend on the number of backslashes and other characters you need to do):

textView1.setText(sample.replaceAll("\\\\n", "\n"));

If you need to do it in multiple places create a function for this.

Android TextView: how to set line breaks in 2 TextViews to be the same

Place a text to one of the TextViews and then use the text inside as a source for the second text view using:

Detect where Android's TextView would insert a line break

The second text view should have a little more "width capacity" to be able to place everything what is in the first view without making its "own" line breaks.

Java android: appending a newline using TextView

If you just want to have some empty space between two other views, you could do this in your XML (assuming you're using XML for the layout). Something like this could work, basically putting in a View with a transparent background and given height. This is assuming you have whatever parameters you want in your TextViews.

<TextView />

<View android:background="#00000000"
android:layout_height="12dp" //or whatever density pixel height you want
android:layout_width="fill_parent" />

<TextView />

Also, in what you tried above... you could try a space and newline... that might work.

nline.setText(" \n");

Android textview not supporting line break

I've been having the exact same problem under the exact same circumstances. The solution is fairly straight forward.

When you think about it, since the textview widget is displaying the text with the literal "\n" values, then the string that it is being given must be storing each "\n" like "\\n". So, when your XML string is read and stored, all occurrences of "\n" are being escaped to preserve that text as literal text.

Anyway, all you need to do is:

fireBody.setText(fireText.replace("\\n", "\n"));

Works for me!



Related Topics



Leave a reply



Submit