Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Line character \n not displaying properly in textView Android

I know that if you do something like

myTextView.setText("This is on first line \n This is on second line");

Then it will display properly like this:

This is on first line
This is on second line

When I store that string in a database and then set it to the view it displays as such:

This is on first line \n This is on second line

Here is the line of code I use to extract the string from the database:

factView.setText(factsCursor.getString(MyDBAdapter.FACT_COLUMN));

I simply populate the database from a text file where each line is a new entry into the table so a line would look like this "This is on first line \n This is on second line" and it is stored as text.

Is there a reason that it isn't displaying the \n characters properly? It must be something to do with the string being in the database. Any suggestions?

like image 542
Mike Avatar asked Aug 27 '10 17:08

Mike


People also ask

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

for the new line in TextView just add \n in middle of your text it works..

How do you add a line in TextView?

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.

How do I limit characters in TextView?

you can extend the TextView class and overwrite the setText() function. In this function you check for text length or word cound. Better than counting the text length or the word cound a better way would be to use the "maxLines" attribute along with "ellipsize" attribute to attain the desired effect.

Can we change the text in TextView?

TextView tv1 = (TextView)findViewById(R. id. textView1); tv1. setText("Hello"); setContentView(tv1);


4 Answers

As Falmarri said in his comment, your string is being escaped when it is put into the database. You could try and unescape the string by calling String s = unescape(stringFromDatabase) before you place it in your TextView.

As a side note, make sure you are using DatabaseUtils.sqlEscapeString() on any kind of data that is from the user or an unknown changeable source when inserting data into the database. This will protect you from errors and SQL Injection.

like image 39
Austyn Mahoney Avatar answered Oct 23 '22 23:10

Austyn Mahoney


I found this question Austyn Mahoney's answer is correct but here's a little help:

   private String unescape(String description) {
    return description.replaceAll("\\\\n", "\\\n");
}

description being the string coming out of your SQLite DB

like image 124
Blundell Avatar answered Oct 23 '22 23:10

Blundell


Try \\n instead of \n. If it throws an exception than use newline keyword in place of \n....newline is one character, ascii 10; it's often entered in a string literal...and will serve your purpose....:)

like image 1
Rohan K Avatar answered Oct 24 '22 01:10

Rohan K


"This is on first line"||x'0A'||"This is on second line"

The || concatenates strings and the x'0A' is an unescaped newline.

If you're inserting records you'll have to replace every newline with "||x'0A'||" (If your string is double quoted). This may seem clumsy compared to the other asnswers. However if your lines are in separate columns this also works in a select:

SELECT firstline||x'0A'||secondline FROM wherever;

I found this while having the same problem you are: http://www.mail-archive.com/[email protected]/msg43557.html

like image 1
Brendan Draper Avatar answered Oct 23 '22 23:10

Brendan Draper