19

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?

Mike
  • 1,481
  • 6
  • 17
  • 22
  • Your newline is probably being escaped when putting it into the database. You have to un-escape it when getting it back. – Falmarri Aug 27 '10 at 17:43
  • 1
    How would I go about doing that? The \n does show up in the text view so I dont think sqlite is stripping it out. – Mike Aug 27 '10 at 19:13

6 Answers6

36

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

Blundell
  • 75,855
  • 30
  • 208
  • 233
20

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.

Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
  • The SQLiteDatabase class does not take care of this itself? – Andrew Oct 27 '11 at 15:14
  • It has been 6 years since your reply. Do you know if there is a current way to do this? I have unicode characters coming from my database (e.g., \u2265) that I now believe are being escaped when put into the database. – Brian Mar 21 '17 at 14:22
1
"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/sqlite-users@sqlite.org/msg43557.html

1

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....:)

Tyler
  • 21,762
  • 11
  • 61
  • 90
Rohan K
  • 1,608
  • 4
  • 21
  • 32
  • I am assuming you meant to surround the \n with white space. That didn't work. I'm a little confused on what you mean by use the new line keyword. There was no exception thrown. – Mike Aug 27 '10 at 19:12
0

A text area can be in multi line or single line mode. When it is in single line mode newline characters '\n' will be treated as spaces. When in doubt, to switch multi line mode on you can use the following code:

    setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_MULTI_LINE);

I had the problem that the same code did not work on honeycomb and on froyo, which seem to have different defaults. I am now also excluding the flag when I want to force a field to be single lined.

From the Android doc:

public static final int TYPE_TEXT_FLAG_MULTI_LINE Added in API level 3

Flag for TYPE_CLASS_TEXT: multiple lines of text can be entered into the field. If this flag is not set, the text field will be constrained to a single line. Constant Value: 131072 (0x00020000)

http://developer.android.com/reference/android/text/InputType.html#TYPE_TEXT_FLAG_MULTI_LINE

You have to set the flag before you populate the field.

  • I tried for 30 minutes to get my TextView to show newlines and what finally worked was to remove the above code from my app. I'm not sure what it should be, but this line of code was the only thing preventing me from using "\n" newlines. – Matt Robertson Oct 30 '12 at 21:25
  • It could be that it works without setting the flag. It also did in my case on some platforms. But setting the flag should not turn your textview into a single line. There must be something else wrong. –  Oct 30 '12 at 21:45
0

Put your Arabic sentence inside a quote and just put "\n" before the quote. This will work.

For example: <string name="title">\n"خلق الله السموات والأرض"</string>

Spidey
  • 397
  • 5
  • 19