I’m creating a custom view programmatically that is displaying text that is parsed from an XML file. The text is long and contains the “/n” character for force line breaks. For some reason, the text view is displaying the /n and there isn’t any line breaks. Here is my code:
// get the first section body Object body1 = tempDict.get("FIRE"); String fireText = body1.toString(); // create the section body TextView fireBody = new TextView(getActivity()); fireBody.setTextColor(getResources().getColor(R.color.black)); fireBody.setText(fireText); fireBody.setTextSize(14); fireBody.setSingleLine(false); fireBody.setMaxLines(20); fireBody.setBackgroundColor(getResources().getColor(R.color.white)); // set the margins and add to view layoutParams.setMargins(10, 0, 10, 0); childView.addView(fireBody,layoutParams);
The text from the XML file is like this:
Now is the time /n for all good men to /n come to the aid of their /n party
It should display as such;
Now is the time for all good men to come to the aid of their party
Is there are setting that I’m missing?
\r\n works if I hard code it into my view. ie:
String fireText = "Now is the time \r\n for all good men \r\n to come to the aid";
Actually \n also works if i hard code it:
String fireText = "Line one\nLine two\nLine three";
this returns a string of “/n” so there is no need to convert to “/r/n”.
Unfortunately, my data originates in a XML file that is parsed and stored in a hashmap. I tried the following:
String fireText = body1.toString().replaceAll("\n", "\r\n");
The \n is not getting replaced with \r\n. Could it be because I’m converting from an object to String?
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:
Works for me!
For line break you use set text view in xml layout this way.
Now is the time
\r\nfor all good men to
\r\ncome to the aid of their
Tried all the above, did some research of my own resulting in the following solution for rendering line feed 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 formated data.
I also had this problem and I found a really SIMPLE SOLUTION, if setting the text from an XML resources (I know this is not the same as OP, but its worth to know it)
There is no need to add line breaks manually, just add the text as it is BUT quoted.
<string name="test_string">"1. First 2. Second Line 3. Third Line"</string>
your server need to send
To force a line break through the XML of your textview, you need to use \r\n instead of just \n.
So, now your code in the XML becomes
android:text="Now is the time \r\n for all good men to \r\n come to the aid of their \r\n party"
Or if you want to do it programatically, then in your java code :
fireBody.setText("Now is the time \r\n for all good men to \r\n come to the aid of their \r\n party");
You can also declare the text as a string resource value, like this :
<string name="sample_string">"some test line 1 \n some test line 2"</string>
Another easy way to do it would be to change the default attribute of the TextView in your xml file.
<TextView android:id="@+id/tvContent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="false" />
And then use,
textView.setText("First line \nSecond line \nThird line");