Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android textview not supporting line break

Tags:

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?

UPDATE

\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"; 

FYI

System.getProperty("line.separator"); 

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?

like image 231
wyoskibum Avatar asked Jul 08 '12 04:07

wyoskibum


People also ask

How to add line break in TextView android?

Add Line Breaks to a TextView Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.

What is Android Ellipsize?

Android Ellipsize Android TextView ellipsize property Causes words in the text that are longer than the view's width to be ellipsized ( means to shorten text using an ellipsis, i.e. three dots …) instead of broken in the middle to fit it inside the given view.

How do you add a line break in Kotlin?

To split a string on newlines, we can use the regular expression \r?\ n|\r which matches with all different line terminator i.e., \r\n , \r , and \n . To skip empty lines, we can change the regular expression to [\r\n]+ . To match with any Unicode linebreak sequence, we can use the linebreak matcher \R .


2 Answers

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!

like image 196
Rob Avatar answered Sep 16 '22 19:09

Rob


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.

Cheers :D

like image 37
Robert Avatar answered Sep 18 '22 19:09

Robert