2

I'm getting some data from an SQLite database. The data consists of text and some tabs (\t) and newline (\n) elements. When I print the string from a file, everything is shown correctly, but if I get my string from the database, the newline and tabs are shown in text.

For example:

This is my first row\tText afterTab\nThis is my second row\tText after second tab

Here is my sourcecode:

java:

textfieldAntwort = (TextView) findViewById(R.id.textViewAntwort);
textfieldAntwort.setText(frageundantwort.getAntwort());

xml:

<TextView
        android:id="@+id/textViewAntwort"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textViewFrage"
        android:layout_below="@+id/ima_frage"
        android:layout_marginTop="20dp"
        android:text="@string/txt_answer" />
Carl Anderson
  • 3,446
  • 1
  • 25
  • 45
tom_tom
  • 465
  • 1
  • 7
  • 18
  • possible duplicate of [New Line character \n not displaying properly in textView Android](http://stackoverflow.com/questions/3586763/new-line-character-n-not-displaying-properly-in-textview-android) – Carl Anderson Dec 30 '13 at 22:13

1 Answers1

4

You need to unescape your string

String s = unescape(stringFromDatabase) 

before you place it in your TextView.

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

Source: New Line character \n not displaying properly in textView Android

Community
  • 1
  • 1
Carl Anderson
  • 3,446
  • 1
  • 25
  • 45